2014-12-30 15:54:58 -05:00
|
|
|
require_relative '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
|
2015-10-08 12:37:37 -04:00
|
|
|
boss.expect(:options, {:queues => ['default'] }, [])
|
|
|
|
boss.expect(:options, {:queues => ['default'] }, [])
|
2012-02-10 00:46:44 -05:00
|
|
|
processor = Sidekiq::Processor.new(boss)
|
2015-10-06 15:43:01 -04:00
|
|
|
boss.expect(:processor_done, nil, [processor])
|
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
|
|
|
|
|
2014-10-28 13:59:45 -04:00
|
|
|
it 'correctly prepends middleware' do
|
|
|
|
chain = Sidekiq::Middleware::Chain.new
|
|
|
|
chain_entries = chain.entries
|
|
|
|
chain.add CustomMiddleware
|
|
|
|
chain.prepend YetAnotherCustomMiddleware
|
|
|
|
assert_equal YetAnotherCustomMiddleware, chain_entries.first.klass
|
|
|
|
assert_equal CustomMiddleware, chain_entries.last.klass
|
|
|
|
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'
|
2014-03-12 16:07:29 -04:00
|
|
|
I18n.enforce_available_locales = false
|
2013-03-03 19:10:50 -05:00
|
|
|
require 'sidekiq/middleware/i18n'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves and restores locale' do
|
|
|
|
I18n.locale = 'fr'
|
|
|
|
msg = {}
|
|
|
|
mw = Sidekiq::Middleware::I18n::Client.new
|
2014-03-26 00:38:17 -04:00
|
|
|
mw.call(nil, msg, nil, nil) { }
|
2013-03-03 19:10:50 -05:00
|
|
|
assert_equal :fr, msg['locale']
|
|
|
|
|
|
|
|
msg['locale'] = 'jp'
|
2014-01-01 17:32:09 -05:00
|
|
|
I18n.locale = I18n.default_locale
|
2013-03-03 19:10:50 -05:00
|
|
|
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
|
2014-01-01 17:32:09 -05:00
|
|
|
|
|
|
|
it 'supports I18n.enforce_available_locales = true' do
|
|
|
|
I18n.enforce_available_locales = true
|
2014-01-01 17:37:28 -05:00
|
|
|
I18n.available_locales = [:en, :jp]
|
2014-01-01 17:32:09 -05:00
|
|
|
|
|
|
|
msg = { 'locale' => 'jp' }
|
|
|
|
mw = Sidekiq::Middleware::I18n::Server.new
|
|
|
|
mw.call(nil, msg, nil) do
|
|
|
|
assert_equal :jp, I18n.locale
|
|
|
|
end
|
|
|
|
|
2014-03-12 16:07:29 -04:00
|
|
|
I18n.enforce_available_locales = false
|
2014-01-01 17:32:09 -05:00
|
|
|
I18n.available_locales = nil
|
|
|
|
end
|
2013-03-03 19:10:50 -05:00
|
|
|
end
|
2012-02-09 23:32:59 -05:00
|
|
|
end
|