/
worker_spec.rb
157 lines (146 loc) · 5.46 KB
/
worker_spec.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require 'spec_helper'
require 'resqued/config/worker'
describe Resqued::Config::Worker do
# Create a bunch of Resqued::Worker objects from
#
# worker 'one'
# worker 'two', 'three', :interval => 2
# worker # assumes '*' as the queue
# worker_pool 10
# queue 'four', :percent => 20
# queue 'five', :count => 5
# queue 'six', '40%'
# queue 'seven', 3
# queue '*'
#
# ignore calls to any other top-level method.
let(:evaluator) { described_class.new(:worker_class => FakeWorker) }
let(:result) { evaluator.apply(config) }
module FakeWorker
def self.new(options)
options
end
end
context 'individual' do
let(:config) { <<-END_CONFIG }
before_fork { }
after_fork { }
2.times { worker 'a' }
worker 'b'
worker 'c', 'd'
worker 'd', 'c', :interval => 3
worker
after_fork { } # So that we don't rely on `workers`'s result falling through.
END_CONFIG
it { expect(result.size).to eq(6) }
it { expect(result[0]).to eq(:queues => ['a']) }
it { expect(result[1]).to eq(:queues => ['a']) }
it { expect(result[2]).to eq(:queues => ['b']) }
it { expect(result[3]).to eq(:queues => ['c', 'd']) }
it { expect(result[4]).to eq(:queues => ['d', 'c'], :interval => 3) }
it { expect(result[5]).to eq(:queues => ['*']) }
end
context 'pool (hash for concurrency)' do
let(:config) { <<-END_CONFIG }
before_fork { }
after_fork { }
worker_pool 20, :interval => 1
queue 'a', :percent => 20
queue 'b', :count => 10
queue 'c'
after_fork { } # So that we don't rely on `worker_pool`'s result falling through.
END_CONFIG
it { expect(result.size).to eq(20) }
it { expect(result[0]).to eq(:queues => ['a', 'b', 'c'], :interval => 1) }
it { expect(result[3]).to eq(:queues => ['a', 'b', 'c'], :interval => 1) }
it { expect(result[4]).to eq(:queues => ['b', 'c'], :interval => 1) }
it { expect(result[9]).to eq(:queues => ['b', 'c'], :interval => 1) }
it { expect(result[10]).to eq(:queues => ['c'], :interval => 1) }
it { expect(result[19]).to eq(:queues => ['c'], :interval => 1) }
end
context 'pool (value for concurrency)' do
let(:config) { <<-END_CONFIG }
before_fork { }
after_fork { }
worker_pool 20, :interval => 1
queue 'a', '20%'
queue 'b', 10
queue 'c'
after_fork { } # So that we don't rely on `worker_pool`'s result falling through.
END_CONFIG
it { expect(result.size).to eq(20) }
it { expect(result[0]).to eq(:queues => ['a', 'b', 'c'], :interval => 1) }
it { expect(result[3]).to eq(:queues => ['a', 'b', 'c'], :interval => 1) }
it { expect(result[4]).to eq(:queues => ['b', 'c'], :interval => 1) }
it { expect(result[9]).to eq(:queues => ['b', 'c'], :interval => 1) }
it { expect(result[10]).to eq(:queues => ['c'], :interval => 1) }
it { expect(result[19]).to eq(:queues => ['c'], :interval => 1) }
end
context 'pool, with implied queue' do
let(:config) { <<-END_CONFIG }
before_fork { }
after_fork { }
worker_pool 20
after_fork { } # So that we don't rely on `worker_pool`'s result falling through.
END_CONFIG
it { expect(result.size).to eq(20) }
it { expect(result[0]).to eq(:queues => ['*']) }
it { expect(result[19]).to eq(:queues => ['*']) }
end
context 'pool, with fewer queues than workers' do
let(:config) { <<-END_CONFIG }
before_fork { }
after_fork { }
worker_pool 20
queue 'a', 10
after_fork { } # So that we don't rely on `worker_pool`'s result falling through.
END_CONFIG
it { expect(result.size).to eq(20) }
it { expect(result[0]).to eq(:queues => ['a']) }
it { expect(result[9]).to eq(:queues => ['a']) }
it { expect(result[10]).to eq(:queues => ['*']) }
it { expect(result[19]).to eq(:queues => ['*']) }
end
context 'pool, with more queues than workers' do
let(:config) { <<-END_CONFIG }
before_fork { }
after_fork { }
worker_pool 20
queue 'a', 30
after_fork { } # So that we don't rely on `worker_pool`'s result falling through.
END_CONFIG
it { expect(result.size).to eq(20) }
end
context 'multiple worker configs' do
let(:config) { <<-END_CONFIG }
worker 'one'
worker 'two'
worker_pool 2
END_CONFIG
it { expect(result.size).to eq(4) }
it { expect(result[0]).to eq(:queues => ['one']) }
it { expect(result[1]).to eq(:queues => ['two']) }
it { expect(result[2]).to eq(:queues => ['*']) }
it { expect(result[3]).to eq(:queues => ['*']) }
end
context 'unflattened queues' do
let(:config) { <<-END_CONFIG }
worker 'flattened', 'queues'
queues = %w(unflattened queues)
worker queues, 'flattened'
worker queues
worker 'a', queues, 'b', :interval => 3
END_CONFIG
it { expect(result[0]).to eq(:queues => ['flattened', 'queues']) }
it { expect(result[1]).to eq(:queues => ['unflattened', 'queues', 'flattened']) }
it { expect(result[2]).to eq(:queues => ['unflattened', 'queues']) }
it { expect(result[3]).to eq(:queues => ['a', 'unflattened', 'queues', 'b'], :interval => 3) }
end
context 'with default options' do
let(:evaluator) { described_class.new(:worker_class => FakeWorker, :config => 'something') }
let(:config) { <<-END_CONFIG }
worker 'a', :interval => 1
END_CONFIG
it { expect(result[0]).to eq(:queues => ['a'], :interval => 1, :config => 'something') }
end
end