From d19cdd007b3960ac105397c32b5d84124d273488 Mon Sep 17 00:00:00 2001 From: Christoph Lupprich Date: Fri, 14 Jan 2022 14:50:46 +0100 Subject: [PATCH 1/3] Limit Rake dependency 11.0 introduces a breaking change that removes the `#last_comment` method that rspec < 3 needs. --- mega_mutex.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mega_mutex.gemspec b/mega_mutex.gemspec index 92a6747..29895e5 100644 --- a/mega_mutex.gemspec +++ b/mega_mutex.gemspec @@ -42,6 +42,6 @@ Gem::Specification.new do |s| s.add_runtime_dependency(%q, ["~> 4"]) s.add_runtime_dependency(%q, [">= 1.1.4"]) s.add_development_dependency("rspec", ["= 1.3.0"]) - s.add_development_dependency("rake", [">= 1.0"]) + s.add_development_dependency("rake", ["< 11.0"]) end From ee8ffd4bad727043b7c5f0b318e38b5f2bfbbc97 Mon Sep 17 00:00:00 2001 From: Christoph Lupprich Date: Fri, 14 Jan 2022 14:51:28 +0100 Subject: [PATCH 2/3] Make specs work --- lib/mega_mutex.rb | 1 - spec/lib/mega_mutex_spec.rb | 8 ++++---- spec/spec_helper.rb | 4 +--- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/mega_mutex.rb b/lib/mega_mutex.rb index 833536f..bbaaa52 100644 --- a/lib/mega_mutex.rb +++ b/lib/mega_mutex.rb @@ -104,4 +104,3 @@ def configuration end end end - diff --git a/spec/lib/mega_mutex_spec.rb b/spec/lib/mega_mutex_spec.rb index 182ff55..86d0fc2 100644 --- a/spec/lib/mega_mutex_spec.rb +++ b/spec/lib/mega_mutex_spec.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/../spec_helper' +require_relative '../spec_helper' module MegaMutex describe MegaMutex do @@ -39,7 +39,7 @@ def logger describe "with the same lock key" do before(:each) do - Redis.new({:host => 'redis.dev', :port => 6379}).delete(mutex_id) + Redis.new({:host => 'redis.dev', :port => 6379}).del(mutex_id) end def mutex_id @@ -106,7 +106,6 @@ def mutex_id end describe "with a timeout" do - it "should raise an error if the code blocks for longer than the timeout" do @exception = nil @first_thread_has_started = false @@ -126,7 +125,8 @@ def mutex_id end end wait_for_threads_to_finish - assert @exception.is_a?(MegaMutex::TimeoutError), "Expected TimeoutError to be raised, but wasn't" + + @exception.should be_kind_of(MegaMutex::TimeoutError), "Expected TimeoutError to be raised, but wasn't" end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7044eff..a225ca6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,4 @@ -require File.expand_path(File.dirname(__FILE__) + '/../lib/mega_mutex') -require 'test/unit/assertions' +require_relative '../lib/mega_mutex' # Logging::Logger[:root].add_appenders(Logging::Appenders.stdout) @@ -28,5 +27,4 @@ def wait_for_threads_to_finish Spec::Runner.configure do |config| config.extend ThreadHelper config.include ThreadExampleHelper - config.include Test::Unit::Assertions end From b1c7afb43ad52c3dc99be6a508e751f7cd7ed643 Mon Sep 17 00:00:00 2001 From: Christoph Lupprich Date: Wed, 9 Feb 2022 11:19:37 +0100 Subject: [PATCH 3/3] Fix race condition when setting mutex key --- lib/mega_mutex/distributed_mutex.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mega_mutex/distributed_mutex.rb b/lib/mega_mutex/distributed_mutex.rb index 2300baa..6e194be 100644 --- a/lib/mega_mutex/distributed_mutex.rb +++ b/lib/mega_mutex/distributed_mutex.rb @@ -74,9 +74,9 @@ def locked_by_me? end def set_current_lock(new_lock) - cache.set(@key, my_lock_id) - # expire redis key after 1 hour - cache.expire(@key, 3600) + # nx: only set key if it doesn't exist + # px: expire redis key after 1 hour (in milliseconds) + cache.set(@key, my_lock_id, nx: true, px: 3_600_000) end def my_lock_id