21703: Improve test failure reporting.
[arvados.git] / services / api / test / functional / sys_controller_test.rb
index ab304c1c7ce94dbd07b3e46981a2957e04093a95..c3f13cf4b8e575b7f4cc088e011585a6cbc53c11 100644 (file)
@@ -91,6 +91,17 @@ class SysControllerTest < ActionController::TestCase
     assert_not_empty Group.where('uuid=? and is_trashed=true', p.uuid)
   end
 
+  test "trash_sweep - role groups are deleted" do
+    p = groups(:trashed_role_on_next_sweep)
+    assert_empty Group.where('uuid=? and is_trashed=true', p.uuid)
+    assert_not_empty Link.where(uuid: links(:foo_file_readable_by_soon_to_be_trashed_role).uuid)
+    authorize_with :admin
+    post :trash_sweep
+    assert_response :success
+    assert_empty Group.where(uuid: p.uuid)
+    assert_empty Link.where(uuid: links(:foo_file_readable_by_soon_to_be_trashed_role).uuid)
+  end
+
   test "trash_sweep - delete projects and their contents" do
     g_foo = groups(:trashed_project)
     g_bar = groups(:trashed_subproject)
@@ -110,6 +121,8 @@ class SysControllerTest < ActionController::TestCase
     assert_not_empty ContainerRequest.where(uuid: cr.uuid)
 
     authorize_with :admin
+    Group.find_by_uuid(g_foo.uuid).update!(delete_at: Time.now - 1.second)
+
     post :trash_sweep
     assert_response :success
 
@@ -122,9 +135,40 @@ class SysControllerTest < ActionController::TestCase
     assert_equal user_nr_was, User.all.length
     assert_equal coll_nr_was-2,        # collection_in_trashed_subproject
                  Collection.all.length # & deleted_on_next_sweep collections
-    assert_equal group_nr_was, Group.where('group_class<>?', 'project').length
+    assert_equal group_nr_was-1,       # trashed_role_on_next_sweep
+                 Group.where('group_class<>?', 'project').length
     assert_equal project_nr_was-3, Group.where(group_class: 'project').length
     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