Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dv committed Aug 5, 2011
1 parent c541095 commit 512ba28
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
53 changes: 53 additions & 0 deletions spec/redis_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require File.dirname(__FILE__) + '/spec_helper'

describe "redis" do
before(:all) do
# use database 15 for testing so we dont accidentally step on you real data
@redis = Redis.new :db => 15
@semaphore = Redis::Semaphore.new(:my_semaphore, @redis)
end

before(:each) do
@redis.flushdb
end

after(:each) do
@redis.flushdb
end

after(:all) do
@redis.quit
end

it "should be unlocked from the start" do
@semaphore.locked?.should == false
end

it "should lock and unlock" do
@semaphore.lock
@semaphore.locked?.should == true
@semaphore.unlock
@semaphore.locked?.should == false
end

it "should not lock twice as a mutex" do
@semaphore.lock
@semaphore.lock(1).should == false
end

it "should not lock three times when only two available" do
multisem = Redis::Semaphore.new(:my_semaphore2, 2, @redis)
multisem.lock.should == true
multisem.lock(1).should == true
multisem.lock(1).should == false
end

it "should execute the given code block" do
code_executed = false
@semaphore.lock do
code_executed = true
end
code_executed.should == true
end

end
18 changes: 18 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rubygems'
require 'bundler'
Bundler.setup(:default, :test)
Bundler.require(:default, :test)

require 'rspec'
require 'redis'
require 'logger'

$TESTING=true
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'redis/semaphore'

RSpec::Matchers.define :have_key do |expected|
match do |redis|
redis.exists(expected)
end
end

0 comments on commit 512ba28

Please sign in to comment.