X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/7f8fa5edc5175ffda9fdfaa28081f799ae485ec0..7a5a0ef9eb13312b2ee8b0b3731dbddb4de7b669:/services/api/test/functional/sys_controller_test.rb diff --git a/services/api/test/functional/sys_controller_test.rb b/services/api/test/functional/sys_controller_test.rb index ab304c1c7c..c3f13cf4b8 100644 --- a/services/api/test/functional/sys_controller_test.rb +++ b/services/api/test/functional/sys_controller_test.rb @@ -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