Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add full multithreaded integration test for manager
- Loading branch information
Showing
6 changed files
with
100 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'helper' | ||
require 'sidekiq' | ||
require 'sidekiq/manager' | ||
require 'timed_queue' | ||
|
||
class TestManager < MiniTest::Unit::TestCase | ||
describe 'with redis' do | ||
before do | ||
Sidekiq::Client.redis = @redis = Redis.connect(:url => 'redis://localhost/sidekiq_test') | ||
@redis.flushdb | ||
$processed = 0 | ||
end | ||
|
||
class IntegrationWorker | ||
include Sidekiq::Worker | ||
|
||
def perform(a, b) | ||
$processed += 1 | ||
a + b | ||
end | ||
end | ||
|
||
it 'processes messages' do | ||
Sidekiq::Client.push(:foo, 'class' => IntegrationWorker, 'args' => [1, 2]) | ||
Sidekiq::Client.push(:foo, 'class' => IntegrationWorker, 'args' => [1, 2]) | ||
|
||
q = TimedQueue.new | ||
mgr = Sidekiq::Manager.new("redis://localhost/sidekiq_test", :queues => [:foo]) | ||
mgr.when_done do |_| | ||
q << 'done' if $processed == 2 | ||
end | ||
mgr.start! | ||
result = q.timed_pop | ||
assert_equal 'done', result | ||
mgr.stop | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'thread' | ||
require 'timeout' | ||
|
||
class TimedQueue | ||
def initialize | ||
@que = [] | ||
@mutex = Mutex.new | ||
@resource = ConditionVariable.new | ||
end | ||
|
||
def push(obj) | ||
@mutex.synchronize do | ||
@que.push obj | ||
@resource.broadcast | ||
end | ||
end | ||
alias_method :<<, :push | ||
|
||
def timed_pop(timeout=0.5) | ||
deadline = Time.now + timeout | ||
@mutex.synchronize do | ||
loop do | ||
return @que.shift unless @que.empty? | ||
to_wait = deadline - Time.now | ||
raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0 | ||
@resource.wait(@mutex, to_wait) | ||
end | ||
end | ||
end | ||
|
||
def empty? | ||
@que.empty? | ||
end | ||
|
||
def clear | ||
@que.clear | ||
end | ||
|
||
def length | ||
@que.length | ||
end | ||
end |