2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2022-03-03 15:50:03 -05:00
|
|
|
|
|
|
|
require_relative "helper"
|
2022-03-14 15:16:36 -04:00
|
|
|
require "active_job"
|
2022-03-03 15:50:03 -05:00
|
|
|
require "sidekiq/api"
|
2022-03-09 14:36:32 -05:00
|
|
|
require "sidekiq/rails"
|
2012-01-21 19:42:21 -05:00
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
describe Sidekiq::Client do
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "errors" do
|
|
|
|
it "raises ArgumentError with invalid params" do
|
2012-01-23 15:56:49 -05:00
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push("foo", 1)
|
2012-01-23 15:56:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push("foo", class: "Foo", noargs: [1, 2])
|
2012-01-23 15:56:49 -05:00
|
|
|
end
|
2012-11-15 15:58:58 -05:00
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push("queue" => "foo", "class" => MyWorker, "noargs" => [1, 2])
|
2012-11-15 15:58:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push("queue" => "foo", "class" => 42, "args" => [1, 2])
|
2012-11-15 15:58:58 -05:00
|
|
|
end
|
|
|
|
|
2013-01-05 04:32:05 -05:00
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push("queue" => "foo", "class" => MyWorker, "args" => :not_an_array)
|
2013-01-05 04:32:05 -05:00
|
|
|
end
|
2017-03-23 14:05:20 -04:00
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push("queue" => "foo", "class" => MyWorker, "args" => [1], "at" => :not_a_numeric)
|
2019-09-04 15:21:48 -04:00
|
|
|
end
|
2019-09-21 17:03:59 -04:00
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push("queue" => "foo", "class" => MyWorker, "args" => [1], "tags" => :not_an_array)
|
2019-09-21 17:03:59 -04:00
|
|
|
end
|
2013-10-26 00:07:54 -04:00
|
|
|
end
|
2015-09-22 15:48:43 -04:00
|
|
|
end
|
2013-10-26 00:07:54 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "as instance" do
|
|
|
|
it "handles nil queue" do
|
2020-02-26 13:51:25 -05:00
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push("class" => "Blah", "args" => [1, 2, 3], "queue" => "")
|
2020-02-26 13:51:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "can push" do
|
2015-09-22 15:48:43 -04:00
|
|
|
client = Sidekiq::Client.new
|
2022-03-03 15:50:03 -05:00
|
|
|
jid = client.push("class" => "Blah", "args" => [1, 2, 3])
|
2015-09-22 15:48:43 -04:00
|
|
|
assert_equal 24, jid.size
|
|
|
|
end
|
2013-10-26 00:07:54 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "allows middleware to stop bulk jobs" do
|
2016-03-16 12:26:07 -04:00
|
|
|
mware = Class.new do
|
2022-03-03 15:50:03 -05:00
|
|
|
def call(worker_klass, msg, q, r)
|
|
|
|
msg["args"][0] == 1 ? yield : false
|
2016-03-16 12:26:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
client = Sidekiq::Client.new
|
|
|
|
client.middleware do |chain|
|
|
|
|
chain.add mware
|
|
|
|
end
|
|
|
|
q = Sidekiq::Queue.new
|
|
|
|
q.clear
|
2022-03-03 15:50:03 -05:00
|
|
|
result = client.push_bulk("class" => "Blah", "args" => [[1], [2], [3]])
|
2016-03-16 12:26:07 -04:00
|
|
|
assert_equal 1, result.size
|
|
|
|
assert_equal 1, q.size
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "allows local middleware modification" do
|
2015-09-22 15:48:43 -04:00
|
|
|
$called = false
|
2022-03-03 15:50:03 -05:00
|
|
|
mware = Class.new {
|
|
|
|
def call(worker_klass, msg, q, r)
|
|
|
|
$called = true
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
}
|
2015-09-22 15:48:43 -04:00
|
|
|
client = Sidekiq::Client.new
|
|
|
|
client.middleware do |chain|
|
|
|
|
chain.add mware
|
2013-10-26 00:07:54 -04:00
|
|
|
end
|
2022-03-03 15:50:03 -05:00
|
|
|
client.push("class" => "Blah", "args" => [1, 2, 3])
|
2015-09-22 15:48:43 -04:00
|
|
|
|
|
|
|
assert $called
|
|
|
|
assert client.middleware.exists?(mware)
|
|
|
|
refute Sidekiq.client_middleware.exists?(mware)
|
2012-01-23 15:56:49 -05:00
|
|
|
end
|
2015-09-22 15:48:43 -04:00
|
|
|
end
|
2012-01-23 15:56:49 -05:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "client" do
|
|
|
|
it "pushes messages to redis" do
|
|
|
|
q = Sidekiq::Queue.new("foo")
|
2015-09-22 15:48:43 -04:00
|
|
|
pre = q.size
|
2022-03-03 15:50:03 -05:00
|
|
|
jid = Sidekiq::Client.push("queue" => "foo", "class" => MyWorker, "args" => [1, 2])
|
2015-09-22 15:48:43 -04:00
|
|
|
assert jid
|
|
|
|
assert_equal 24, jid.size
|
|
|
|
assert_equal pre + 1, q.size
|
2012-01-21 19:42:21 -05:00
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "pushes messages to redis using a String class" do
|
|
|
|
q = Sidekiq::Queue.new("foo")
|
2015-09-22 15:48:43 -04:00
|
|
|
pre = q.size
|
2022-03-03 15:50:03 -05:00
|
|
|
jid = Sidekiq::Client.push("queue" => "foo", "class" => "MyWorker", "args" => [1, 2])
|
2015-09-22 15:48:43 -04:00
|
|
|
assert jid
|
|
|
|
assert_equal 24, jid.size
|
|
|
|
assert_equal pre + 1, q.size
|
2012-11-15 15:58:58 -05:00
|
|
|
end
|
|
|
|
|
2012-01-23 15:56:49 -05:00
|
|
|
class MyWorker
|
2012-01-25 16:53:00 -05:00
|
|
|
include Sidekiq::Worker
|
2012-01-23 15:56:49 -05:00
|
|
|
end
|
|
|
|
|
2012-02-10 23:20:01 -05:00
|
|
|
class QueuedWorker
|
|
|
|
include Sidekiq::Worker
|
2022-03-03 15:50:03 -05:00
|
|
|
sidekiq_options queue: :flimflam
|
2012-02-10 23:20:01 -05:00
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "enqueues" do
|
|
|
|
Sidekiq.redis { |c| c.flushdb }
|
2022-03-03 15:37:25 -05:00
|
|
|
assert_equal Sidekiq.default_job_options, MyWorker.get_sidekiq_options
|
2015-09-22 15:48:43 -04:00
|
|
|
assert MyWorker.perform_async(1, 2)
|
|
|
|
assert Sidekiq::Client.enqueue(MyWorker, 1, 2)
|
|
|
|
assert Sidekiq::Client.enqueue_to(:custom_queue, MyWorker, 1, 2)
|
2022-03-03 15:50:03 -05:00
|
|
|
assert_equal 1, Sidekiq::Queue.new("custom_queue").size
|
2018-12-03 17:09:26 -05:00
|
|
|
assert Sidekiq::Client.enqueue_to_in(:custom_queue, 3, MyWorker, 1, 2)
|
|
|
|
assert Sidekiq::Client.enqueue_to_in(:custom_queue, -3, MyWorker, 1, 2)
|
2022-03-03 15:50:03 -05:00
|
|
|
assert_equal 2, Sidekiq::Queue.new("custom_queue").size
|
2018-12-03 17:09:26 -05:00
|
|
|
assert Sidekiq::Client.enqueue_in(3, MyWorker, 1, 2)
|
2015-09-22 15:48:43 -04:00
|
|
|
assert QueuedWorker.perform_async(1, 2)
|
2022-03-03 15:50:03 -05:00
|
|
|
assert_equal 1, Sidekiq::Queue.new("flimflam").size
|
2012-02-15 14:28:19 -05:00
|
|
|
end
|
2021-12-07 16:20:20 -05:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "argument checking" do
|
2021-12-07 16:20:20 -05:00
|
|
|
class InterestingWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
|
|
|
def perform(an_argument)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "enqueues jobs with a symbol as an argument" do
|
2021-12-07 16:20:20 -05:00
|
|
|
InterestingWorker.perform_async(:symbol)
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "enqueues jobs with a Date as an argument" do
|
2021-12-07 16:20:20 -05:00
|
|
|
InterestingWorker.perform_async(Date.new(2021, 1, 1))
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "enqueues jobs with a Hash with symbols and string as keys as an argument" do
|
2021-12-07 16:20:20 -05:00
|
|
|
InterestingWorker.perform_async(
|
|
|
|
{
|
2022-03-03 15:50:03 -05:00
|
|
|
:some => "hash",
|
|
|
|
"with" => "different_keys"
|
2021-12-07 16:20:20 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "enqueues jobs with a Struct as an argument" do
|
2021-12-07 16:20:20 -05:00
|
|
|
InterestingWorker.perform_async(
|
|
|
|
Struct.new(:x, :y).new(0, 0)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "works with a JSON-friendly deep, nested structure" do
|
2021-12-07 16:20:20 -05:00
|
|
|
InterestingWorker.perform_async(
|
|
|
|
{
|
2022-03-03 15:50:03 -05:00
|
|
|
"foo" => ["a", "b", "c"],
|
|
|
|
"bar" => ["x", "y", "z"]
|
2021-12-07 16:20:20 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "strict args is enabled" do
|
2021-12-07 16:20:20 -05:00
|
|
|
before do
|
2022-01-05 22:54:12 -05:00
|
|
|
Sidekiq.strict_args!
|
2021-12-07 16:20:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2022-01-05 22:54:12 -05:00
|
|
|
Sidekiq.strict_args!(false)
|
2021-12-07 16:20:20 -05:00
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "raises an error when using a symbol as an argument" do
|
2021-12-07 16:20:20 -05:00
|
|
|
assert_raises ArgumentError do
|
|
|
|
InterestingWorker.perform_async(:symbol)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "raises an error when using a Date as an argument" do
|
2021-12-07 16:20:20 -05:00
|
|
|
assert_raises ArgumentError do
|
|
|
|
InterestingWorker.perform_async(Date.new(2021, 1, 1))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "raises an error when using a Hash with symbols and string as keys as an argument" do
|
2021-12-07 16:20:20 -05:00
|
|
|
assert_raises ArgumentError do
|
|
|
|
InterestingWorker.perform_async(
|
|
|
|
{
|
2022-03-03 15:50:03 -05:00
|
|
|
:some => "hash",
|
|
|
|
"with" => "different_keys"
|
2021-12-07 16:20:20 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "raises an error when using a Struct as an argument" do
|
2021-12-07 16:20:20 -05:00
|
|
|
assert_raises ArgumentError do
|
|
|
|
InterestingWorker.perform_async(
|
|
|
|
Struct.new(:x, :y).new(0, 0)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "works with a JSON-friendly deep, nested structure" do
|
2021-12-07 16:20:20 -05:00
|
|
|
InterestingWorker.perform_async(
|
|
|
|
{
|
2022-03-03 15:50:03 -05:00
|
|
|
"foo" => ["a", "b", "c"],
|
|
|
|
"bar" => ["x", "y", "z"]
|
2021-12-07 16:20:20 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "worker that takes deep, nested structures" do
|
|
|
|
it "raises an error on JSON-unfriendly structures" do
|
2022-03-09 14:36:32 -05:00
|
|
|
error = assert_raises ArgumentError do
|
2021-12-07 16:20:20 -05:00
|
|
|
InterestingWorker.perform_async(
|
|
|
|
{
|
2022-03-03 15:50:03 -05:00
|
|
|
"foo" => [:a, :b, :c],
|
|
|
|
:bar => ["x", "y", "z"]
|
2021-12-07 16:20:20 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2022-03-14 15:16:36 -04:00
|
|
|
assert_match(/Job arguments to InterestingWorker/, error.message)
|
2022-03-09 14:36:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-14 15:16:36 -04:00
|
|
|
describe "ActiveJob with non-native json types" do
|
2022-03-09 14:36:32 -05:00
|
|
|
before do
|
|
|
|
ActiveJob::Base.queue_adapter = :sidekiq
|
|
|
|
ActiveJob::Base.logger = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestActiveJob < ActiveJob::Base
|
|
|
|
def perform(arg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-14 15:16:36 -04:00
|
|
|
it "raises error with correct class name" do
|
2022-03-09 14:36:32 -05:00
|
|
|
error = assert_raises ArgumentError do
|
2022-03-14 15:16:36 -04:00
|
|
|
TestActiveJob.perform_later(BigDecimal("1.1212"))
|
2022-03-09 14:36:32 -05:00
|
|
|
end
|
2022-03-14 15:16:36 -04:00
|
|
|
assert_match(/Job arguments to TestActiveJob/, error.message)
|
2021-12-07 16:20:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-01-21 19:42:21 -05:00
|
|
|
end
|
2012-06-16 23:56:55 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "bulk" do
|
2013-01-06 00:17:08 -05:00
|
|
|
after do
|
|
|
|
Sidekiq::Queue.new.clear
|
|
|
|
end
|
2019-09-04 15:21:48 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "can push a large set of jobs at once" do
|
|
|
|
jids = Sidekiq::Client.push_bulk("class" => QueuedWorker, "args" => (1..1_000).to_a.map { |x| Array(x) })
|
2013-11-05 12:17:30 -05:00
|
|
|
assert_equal 1_000, jids.size
|
2012-09-11 23:53:22 -04:00
|
|
|
end
|
2019-09-04 15:21:48 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "can push a large set of jobs at once using a String class" do
|
|
|
|
jids = Sidekiq::Client.push_bulk("class" => "QueuedWorker", "args" => (1..1_000).to_a.map { |x| Array(x) })
|
2013-11-05 12:17:30 -05:00
|
|
|
assert_equal 1_000, jids.size
|
|
|
|
end
|
2019-09-04 15:21:48 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "can push jobs scheduled at different times" do
|
2019-09-04 15:21:48 -04:00
|
|
|
first_at = Time.new(2019, 1, 1)
|
|
|
|
second_at = Time.new(2019, 1, 2)
|
2022-03-03 15:50:03 -05:00
|
|
|
jids = Sidekiq::Client.push_bulk("class" => QueuedWorker, "args" => [[1], [2]], "at" => [first_at.to_f, second_at.to_f])
|
2019-09-04 15:21:48 -04:00
|
|
|
(first_jid, second_jid) = jids
|
|
|
|
assert_equal first_at, Sidekiq::ScheduledSet.new.find_job(first_jid).at
|
|
|
|
assert_equal second_at, Sidekiq::ScheduledSet.new.find_job(second_jid).at
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "can push jobs scheduled using ActiveSupport::Duration" do
|
2022-03-14 15:16:36 -04:00
|
|
|
require "active_support/core_ext/integer/time"
|
2022-03-03 15:50:03 -05:00
|
|
|
jids = Sidekiq::Client.push_bulk("class" => QueuedWorker, "args" => [[1], [2]], "at" => [1.seconds, 111.seconds])
|
2021-09-20 20:56:07 -04:00
|
|
|
assert_equal 2, jids.size
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "returns the jids for the jobs" do
|
|
|
|
Sidekiq::Client.push_bulk("class" => "QueuedWorker", "args" => (1..2).to_a.map { |x| Array(x) }).each do |jid|
|
2014-03-19 20:41:11 -04:00
|
|
|
assert_match(/[0-9a-f]{12}/, jid)
|
2013-11-05 12:17:30 -05:00
|
|
|
end
|
2012-11-15 15:58:58 -05:00
|
|
|
end
|
2019-09-04 15:21:48 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "handles no jobs" do
|
|
|
|
result = Sidekiq::Client.push_bulk("class" => "QueuedWorker", "args" => [])
|
2016-05-31 13:05:16 -04:00
|
|
|
assert_equal 0, result.size
|
|
|
|
end
|
2020-05-26 14:01:22 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "errors" do
|
|
|
|
it "raises ArgumentError with invalid params" do
|
2020-05-26 14:01:22 -04:00
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push_bulk("class" => "QueuedWorker", "args" => [[1], 2])
|
2020-05-26 14:01:22 -04:00
|
|
|
end
|
2020-05-26 14:38:09 -04:00
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push_bulk("class" => "QueuedWorker", "args" => [[1], [2]], "at" => [Time.now.to_f, :not_a_numeric])
|
2020-05-26 14:38:09 -04:00
|
|
|
end
|
2020-06-18 10:33:13 -04:00
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push_bulk("class" => QueuedWorker, "args" => [[1], [2]], "at" => [Time.now.to_f])
|
2020-06-18 10:33:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push_bulk("class" => QueuedWorker, "args" => [[1]], "at" => [Time.now.to_f, Time.now.to_f])
|
2020-06-18 10:33:13 -04:00
|
|
|
end
|
2020-05-26 14:01:22 -04:00
|
|
|
end
|
|
|
|
end
|
2021-11-02 12:54:44 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe ".perform_bulk" do
|
|
|
|
it "pushes a large set of jobs" do
|
2021-11-02 12:54:44 -04:00
|
|
|
jids = MyWorker.perform_bulk((1..1_001).to_a.map { |x| Array(x) })
|
|
|
|
assert_equal 1_001, jids.size
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "pushes a large set of jobs with a different batch size" do
|
2021-11-02 12:54:44 -04:00
|
|
|
jids = MyWorker.perform_bulk((1..1_001).to_a.map { |x| Array(x) }, batch_size: 100)
|
|
|
|
assert_equal 1_001, jids.size
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "handles no jobs" do
|
2021-11-02 12:54:44 -04:00
|
|
|
jids = MyWorker.perform_bulk([])
|
|
|
|
assert_equal 0, jids.size
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "errors" do
|
|
|
|
it "raises ArgumentError with invalid params" do
|
2021-11-02 12:54:44 -04:00
|
|
|
assert_raises ArgumentError do
|
2022-03-03 15:50:03 -05:00
|
|
|
Sidekiq::Client.push_bulk("class" => "MyWorker", "args" => [[1], 2])
|
2021-11-02 12:54:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-11-16 11:54:35 -05:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "lazy enumerator" do
|
|
|
|
it "enqueues the jobs by evaluating the enumerator" do
|
2021-11-16 11:54:35 -05:00
|
|
|
lazy_array = (1..1_001).to_a.map { |x| Array(x) }.lazy
|
|
|
|
jids = MyWorker.perform_bulk(lazy_array)
|
|
|
|
assert_equal 1_001, jids.size
|
|
|
|
end
|
|
|
|
end
|
2021-11-02 12:54:44 -04:00
|
|
|
end
|
2012-09-11 23:53:22 -04:00
|
|
|
end
|
|
|
|
|
2012-06-16 23:56:55 -04:00
|
|
|
class BaseWorker
|
|
|
|
include Sidekiq::Worker
|
2022-03-03 15:50:03 -05:00
|
|
|
sidekiq_options "retry" => "base"
|
2012-06-16 23:56:55 -04:00
|
|
|
end
|
2022-03-03 15:50:03 -05:00
|
|
|
|
2012-06-16 23:56:55 -04:00
|
|
|
class AWorker < BaseWorker
|
|
|
|
end
|
2022-03-03 15:50:03 -05:00
|
|
|
|
2012-06-16 23:56:55 -04:00
|
|
|
class BWorker < BaseWorker
|
2022-03-03 15:50:03 -05:00
|
|
|
sidekiq_options "retry" => "b"
|
2012-06-16 23:56:55 -04:00
|
|
|
end
|
2022-03-03 15:50:03 -05:00
|
|
|
|
2012-11-08 15:28:50 -05:00
|
|
|
class CWorker < BaseWorker
|
2022-03-03 15:50:03 -05:00
|
|
|
sidekiq_options "retry" => 2
|
2012-11-08 15:28:50 -05:00
|
|
|
end
|
2012-06-16 23:56:55 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "client middleware" do
|
2012-09-13 00:22:13 -04:00
|
|
|
class Stopper
|
2015-09-22 15:48:43 -04:00
|
|
|
def call(worker_class, job, queue, r)
|
2014-03-26 00:38:17 -04:00
|
|
|
raise ArgumentError unless r
|
2022-03-03 15:50:03 -05:00
|
|
|
yield if job["args"].first.odd?
|
2012-09-13 00:22:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-04 13:39:16 -04:00
|
|
|
class MiddlewareArguments
|
|
|
|
def call(worker_class, job, queue, redis)
|
|
|
|
$arguments_worker_class = worker_class
|
|
|
|
$arguments_job = job
|
|
|
|
$arguments_queue = queue
|
|
|
|
$arguments_redis = redis
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "push sends correct arguments to middleware" do
|
|
|
|
minimum_job_args = ["args", "class", "created_at", "enqueued_at", "jid", "queue"]
|
|
|
|
client = Sidekiq::Client.new
|
|
|
|
client.middleware do |chain|
|
|
|
|
chain.add MiddlewareArguments
|
|
|
|
end
|
|
|
|
client.push("class" => MyWorker, "args" => [0])
|
|
|
|
|
|
|
|
assert_equal($arguments_worker_class, MyWorker)
|
|
|
|
assert((minimum_job_args & $arguments_job.keys) == minimum_job_args)
|
|
|
|
assert_instance_of(ConnectionPool, $arguments_redis)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "push bulk sends correct arguments to middleware" do
|
|
|
|
minimum_job_args = ["args", "class", "created_at", "enqueued_at", "jid", "queue"]
|
|
|
|
client = Sidekiq::Client.new
|
|
|
|
client.middleware do |chain|
|
|
|
|
chain.add MiddlewareArguments
|
|
|
|
end
|
|
|
|
client.push_bulk("class" => MyWorker, "args" => [[0]])
|
|
|
|
|
|
|
|
assert_equal($arguments_worker_class, MyWorker)
|
|
|
|
assert((minimum_job_args & $arguments_job.keys) == minimum_job_args)
|
|
|
|
assert_instance_of(ConnectionPool, $arguments_redis)
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "can stop some of the jobs from pushing" do
|
2015-09-22 15:48:43 -04:00
|
|
|
client = Sidekiq::Client.new
|
|
|
|
client.middleware do |chain|
|
|
|
|
chain.add Stopper
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
assert_nil client.push("class" => MyWorker, "args" => [0])
|
|
|
|
assert_match(/[0-9a-f]{12}/, client.push("class" => MyWorker, "args" => [1]))
|
|
|
|
client.push_bulk("class" => MyWorker, "args" => [[0], [1]]).each do |jid|
|
2015-09-22 15:48:43 -04:00
|
|
|
assert_match(/[0-9a-f]{12}/, jid)
|
2012-09-13 00:22:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "inheritance" do
|
|
|
|
it "inherits sidekiq options" do
|
|
|
|
assert_equal "base", AWorker.get_sidekiq_options["retry"]
|
|
|
|
assert_equal "b", BWorker.get_sidekiq_options["retry"]
|
2012-06-16 23:56:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "sharding" do
|
2014-03-25 14:56:15 -04:00
|
|
|
class DWorker < BaseWorker
|
|
|
|
end
|
2015-09-22 15:48:43 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "allows sidekiq_options to point to different Redi" do
|
2014-03-25 14:56:15 -04:00
|
|
|
conn = MiniTest::Mock.new
|
2021-11-02 10:49:34 -04:00
|
|
|
conn.expect(:pipelined, [0, 1])
|
2022-03-03 15:50:03 -05:00
|
|
|
DWorker.sidekiq_options("pool" => ConnectionPool.new(size: 1) { conn })
|
|
|
|
DWorker.perform_async(1, 2, 3)
|
2014-03-25 14:56:15 -04:00
|
|
|
conn.verify
|
|
|
|
end
|
2015-09-22 15:48:43 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "allows #via to point to same Redi" do
|
2016-03-29 15:14:07 -04:00
|
|
|
conn = MiniTest::Mock.new
|
2021-11-02 10:49:34 -04:00
|
|
|
conn.expect(:pipelined, [0, 1])
|
2016-03-29 15:14:07 -04:00
|
|
|
sharded_pool = ConnectionPool.new(size: 1) { conn }
|
|
|
|
Sidekiq::Client.via(sharded_pool) do
|
|
|
|
Sidekiq::Client.via(sharded_pool) do
|
2022-03-03 15:50:03 -05:00
|
|
|
CWorker.perform_async(1, 2, 3)
|
2016-03-29 15:14:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
conn.verify
|
|
|
|
end
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "allows #via to point to different Redi" do
|
2014-03-26 23:58:45 -04:00
|
|
|
default = Sidekiq::Client.new.redis_pool
|
2018-01-10 12:53:57 -05:00
|
|
|
|
|
|
|
moo = MiniTest::Mock.new
|
2021-11-02 10:49:34 -04:00
|
|
|
moo.expect(:pipelined, [0, 1])
|
2018-01-10 12:53:57 -05:00
|
|
|
beef = ConnectionPool.new(size: 1) { moo }
|
|
|
|
|
|
|
|
oink = MiniTest::Mock.new
|
2021-11-02 10:49:34 -04:00
|
|
|
oink.expect(:pipelined, [0, 1])
|
2018-01-10 12:53:57 -05:00
|
|
|
pork = ConnectionPool.new(size: 1) { oink }
|
|
|
|
|
|
|
|
Sidekiq::Client.via(beef) do
|
2022-03-03 15:50:03 -05:00
|
|
|
CWorker.perform_async(1, 2, 3)
|
2018-01-10 12:53:57 -05:00
|
|
|
assert_equal beef, Sidekiq::Client.new.redis_pool
|
|
|
|
Sidekiq::Client.via(pork) do
|
|
|
|
assert_equal pork, Sidekiq::Client.new.redis_pool
|
2022-03-03 15:50:03 -05:00
|
|
|
CWorker.perform_async(1, 2, 3)
|
2014-03-26 23:58:45 -04:00
|
|
|
end
|
2018-01-10 12:53:57 -05:00
|
|
|
assert_equal beef, Sidekiq::Client.new.redis_pool
|
2014-03-26 23:35:57 -04:00
|
|
|
end
|
2014-03-26 23:58:45 -04:00
|
|
|
assert_equal default, Sidekiq::Client.new.redis_pool
|
2018-01-10 12:53:57 -05:00
|
|
|
moo.verify
|
|
|
|
oink.verify
|
2014-03-26 23:35:57 -04:00
|
|
|
end
|
2015-09-22 15:48:43 -04:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "allows Resque helpers to point to different Redi" do
|
2014-03-25 14:56:15 -04:00
|
|
|
conn = MiniTest::Mock.new
|
2022-01-24 11:18:16 -05:00
|
|
|
conn.expect(:pipelined, []) { |*args, &block| block.call(conn) }
|
2014-03-25 14:56:15 -04:00
|
|
|
conn.expect(:zadd, 1, [String, Array])
|
2022-03-03 15:50:03 -05:00
|
|
|
DWorker.sidekiq_options("pool" => ConnectionPool.new(size: 1) { conn })
|
2014-03-25 14:56:15 -04:00
|
|
|
Sidekiq::Client.enqueue_in(10, DWorker, 3)
|
|
|
|
conn.verify
|
|
|
|
end
|
|
|
|
end
|
2016-01-18 10:58:58 -05:00
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
describe "class attribute race conditions" do
|
2018-10-18 11:27:45 -04:00
|
|
|
new_class = -> {
|
|
|
|
Class.new do
|
2022-03-03 15:50:03 -05:00
|
|
|
class_eval("include Sidekiq::Worker", __FILE__, __LINE__)
|
2018-10-18 11:27:45 -04:00
|
|
|
|
|
|
|
define_method(:foo) { get_sidekiq_options }
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2022-03-03 15:50:03 -05:00
|
|
|
it "does not explode when new initializing classes from multiple threads" do
|
2018-10-18 17:35:10 -04:00
|
|
|
100.times do
|
2018-10-18 11:27:45 -04:00
|
|
|
klass = new_class.call
|
|
|
|
|
|
|
|
t1 = Thread.new { klass.sidekiq_options({}) }
|
|
|
|
t2 = Thread.new { klass.sidekiq_options({}) }
|
|
|
|
t1.join
|
|
|
|
t2.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-01-21 19:42:21 -05:00
|
|
|
end
|