2014-12-30 15:54:58 -05:00
|
|
|
require_relative 'helper'
|
2012-05-01 18:54:04 -04:00
|
|
|
require 'sidekiq'
|
|
|
|
require 'sidekiq/worker'
|
|
|
|
require 'active_record'
|
|
|
|
require 'action_mailer'
|
|
|
|
require 'sidekiq/rails'
|
|
|
|
require 'sidekiq/extensions/action_mailer'
|
|
|
|
require 'sidekiq/extensions/active_record'
|
|
|
|
|
|
|
|
Sidekiq.hook_rails!
|
|
|
|
|
2013-09-22 17:38:33 -04:00
|
|
|
class TestInline < Sidekiq::Test
|
2012-05-01 18:54:04 -04:00
|
|
|
describe 'sidekiq inline testing' do
|
|
|
|
class InlineError < RuntimeError; end
|
2012-06-01 09:37:50 -04:00
|
|
|
class ParameterIsNotString < RuntimeError; end
|
2012-05-01 18:54:04 -04:00
|
|
|
|
|
|
|
class InlineWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(pass)
|
2014-01-30 12:22:07 -05:00
|
|
|
raise ArgumentError, "no jid" unless jid
|
2012-05-01 18:54:04 -04:00
|
|
|
raise InlineError unless pass
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-01 09:37:50 -04:00
|
|
|
class InlineWorkerWithTimeParam
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(time)
|
2012-08-19 21:19:59 -04:00
|
|
|
raise ParameterIsNotString unless time.is_a?(String) || time.is_a?(Numeric)
|
2012-06-01 09:37:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-01 18:54:04 -04:00
|
|
|
class InlineFooMailer < ActionMailer::Base
|
|
|
|
def bar(str)
|
|
|
|
raise InlineError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class InlineFooModel < ActiveRecord::Base
|
|
|
|
def self.bar(str)
|
|
|
|
raise InlineError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2013-09-20 17:39:11 -04:00
|
|
|
require 'sidekiq/testing/inline.rb'
|
|
|
|
Sidekiq::Testing.inline!
|
2012-05-01 18:54:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2013-09-20 17:39:11 -04:00
|
|
|
Sidekiq::Testing.disable!
|
2012-05-01 18:54:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'stubs the async call when in testing mode' do
|
|
|
|
assert InlineWorker.perform_async(true)
|
|
|
|
|
|
|
|
assert_raises InlineError do
|
|
|
|
InlineWorker.perform_async(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stubs the delay call on mailers' do
|
|
|
|
assert_raises InlineError do
|
|
|
|
InlineFooMailer.delay.bar('three')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stubs the delay call on models' do
|
|
|
|
assert_raises InlineError do
|
|
|
|
InlineFooModel.delay.bar('three')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stubs the enqueue call when in testing mode' do
|
|
|
|
assert Sidekiq::Client.enqueue(InlineWorker, true)
|
|
|
|
|
|
|
|
assert_raises InlineError do
|
|
|
|
Sidekiq::Client.enqueue(InlineWorker, false)
|
|
|
|
end
|
|
|
|
end
|
2012-06-01 09:37:50 -04:00
|
|
|
|
2012-11-19 20:34:46 -05:00
|
|
|
it 'stubs the push_bulk call when in testing mode' do
|
2013-03-24 20:42:43 -04:00
|
|
|
assert Sidekiq::Client.push_bulk({'class' => InlineWorker, 'args' => [[true], [true]]})
|
2012-11-19 20:34:46 -05:00
|
|
|
|
|
|
|
assert_raises InlineError do
|
2013-03-24 20:42:43 -04:00
|
|
|
Sidekiq::Client.push_bulk({'class' => InlineWorker, 'args' => [[true], [false]]})
|
2012-11-19 20:34:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-01 09:37:50 -04:00
|
|
|
it 'should relay parameters through json' do
|
|
|
|
assert Sidekiq::Client.enqueue(InlineWorkerWithTimeParam, Time.now)
|
|
|
|
end
|
2015-09-04 18:35:33 -04:00
|
|
|
|
|
|
|
describe 'with middleware' do
|
|
|
|
class AttributeWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
class_attribute :count
|
|
|
|
self.count = 0
|
|
|
|
attr_accessor :foo
|
|
|
|
|
|
|
|
def perform
|
|
|
|
self.class.count += 1 if foo == :bar
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AttributeMiddleware
|
|
|
|
def call(worker, msg, queue)
|
|
|
|
worker.foo = :bar if worker.respond_to?(:foo=)
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
Sidekiq::Testing.server_middleware do |chain|
|
|
|
|
chain.add AttributeMiddleware
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Sidekiq::Testing.server_middleware do |chain|
|
|
|
|
chain.clear
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'wraps the inlined worker with middleware' do
|
|
|
|
AttributeWorker.perform_async
|
|
|
|
assert_equal 1, AttributeWorker.count
|
|
|
|
end
|
|
|
|
end
|
2012-05-01 18:54:04 -04:00
|
|
|
end
|
|
|
|
end
|