Permalink
Browse files

Added tests.

  • Loading branch information...
1 parent c541095 commit 512ba28a077fdeb6b366624e1d741f108bd01bea @dv committed Aug 5, 2011
Showing with 71 additions and 0 deletions.
  1. +53 −0 spec/redis_spec.rb
  2. +18 −0 spec/spec_helper.rb
View
@@ -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
View
@@ -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.