2012-02-05 00:45:34 -05:00
|
|
|
require 'helper'
|
2012-02-08 20:04:02 -05:00
|
|
|
require 'sidekiq/middleware/chain'
|
2012-02-05 00:45:34 -05:00
|
|
|
require 'sidekiq/processor'
|
|
|
|
|
2013-09-22 17:38:33 -04:00
|
|
|
class TestMiddleware < Sidekiq::Test
|
2012-02-05 00:45:34 -05:00
|
|
|
describe 'middleware chain' do
|
2012-02-11 16:14:03 -05:00
|
|
|
before do
|
2012-03-17 20:59:18 -04:00
|
|
|
$errors = []
|
2012-03-14 00:19:46 -04:00
|
|
|
Sidekiq.redis = REDIS
|
2012-02-11 16:14:03 -05:00
|
|
|
end
|
2012-02-05 00:45:34 -05:00
|
|
|
|
|
|
|
class CustomMiddleware
|
|
|
|
def initialize(name, recorder)
|
|
|
|
@name = name
|
|
|
|
@recorder = recorder
|
|
|
|
end
|
|
|
|
|
2012-02-11 02:16:12 -05:00
|
|
|
def call(*args)
|
2012-02-05 00:45:34 -05:00
|
|
|
@recorder << [@name, 'before']
|
|
|
|
yield
|
|
|
|
@recorder << [@name, 'after']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports custom middleware' do
|
2012-02-08 20:04:02 -05:00
|
|
|
chain = Sidekiq::Middleware::Chain.new
|
2012-02-18 15:12:05 -05:00
|
|
|
chain.add CustomMiddleware, 1, []
|
2012-02-08 20:04:02 -05:00
|
|
|
|
2012-02-09 23:32:59 -05:00
|
|
|
assert_equal CustomMiddleware, chain.entries.last.klass
|
2012-02-05 00:45:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class CustomWorker
|
2012-06-21 23:51:47 -04:00
|
|
|
$recorder = []
|
2012-02-11 02:16:12 -05:00
|
|
|
include Sidekiq::Worker
|
2012-02-05 00:45:34 -05:00
|
|
|
def perform(recorder)
|
2012-06-21 23:51:47 -04:00
|
|
|
$recorder << ['work_performed']
|
2012-02-05 00:45:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-08 20:28:12 -05:00
|
|
|
class NonYieldingMiddleware
|
|
|
|
def call(*args)
|
|
|
|
end
|
|
|
|
end
|
2013-01-06 00:17:08 -05:00
|
|
|
|
2012-12-19 00:42:23 -05:00
|
|
|
class AnotherCustomMiddleware
|
|
|
|
def initialize(name, recorder)
|
|
|
|
@name = name
|
|
|
|
@recorder = recorder
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(*args)
|
|
|
|
@recorder << [@name, 'before']
|
|
|
|
yield
|
|
|
|
@recorder << [@name, 'after']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class YetAnotherCustomMiddleware
|
|
|
|
def initialize(name, recorder)
|
|
|
|
@name = name
|
|
|
|
@recorder = recorder
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(*args)
|
|
|
|
@recorder << [@name, 'before']
|
|
|
|
yield
|
|
|
|
@recorder << [@name, 'after']
|
|
|
|
end
|
|
|
|
end
|
2012-02-08 20:28:12 -05:00
|
|
|
|
2012-02-05 00:45:34 -05:00
|
|
|
it 'executes middleware in the proper order' do
|
2012-06-21 23:51:47 -04:00
|
|
|
msg = Sidekiq.dump_json({ 'class' => CustomWorker.to_s, 'args' => [$recorder] })
|
2012-02-05 00:45:34 -05:00
|
|
|
|
2012-02-18 15:12:05 -05:00
|
|
|
Sidekiq.server_middleware do |chain|
|
2013-08-01 14:54:30 -04:00
|
|
|
# should only add once, second should replace the first
|
2012-06-21 23:51:47 -04:00
|
|
|
2.times { |i| chain.add CustomMiddleware, i.to_s, $recorder }
|
2012-12-19 11:40:36 -05:00
|
|
|
chain.insert_before CustomMiddleware, AnotherCustomMiddleware, '2', $recorder
|
|
|
|
chain.insert_after AnotherCustomMiddleware, YetAnotherCustomMiddleware, '3', $recorder
|
2012-02-05 00:45:34 -05:00
|
|
|
end
|
|
|
|
|
2013-05-12 17:25:30 -04:00
|
|
|
boss = Minitest::Mock.new
|
2012-02-10 00:46:44 -05:00
|
|
|
processor = Sidekiq::Processor.new(boss)
|
2013-05-12 17:25:30 -04:00
|
|
|
actor = Minitest::Mock.new
|
2012-11-03 22:56:06 -04:00
|
|
|
actor.expect(:processor_done, nil, [processor])
|
2013-06-11 01:20:15 -04:00
|
|
|
actor.expect(:real_thread, nil, [nil, Celluloid::Thread])
|
|
|
|
boss.expect(:async, actor, [])
|
2012-11-03 22:56:06 -04:00
|
|
|
boss.expect(:async, actor, [])
|
2013-01-06 00:17:08 -05:00
|
|
|
processor.process(Sidekiq::BasicFetch::UnitOfWork.new('queue:default', msg))
|
2013-08-01 14:54:30 -04:00
|
|
|
assert_equal %w(2 before 3 before 1 before work_performed 1 after 3 after 2 after), $recorder.flatten
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly replaces middleware when using middleware with options in the initializer' do
|
|
|
|
chain = Sidekiq::Middleware::Chain.new
|
|
|
|
chain.add Sidekiq::Middleware::Server::RetryJobs
|
|
|
|
chain.add Sidekiq::Middleware::Server::RetryJobs, {:max_retries => 5}
|
|
|
|
assert_equal 1, chain.count
|
2012-02-05 00:45:34 -05:00
|
|
|
end
|
|
|
|
|
2012-02-08 20:28:12 -05:00
|
|
|
it 'allows middleware to abruptly stop processing rest of chain' do
|
|
|
|
recorder = []
|
|
|
|
chain = Sidekiq::Middleware::Chain.new
|
2012-02-18 15:12:05 -05:00
|
|
|
chain.add NonYieldingMiddleware
|
|
|
|
chain.add CustomMiddleware, 1, recorder
|
2012-02-05 00:45:34 -05:00
|
|
|
|
2012-02-08 20:28:12 -05:00
|
|
|
final_action = nil
|
|
|
|
chain.invoke { final_action = true }
|
2012-02-09 23:32:59 -05:00
|
|
|
assert_equal nil, final_action
|
|
|
|
assert_equal [], recorder
|
2012-02-08 20:28:12 -05:00
|
|
|
end
|
|
|
|
end
|
2013-03-03 19:10:50 -05:00
|
|
|
|
|
|
|
describe 'i18n' do
|
|
|
|
before do
|
|
|
|
require 'i18n'
|
|
|
|
require 'sidekiq/middleware/i18n'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves and restores locale' do
|
|
|
|
I18n.locale = 'fr'
|
|
|
|
msg = {}
|
|
|
|
mw = Sidekiq::Middleware::I18n::Client.new
|
|
|
|
mw.call(nil, msg, nil) { }
|
|
|
|
assert_equal :fr, msg['locale']
|
|
|
|
|
|
|
|
msg['locale'] = 'jp'
|
|
|
|
I18n.locale = nil
|
|
|
|
assert_equal :en, I18n.locale
|
|
|
|
mw = Sidekiq::Middleware::I18n::Server.new
|
|
|
|
mw.call(nil, msg, nil) do
|
|
|
|
assert_equal :jp, I18n.locale
|
|
|
|
end
|
|
|
|
assert_equal :en, I18n.locale
|
|
|
|
end
|
|
|
|
end
|
2012-02-09 23:32:59 -05:00
|
|
|
end
|