Skip to content
Browse files

Add tests for GirlFriday module methods, fixes GH-32

  • Loading branch information...
1 parent 0c2cc10 commit ff708798dbf2895ec1db7b47eb03ca8c6b626755 @mperham committed Sep 22, 2011
Showing with 273 additions and 235 deletions.
  1. +1 −1 lib/girl_friday.rb
  2. +1 −0 test/helper.rb
  3. +21 −234 test/test_girl_friday.rb
  4. +250 −0 test/test_girl_friday_queue.rb
View
2 lib/girl_friday.rb
@@ -40,7 +40,7 @@ def self.queues
def self.status
queues.inject({}) do |memo, queue|
begin
- memo.merge(queue.__getobj__.status)
+ memo = memo.merge(queue.__getobj__.status)
rescue WeakRef::RefError
end
memo
View
1 test/helper.rb
@@ -18,6 +18,7 @@
# end
require 'rubygems'
+require 'minitest/spec'
require 'minitest/autorun'
require 'connection_pool'
require 'girl_friday'
View
255 test/test_girl_friday.rb
@@ -2,247 +2,34 @@
class TestGirlFriday < MiniTest::Unit::TestCase
- class TestErrorHandler
- include MiniTest::Assertions
- end
-
- def test_should_process_messages
- async_test do |cb|
- queue = GirlFriday::WorkQueue.new('process') do |msg|
- assert_equal 'foo', msg[:text]
- queue.shutdown do
- cb.call
- end
- end
- queue.push(:text => 'foo')
- end
- end
-
- def test_should_handle_worker_error
- async_test do |cb|
- queue = nil
- TestErrorHandler.send(:define_method, :handle) do |ex|
- assert_equal 'oops', ex.message
- assert_equal 'RuntimeError', ex.class.name
- queue.shutdown do
- cb.call
- end
- end
-
- queue = GirlFriday::WorkQueue.new('error', :error_handler => TestErrorHandler) do |msg|
- raise 'oops'
- end
- queue.push(:text => 'foo')
- end
- end
-
- def test_should_call_callback_when_complete
- async_test do |cb|
- queue = GirlFriday::WorkQueue.new('callback', :size => 1) do |msg|
- assert_equal 'foo', msg[:text]
- 'camel'
- end
- queue.push(:text => 'foo') do |result|
- assert_equal 'camel', result
- queue.shutdown do
- cb.call
- end
- end
- end
- end
-
- def test_should_provide_status
- mutex = Mutex.new
- total = 200
- count = 0
- incr = Proc.new do
- mutex.synchronize do
- count += 1
- end
- end
-
- actual = nil
- async_test do |cb|
- queue = GirlFriday::WorkQueue.new('status', :size => 3) do |msg|
- mycount = incr.call
- actual = queue.status if mycount == 100
- queue.shutdown do
- cb.call
- end if mycount == total
- end
- total.times do |idx|
- queue.push(:text => 'foo')
- end
- end
-
- refute_nil actual
- refute_nil actual['status']
- metrics = actual['status']
- assert metrics[:total_queued] > 0
- assert metrics[:total_queued] <= total
- assert_equal 3, metrics[:pool_size]
- assert_equal 3, metrics[:busy]
- assert_equal 0, metrics[:ready]
- assert(metrics[:backlog] > 0)
- assert(metrics[:total_processed] > 0)
- end
-
- def test_should_persist_with_redis
- begin
- require 'redis'
- redis = Redis.new
- redis.flushdb
- rescue LoadError
- return puts "Skipping redis test, 'redis' gem not found: #{$!.message}"
- rescue Errno::ECONNREFUSED
- return puts 'Skipping redis test, not running locally'
- end
-
- mutex = Mutex.new
- total = 100
- count = 0
- incr = Proc.new do
- mutex.synchronize do
- count += 1
- end
- end
-
- async_test(1.0) do |cb|
- queue = GirlFriday::WorkQueue.new('redis', :size => 2, :store => GirlFriday::Store::Redis) do |msg|
- incr.call
- queue.shutdown do
- cb.call
- end if count == total
- end
- total.times do
- queue.push(:text => 'foo')
- end
- end
- end
-
- def test_should_persist_with_redis_instance
- begin
- require 'redis'
- redis = Redis.new
- redis.flushdb
- rescue LoadError
- return puts "Skipping redis test, 'redis' gem not found: #{$!.message}"
- rescue Errno::ECONNREFUSED
- return puts 'Skipping redis test, not running locally'
- end
-
- mutex = Mutex.new
- total = 100
- count = 0
- incr = Proc.new do
- mutex.synchronize do
- count += 1
- end
- end
-
- async_test(1.0) do |cb|
- queue = GirlFriday::WorkQueue.new('redis-instance', :size => 2, :store => GirlFriday::Store::Redis, :store_config => [{ :redis => redis }]) do |msg|
- incr.call
- queue.shutdown do
- cb.call
- end if count == total
- end
- total.times do
- queue.push(:text => 'foo')
- end
- end
- end
-
- def test_should_persist_with_redis_connection_pool
- begin
- require 'redis'
- require 'connection_pool'
- redis = ConnectionPool.new(:size => 5, :timeout => 5){ Redis.new }
- redis.flushdb
- rescue LoadError
- return puts "Skipping redis test, 'redis' gem not found: #{$!.message}"
- rescue Errno::ECONNREFUSED
- return puts 'Skipping redis test, not running locally'
- end
-
- mutex = Mutex.new
- total = 100
- count = 0
- incr = Proc.new do
- mutex.synchronize do
- count += 1
- end
- end
-
- async_test(1.0) do |cb|
- queue = GirlFriday::WorkQueue.new('redis-pool', :size => 2, :store => GirlFriday::Store::Redis, :store_config => [{ :redis => redis }]) do |msg|
- incr.call
- queue.shutdown do
- cb.call
- end if count == total
- end
- total.times do
- queue.push(:text => 'foo')
- end
- end
- end
-
- def test_should_allow_graceful_shutdown
- mutex = Mutex.new
- total = 100
- count = 0
- incr = Proc.new do
- mutex.synchronize do
- count += 1
- end
+ describe 'GirlFriday' do
+ after do
+ GirlFriday.shutdown!
end
- async_test do |cb|
- queue = GirlFriday::WorkQueue.new('shutdown', :size => 2) do |msg|
- incr.call
- cb.call if count == total
- end
- total.times do
- queue.push(:text => 'foo')
+ describe '.status' do
+ before do
+ q1 = GirlFriday::Queue.new(:q1) do; end
+ q2 = GirlFriday::Queue.new(:q2) do; end
end
-
- assert_equal 1, GirlFriday.queues.size
- count = GirlFriday.shutdown!
- assert_equal 0, count
- cb.call
- end
- end
-
- def test_should_create_workers_lazily
- async_test do |cb|
- queue = GirlFriday::Queue.new('lazy', :size => 2) do |msg|
- assert_equal 1, queue.instance_variable_get(:@ready_workers).size
- queue.shutdown do
- cb.call
- end
+ it 'provides a status structure for each live queue' do
+ hash = GirlFriday.status
+ assert_kind_of Hash, hash
+ assert_equal 2, GirlFriday.queues.size
+ assert_equal 2, hash.size
end
- refute queue.instance_variable_defined?(:@ready_workers)
- # don't instantiate the worker threads until we actually put
- # work onto the queue.
- queue << 'empty msg'
end
- end
- def test_stubbing_girl_friday_with_flexmock
- expected = Thread.current.to_s
- actual = nil
- processor = Proc.new do |msg|
- actual = Thread.current.to_s
- end
- async_test do |cb|
- queue = GirlFriday::Queue.new('flexmock', :size => 2, &processor)
- flexmock(queue).should_receive(:push).zero_or_more_times.and_return do |msg|
- processor.call(msg)
+ describe '.shutdown!' do
+ before do
+ q1 = GirlFriday::Queue.new(:q1) do; end
+ q2 = GirlFriday::Queue.new(:q2) do; end
end
- queue.push 'hello world!'
- assert_equal expected, actual
- queue.shutdown do
- cb.call
+ it 'provides a status structure for each live queue' do
+ a = Time.now
+ assert_equal 0, GirlFriday.shutdown!
+ assert_in_delta 0, Time.now - a, 0.1
+ assert_equal 0, GirlFriday.queues.size
end
end
end
View
250 test/test_girl_friday_queue.rb
@@ -0,0 +1,250 @@
+require 'helper'
+
+class TestGirlFriday < MiniTest::Unit::TestCase
+
+ class TestErrorHandler
+ include MiniTest::Assertions
+ end
+
+ def test_should_process_messages
+ async_test do |cb|
+ queue = GirlFriday::WorkQueue.new('process') do |msg|
+ assert_equal 'foo', msg[:text]
+ queue.shutdown do
+ cb.call
+ end
+ end
+ queue.push(:text => 'foo')
+ end
+ end
+
+ def test_should_handle_worker_error
+ async_test do |cb|
+ queue = nil
+ TestErrorHandler.send(:define_method, :handle) do |ex|
+ assert_equal 'oops', ex.message
+ assert_equal 'RuntimeError', ex.class.name
+ queue.shutdown do
+ cb.call
+ end
+ end
+
+ queue = GirlFriday::WorkQueue.new('error', :error_handler => TestErrorHandler) do |msg|
+ raise 'oops'
+ end
+ queue.push(:text => 'foo')
+ end
+ end
+
+ def test_should_call_callback_when_complete
+ async_test do |cb|
+ queue = GirlFriday::WorkQueue.new('callback', :size => 1) do |msg|
+ assert_equal 'foo', msg[:text]
+ 'camel'
+ end
+ queue.push(:text => 'foo') do |result|
+ assert_equal 'camel', result
+ queue.shutdown do
+ cb.call
+ end
+ end
+ end
+ end
+
+ def test_should_provide_status
+ mutex = Mutex.new
+ total = 200
+ count = 0
+ incr = Proc.new do
+ mutex.synchronize do
+ count += 1
+ end
+ end
+
+ actual = nil
+ async_test do |cb|
+ queue = GirlFriday::WorkQueue.new('status', :size => 3) do |msg|
+ mycount = incr.call
+ actual = queue.status if mycount == 100
+ queue.shutdown do
+ cb.call
+ end if mycount == total
+ end
+ total.times do |idx|
+ queue.push(:text => 'foo')
+ end
+ end
+
+ refute_nil actual
+ refute_nil actual['status']
+ metrics = actual['status']
+ assert metrics[:total_queued] > 0
+ assert metrics[:total_queued] <= total
+ assert_equal 3, metrics[:pool_size]
+ assert_equal 3, metrics[:busy]
+ assert_equal 0, metrics[:ready]
+ assert(metrics[:backlog] > 0)
+ assert(metrics[:total_processed] > 0)
+ end
+
+ def test_should_persist_with_redis
+ begin
+ require 'redis'
+ redis = Redis.new
+ redis.flushdb
+ rescue LoadError
+ return puts "Skipping redis test, 'redis' gem not found: #{$!.message}"
+ rescue Errno::ECONNREFUSED
+ return puts 'Skipping redis test, not running locally'
+ end
+
+ mutex = Mutex.new
+ total = 100
+ count = 0
+ incr = Proc.new do
+ mutex.synchronize do
+ count += 1
+ end
+ end
+
+ async_test(1.0) do |cb|
+ queue = GirlFriday::WorkQueue.new('redis', :size => 2, :store => GirlFriday::Store::Redis) do |msg|
+ incr.call
+ queue.shutdown do
+ cb.call
+ end if count == total
+ end
+ total.times do
+ queue.push(:text => 'foo')
+ end
+ end
+ end
+
+ def test_should_persist_with_redis_instance
+ begin
+ require 'redis'
+ redis = Redis.new
+ redis.flushdb
+ rescue LoadError
+ return puts "Skipping redis test, 'redis' gem not found: #{$!.message}"
+ rescue Errno::ECONNREFUSED
+ return puts 'Skipping redis test, not running locally'
+ end
+
+ mutex = Mutex.new
+ total = 100
+ count = 0
+ incr = Proc.new do
+ mutex.synchronize do
+ count += 1
+ end
+ end
+
+ async_test(1.0) do |cb|
+ queue = GirlFriday::WorkQueue.new('redis-instance', :size => 2, :store => GirlFriday::Store::Redis, :store_config => [{ :redis => redis }]) do |msg|
+ incr.call
+ queue.shutdown do
+ cb.call
+ end if count == total
+ end
+ total.times do
+ queue.push(:text => 'foo')
+ end
+ end
+ end
+
+ def test_should_persist_with_redis_connection_pool
+ begin
+ require 'redis'
+ require 'connection_pool'
+ redis = ConnectionPool.new(:size => 5, :timeout => 5){ Redis.new }
+ redis.flushdb
+ rescue LoadError
+ return puts "Skipping redis test, 'redis' gem not found: #{$!.message}"
+ rescue Errno::ECONNREFUSED
+ return puts 'Skipping redis test, not running locally'
+ end
+
+ mutex = Mutex.new
+ total = 100
+ count = 0
+ incr = Proc.new do
+ mutex.synchronize do
+ count += 1
+ end
+ end
+
+ async_test(1.0) do |cb|
+ queue = GirlFriday::WorkQueue.new('redis-pool', :size => 2, :store => GirlFriday::Store::Redis, :store_config => [{ :redis => redis }]) do |msg|
+ incr.call
+ queue.shutdown do
+ cb.call
+ end if count == total
+ end
+ total.times do
+ queue.push(:text => 'foo')
+ end
+ end
+ end
+
+ def test_should_allow_graceful_shutdown
+ mutex = Mutex.new
+ total = 100
+ count = 0
+ incr = Proc.new do
+ mutex.synchronize do
+ count += 1
+ end
+ end
+
+ async_test do |cb|
+ queue = GirlFriday::WorkQueue.new('shutdown', :size => 2) do |msg|
+ incr.call
+ cb.call if count == total
+ end
+ total.times do
+ queue.push(:text => 'foo')
+ end
+
+ assert_equal 1, GirlFriday.queues.size
+ count = GirlFriday.shutdown!
+ assert_equal 0, count
+ cb.call
+ end
+ end
+
+ def test_should_create_workers_lazily
+ async_test do |cb|
+ queue = GirlFriday::Queue.new('lazy', :size => 2) do |msg|
+ assert_equal 1, queue.instance_variable_get(:@ready_workers).size
+ queue.shutdown do
+ cb.call
+ end
+ end
+ refute queue.instance_variable_defined?(:@ready_workers)
+ # don't instantiate the worker threads until we actually put
+ # work onto the queue.
+ queue << 'empty msg'
+ end
+ end
+
+ def test_stubbing_girl_friday_with_flexmock
+ expected = Thread.current.to_s
+ actual = nil
+ processor = Proc.new do |msg|
+ actual = Thread.current.to_s
+ end
+ async_test do |cb|
+ queue = GirlFriday::Queue.new('flexmock', :size => 2, &processor)
+ flexmock(queue).should_receive(:push).zero_or_more_times.and_return do |msg|
+ processor.call(msg)
+ end
+ queue.push 'hello world!'
+ assert_equal expected, actual
+ queue.shutdown do
+ cb.call
+ end
+ end
+ end
+
+end

0 comments on commit ff70879

Please sign in to comment.
Something went wrong with that request. Please try again.