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/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 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 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