Sha256: b269a4d248654f048061f3c2da721685bb8119edf7ea115ce68b542cba220031

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

require 'spec_helper'

describe 'lock' do
  
  it "acquires the lock if it does not exist" do
    
    @redis.lock("test") { }
    
  end
  
  it "waits and acquire lock" do
    
    start_time = Time.now.to_i
    
    Thread.new {
      @redis.lock("test") { 
        Thread.current.join(1)
        sleep(5) 
      }
    }
    
    Thread.current.join(1)
    
    @redis.lock("test") { }
    
    (Time.now.to_i - start_time).should be > 4
    
  end
  
  it "waits and acquire expired lock" do
    
    start_time = Time.now.to_i
    
    Thread.new {
      @redis.send(:acquire_lock, "test", 5, 10).should == true
    }
    
    Thread.current.join(0.1)
    
    @redis.lock("test") { }
    
    (Time.now.to_i - start_time).should be > 4
    
  end
  
  it "fails to acquire lock" do
    
    Thread.new {
      @redis.lock("test") { 
        Thread.current.join(1)
        sleep(5) 
      }
    }
    
    Thread.current.join(1)
    lambda { @redis.lock("test", 10, 2) {  } }.should raise_error
    
  end
  
  it "correctly release the lock when error is raised" do
    
    begin
      @redis.lock("test") { 
        raise 'some error'
      }
    rescue
    end
    
    @redis.get(@redis.send(:ruby_redis_lock_key, "test")).should == nil
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby_redis_lock-0.1.0 spec/lock_spec.rb