-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
test_fetch.rb
69 lines (59 loc) · 1.69 KB
/
test_fetch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true
require_relative "helper"
require "sidekiq/fetch"
require "sidekiq/api"
describe Sidekiq::BasicFetch do
before do
Sidekiq.redis do |conn|
conn.flushdb
conn.rpush("queue:basic", "msg")
end
Sidekiq.reset!
@config = Sidekiq
end
def fetcher(options)
@config.merge!(options)
Sidekiq::BasicFetch.new(@config)
end
it "retrieves" do
fetch = fetcher(queues: ["basic", "bar"])
uow = fetch.retrieve_work
refute_nil uow
assert_equal "basic", uow.queue_name
assert_equal "msg", uow.job
q = Sidekiq::Queue.new("basic")
assert_equal 0, q.size
uow.requeue
assert_equal 1, q.size
assert_nil uow.acknowledge
end
it "retrieves with strict setting" do
fetch = fetcher(queues: ["basic", "bar", "bar"], strict: true)
cmd = fetch.queues_cmd
assert_equal cmd, ["queue:basic", "queue:bar", {timeout: Sidekiq::BasicFetch::TIMEOUT}]
end
it "bulk requeues" do
Sidekiq.redis do |conn|
conn.rpush("queue:foo", ["bob", "bar"])
conn.rpush("queue:bar", "widget")
end
q1 = Sidekiq::Queue.new("foo")
q2 = Sidekiq::Queue.new("bar")
assert_equal 2, q1.size
assert_equal 1, q2.size
fetch = fetcher(queues: ["foo", "bar"])
works = 3.times.map { fetch.retrieve_work }
assert_equal 0, q1.size
assert_equal 0, q2.size
fetch.bulk_requeue(works, {queues: []})
assert_equal 2, q1.size
assert_equal 1, q2.size
end
it "sleeps when no queues are active" do
fetch = fetcher(queues: [])
mock = Minitest::Mock.new
mock.expect(:call, nil, [Sidekiq::BasicFetch::TIMEOUT])
fetch.stub(:sleep, mock) { assert_nil fetch.retrieve_work }
mock.verify
end
end