2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2014-12-30 15:54:58 -05:00
|
|
|
require_relative 'helper'
|
2015-09-22 15:48:43 -04:00
|
|
|
|
2012-05-01 18:54:04 -04:00
|
|
|
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
|
2015-09-22 15:48:43 -04:00
|
|
|
require 'sidekiq/testing/inline'
|
2013-09-20 17:39:11 -04:00
|
|
|
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
|
2016-03-09 12:39:10 -05:00
|
|
|
assert Sidekiq::Client.enqueue(InlineWorkerWithTimeParam, Time.now.to_f)
|
2012-06-01 09:37:50 -04:00
|
|
|
end
|
2015-09-04 18:35:33 -04:00
|
|
|
|
2012-05-01 18:54:04 -04:00
|
|
|
end
|
|
|
|
end
|