Permalink
Browse files

Use cutest for testing.

  • Loading branch information...
1 parent b5dcb53 commit 899fd13ec2ab9725e35849e49b3479a98ba71f8e @soveran committed Apr 12, 2011
Showing with 39 additions and 31 deletions.
  1. +2 −1 Rakefile
  2. +36 −29 test/ost_test.rb
  3. +1 −1 test/test_helper.rb
View
@@ -1,5 +1,6 @@
task :test do
- system "cd test && ruby ost_test.rb"
+ require "cutest"
+ Cutest.run(Dir["test/ost*"])
end
task :default => :test
View
@@ -1,63 +1,70 @@
-require File.join(File.dirname(__FILE__), "test_helper")
+require File.expand_path("test_helper", File.dirname(__FILE__))
-class TestOst < Test::Unit::TestCase
+scope do
def ost(&job)
thread = Thread.new do
+ Ost.redis = Redis.current
Ost[:events].each(&job)
end
- sleep 0.2
+ sleep 0.1
thread.kill
end
- setup do
- @redis = Redis.new
- @redis.flushdb
+ def enqueue(id)
+ Ost[:events].push(id)
+ end
- Ost.connect
- Ost[:events].push(1)
+ prepare do
+ Ost.redis.flushall
end
- teardown do
- Ost.redis.flushdb
+ setup do
+ Redis.new
end
- should "insert items in the queue" do
- assert_equal ["1"], @redis.lrange("ost:events", 0, -1)
+ test "insert items in the queue" do |redis|
+ enqueue(1)
+ assert_equal ["1"], redis.lrange("ost:events", 0, -1)
end
- should "process items from the queue" do
- @results = []
+ test "process items from the queue" do |redis|
+ enqueue(1)
+
+ results = []
ost do |item|
- @results << item
+ results << item
end
- assert_equal [], @redis.lrange("ost:events", 0, -1)
- assert_equal ["1"], @results
+ assert_equal [], redis.lrange("ost:events", 0, -1)
+ assert_equal ["1"], results
end
- should "add failures to a special list" do
+ test "add failures to special lists" do |redis|
+ enqueue(1)
+
ost do |item|
raise "Wrong answer"
end
- assert_equal 0, @redis.llen("ost:events")
- assert_equal 1, @redis.llen("ost:events:errors")
+ assert_equal 0, redis.llen("ost:events")
+ assert_equal 1, redis.llen("ost:events:errors")
- assert_match /ost:events:1 => #<RuntimeError: Wrong answer/, @redis.rpop("ost:events:errors")
+ assert redis.rpop("ost:events:errors").match(/ost:events:1 => #<RuntimeError: Wrong answer/)
end
- should "publish the error to a specific channel" do
- @results = []
+ test "publish the error to a specific channel" do |redis|
+ enqueue(1)
+ results = []
t1 = Thread.new do
- @redis.subscribe("ost:events:errors") do |on|
+ redis.subscribe("ost:events:errors") do |on|
on.message do |channel, message|
if message[/ost:events:1 => #<RuntimeError: Wrong answer/]
- @results << message
- @redis.unsubscribe
+ results << message
+ redis.unsubscribe
end
end
end
@@ -69,9 +76,9 @@ def ost(&job)
t1.kill
- assert_equal 0, @redis.llen("ost:events")
- assert_equal 1, @redis.llen("ost:events:errors")
+ assert_equal 0, redis.llen("ost:events")
+ assert_equal 1, redis.llen("ost:events:errors")
- assert_match /ost:events:1 => #<RuntimeError: Wrong answer/, @results.pop
+ assert results.pop.match(/ost:events:1 => #<RuntimeError: Wrong answer/)
end
end
View
@@ -1,3 +1,3 @@
require "rubygems"
-require "contest"
+require "cutest"
require File.join(File.dirname(__FILE__), "..", "lib", "ost")

0 comments on commit 899fd13

Please sign in to comment.