2015-12-31 15:33:35 -08:00
|
|
|
# frozen_string_literal: true
|
2015-09-22 12:48:43 -07:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
require_relative "helper"
|
|
|
|
|
|
|
|
describe "Sidekiq::Testing.inline" do
|
2019-02-28 12:43:50 -08:00
|
|
|
class InlineError < RuntimeError; end
|
2022-03-03 12:50:03 -08:00
|
|
|
|
2019-02-28 12:43:50 -08:00
|
|
|
class ParameterIsNotString < RuntimeError; end
|
|
|
|
|
|
|
|
class InlineWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(pass)
|
|
|
|
raise ArgumentError, "no jid" unless jid
|
|
|
|
raise InlineError unless pass
|
2012-05-01 15:54:04 -07:00
|
|
|
end
|
2019-02-28 12:43:50 -08:00
|
|
|
end
|
2012-05-01 15:54:04 -07:00
|
|
|
|
2019-02-28 12:43:50 -08:00
|
|
|
class InlineWorkerWithTimeParam
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(time)
|
|
|
|
raise ParameterIsNotString unless time.is_a?(String) || time.is_a?(Numeric)
|
2012-06-01 16:37:50 +03:00
|
|
|
end
|
2019-02-28 12:43:50 -08:00
|
|
|
end
|
2012-06-01 16:37:50 +03:00
|
|
|
|
2019-02-28 12:43:50 -08:00
|
|
|
before do
|
2022-03-03 12:50:03 -08:00
|
|
|
require "sidekiq/testing/inline"
|
2019-02-28 12:43:50 -08:00
|
|
|
Sidekiq::Testing.inline!
|
|
|
|
end
|
2012-05-01 15:54:04 -07:00
|
|
|
|
2019-02-28 12:43:50 -08:00
|
|
|
after do
|
|
|
|
Sidekiq::Testing.disable!
|
|
|
|
end
|
2012-05-01 15:54:04 -07:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
it "stubs the async call when in testing mode" do
|
2019-02-28 12:43:50 -08:00
|
|
|
assert InlineWorker.perform_async(true)
|
2012-05-01 15:54:04 -07:00
|
|
|
|
2019-02-28 12:43:50 -08:00
|
|
|
assert_raises InlineError do
|
|
|
|
InlineWorker.perform_async(false)
|
2012-05-01 15:54:04 -07:00
|
|
|
end
|
2019-02-28 12:43:50 -08:00
|
|
|
end
|
2012-05-01 15:54:04 -07:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
describe "delay" do
|
|
|
|
require "action_mailer"
|
2019-02-28 12:43:50 -08:00
|
|
|
class InlineFooMailer < ActionMailer::Base
|
|
|
|
def bar(str)
|
|
|
|
raise InlineError
|
2017-01-04 10:30:42 -08:00
|
|
|
end
|
2019-02-28 12:43:50 -08:00
|
|
|
end
|
2017-01-04 10:30:42 -08:00
|
|
|
|
2019-02-28 12:43:50 -08:00
|
|
|
class InlineFooModel
|
|
|
|
def self.bar(str)
|
|
|
|
raise InlineError
|
2017-01-04 10:30:42 -08:00
|
|
|
end
|
2019-02-28 12:43:50 -08:00
|
|
|
end
|
2017-01-04 10:30:42 -08:00
|
|
|
|
2019-02-28 12:43:50 -08:00
|
|
|
before do
|
|
|
|
Sidekiq::Extensions.enable_delay!
|
|
|
|
end
|
2017-01-04 10:30:42 -08:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
it "stubs the delay call on mailers" do
|
2019-02-28 12:43:50 -08:00
|
|
|
assert_raises InlineError do
|
2022-03-03 12:50:03 -08:00
|
|
|
InlineFooMailer.delay.bar("three")
|
2012-05-01 15:54:04 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
it "stubs the delay call on models" do
|
2012-05-01 15:54:04 -07:00
|
|
|
assert_raises InlineError do
|
2022-03-03 12:50:03 -08:00
|
|
|
InlineFooModel.delay.bar("three")
|
2012-05-01 15:54:04 -07:00
|
|
|
end
|
|
|
|
end
|
2019-02-28 12:43:50 -08:00
|
|
|
end
|
2012-06-01 16:37:50 +03:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
it "stubs the enqueue call when in testing mode" do
|
2019-02-28 12:43:50 -08:00
|
|
|
assert Sidekiq::Client.enqueue(InlineWorker, true)
|
2012-11-19 17:34:46 -08:00
|
|
|
|
2019-02-28 12:43:50 -08:00
|
|
|
assert_raises InlineError do
|
|
|
|
Sidekiq::Client.enqueue(InlineWorker, false)
|
2012-11-19 17:34:46 -08:00
|
|
|
end
|
2019-02-28 12:43:50 -08:00
|
|
|
end
|
2012-11-19 17:34:46 -08:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
it "stubs the push_bulk call when in testing mode" do
|
|
|
|
assert Sidekiq::Client.push_bulk({"class" => InlineWorker, "args" => [[true], [true]]})
|
2019-02-28 12:43:50 -08:00
|
|
|
|
|
|
|
assert_raises InlineError do
|
2022-03-03 12:50:03 -08:00
|
|
|
Sidekiq::Client.push_bulk({"class" => InlineWorker, "args" => [[true], [false]]})
|
2012-06-01 16:37:50 +03:00
|
|
|
end
|
2019-02-28 12:43:50 -08:00
|
|
|
end
|
2015-09-04 15:35:33 -07:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
it "should relay parameters through json" do
|
2019-02-28 12:43:50 -08:00
|
|
|
assert Sidekiq::Client.enqueue(InlineWorkerWithTimeParam, Time.now.to_f)
|
2012-05-01 15:54:04 -07:00
|
|
|
end
|
|
|
|
end
|