21703: Improve test failure reporting.
[arvados.git] / services / api / test / functional / sys_controller_test.rb
index 2f02d58e3f42ababf9568fa487d03f00bf368c58..c3f13cf4b8e575b7f4cc088e011585a6cbc53c11 100644 (file)
@@ -141,4 +141,34 @@ class SysControllerTest < ActionController::TestCase
     assert_equal cr_nr_was-1, ContainerRequest.all.length
   end
 
+  test "trash_sweep - delete unused uuid_locks" do
+    uuid_active = "zzzzz-zzzzz-uuidlockstest11"
+    uuid_inactive = "zzzzz-zzzzz-uuidlockstest00"
+
+    ready = Queue.new
+    insertsql = "INSERT INTO uuid_locks (uuid) VALUES ($1) ON CONFLICT (uuid) do UPDATE SET n = uuid_locks.n+1"
+    url = ENV["DATABASE_URL"].sub(/\?.*/, '')
+    Thread.new do
+      conn = PG::Connection.new(url)
+      conn.exec_params(insertsql, [uuid_active])
+      conn.exec_params(insertsql, [uuid_inactive])
+      conn.transaction do |conn|
+        conn.exec_params(insertsql, [uuid_active])
+        ready << true
+        # If we keep this transaction open while trash_sweep runs, the
+        # uuid_active row shouldn't get deleted.
+        sleep 10
+      rescue
+        # Unblock main thread
+        ready << false
+        raise
+      end
+    end
+    assert_equal true, ready.pop
+    authorize_with :admin
+    post :trash_sweep
+    rows = ActiveRecord::Base.connection.exec_query("SELECT uuid FROM uuid_locks ORDER BY uuid", "", []).rows
+    assert_includes(rows, [uuid_active], "row with active lock (still held by thread) should not have been deleted")
+    refute_includes(rows, [uuid_inactive], "row with inactive lock should have been deleted")
+  end
 end