From ff89c9d812ca506b878b34545a1652e936e5a7c1 Mon Sep 17 00:00:00 2001 From: Terry Brady Date: Fri, 23 Aug 2024 12:09:24 -0700 Subject: [PATCH] fix ruby lock logic --- src/main/ruby/lib/merritt_zk_locks.rb | 20 ++++++++++----- src/main/ruby/spec/zk_spec.rb | 35 ++++++++++++++++----------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/main/ruby/lib/merritt_zk_locks.rb b/src/main/ruby/lib/merritt_zk_locks.rb index 5dbe918..a96d458 100644 --- a/src/main/ruby/lib/merritt_zk_locks.rb +++ b/src/main/ruby/lib/merritt_zk_locks.rb @@ -99,30 +99,38 @@ def self.unlock_collection(zk, mnemonic) # no action end + def self.store_lock_path(ark) + "#{LOCKS_STORAGE}/#{ark.gsub(/:?\//, '_')}" + end + def self.lock_object_storage(zk, ark) - create_ephemeral_lock(zk, "#{LOCKS_STORAGE}/#{ark.gsub(':?/', '_')}") + create_ephemeral_lock(zk, store_lock_path(ark)) end def self.check_lock_object_storage(zk, ark) - zk.exists?("#{LOCKS_STORAGE}/#{ark.gsub(':?/', '_')}") + zk.exists?(store_lock_path(ark)) end def self.unlock_object_storage(zk, ark) - zk.delete("#{LOCKS_STORAGE}/#{ark.gsub(':?/', '_')}") + zk.delete(store_lock_path(ark)) rescue StandardError # no action end + def self.inv_lock_path(ark) + "#{LOCKS_INVENTORY}/#{ark.gsub(/:?\//, '_')}" + end + def self.lock_object_inventory(zk, ark) - create_ephemeral_lock(zk, "#{LOCKS_INVENTORY}/#{ark.gsub(':?/', '_')}") + create_ephemeral_lock(zk, inv_lock_path(ark)) end def self.check_lock_object_inventory(zk, ark) - zk.exists?("#{LOCKS_INVENTORY}/#{ark.gsub(':?/', '_')}") + zk.exists?(inv_lock_path(ark)) end def self.unlock_object_inventory(zk, ark) - zk.delete("#{LOCKS_INVENTORY}/#{ark.gsub(':?/', '_')}") + zk.delete(inv_lock_path(ark)) rescue StandardError # no action end diff --git a/src/main/ruby/spec/zk_spec.rb b/src/main/ruby/spec/zk_spec.rb index 8198087..1e42ae2 100644 --- a/src/main/ruby/spec/zk_spec.rb +++ b/src/main/ruby/spec/zk_spec.rb @@ -846,9 +846,8 @@ def make_batch_json(s = 'bar', u = 'bid-uuid') it :lock_collection do |_x| expect(MerrittZK::Locks.check_lock_collection(@zk, 'foo')).to be(false) - expect(MerrittZK::Locks.lock_collection(@zk, 'foo')).to be(true) + MerrittZK::Locks.lock_collection(@zk, 'foo') expect(MerrittZK::Locks.check_lock_collection(@zk, 'foo')).to be(true) - expect(MerrittZK::Locks.lock_collection(@zk, 'foo')).to be(false) MerrittZK::Locks.unlock_collection(@zk, 'foo') expect(MerrittZK::Locks.lock_collection(@zk, 'foo')).to be(true) @@ -860,30 +859,38 @@ def make_batch_json(s = 'bar', u = 'bid-uuid') it :lock_store do |_x| expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/aaa/111')).to be(false) - expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/aaa/111')).to be(true) + MerrittZK::Locks.lock_object_storage(@zk, 'ark:/aaa/111') expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/aaa/111')).to be(true) - expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/aaa/111')).to be(false) MerrittZK::Locks.unlock_object_storage(@zk, 'ark:/aaa/111') - expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/aaa/111')).to be(true) + expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/aaa/111')).to be(false) + MerrittZK::Locks.lock_object_storage(@zk, 'ark:/aaa/111') + expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/aaa/111')).to be(true) - expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/bbb/222')).to be(true) - expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/bbb/222')).to be(false) + expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/bbb/222')).to be(false) + MerrittZK::Locks.lock_object_storage(@zk, 'ark:/bbb/222') + expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/bbb/222')).to be(true) MerrittZK::Locks.unlock_object_storage(@zk, 'ark:/bbb/222') - expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/bbb/222')).to be(true) + expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/bbb/222')).to be(false) + MerrittZK::Locks.lock_object_storage(@zk, 'ark:/bbb/222') + expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/bbb/222')).to be(true) end it :lock_inventory do |_x| expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/aaa/111')).to be(false) - expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/aaa/111')).to be(true) + MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/aaa/111') expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/aaa/111')).to be(true) - expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/aaa/111')).to be(false) MerrittZK::Locks.unlock_object_inventory(@zk, 'ark:/aaa/111') - expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/aaa/111')).to be(true) + expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/aaa/111')).to be(false) + MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/aaa/111') + expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/aaa/111')).to be(true) - expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/bbb/222')).to be(true) - expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/bbb/222')).to be(false) + expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/bbb/222')).to be(false) + MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/bbb/222') + expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/bbb/222')).to be(true) MerrittZK::Locks.unlock_object_inventory(@zk, 'ark:/bbb/222') - expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/bbb/222')).to be(true) + expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/bbb/222')).to be(false) + MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/bbb/222') + expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/bbb/222')).to be(true) end end