-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathshared_examples_for_active_job.rb
281 lines (242 loc) · 10.5 KB
/
shared_examples_for_active_job.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
require 'active_job'
require 'shoryuken/extensions/active_job_extensions'
# Stand-in for a job class specified by the user
class TestJob < ActiveJob::Base; end
# rubocop:disable Metrics/BlockLength
RSpec.shared_examples 'active_job_adapters' do
let(:job_sqs_send_message_parameters) { {} }
let(:job) do
job = TestJob.new
job.sqs_send_message_parameters = job_sqs_send_message_parameters
job
end
let(:fifo) { false }
let(:queue) { double 'Queue', fifo?: fifo }
before do
allow(Shoryuken::Client).to receive(:queues).with(job.queue_name).and_return(queue)
end
describe '#enqueue' do
specify do
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_deduplication_id]).to_not be
expect(hash[:message_attributes]['shoryuken_class'][:string_value]).to eq(described_class::JobWrapper.to_s)
expect(hash[:message_attributes]['shoryuken_class'][:data_type]).to eq("String")
expect(hash[:message_attributes].keys).to eq(['shoryuken_class'])
end
expect(Shoryuken).to receive(:register_worker).with(job.queue_name, described_class::JobWrapper)
subject.enqueue(job)
end
it "should mutate the job's sqs_send_message_parameters reference to match those sent to the queue" do
expect(queue).to receive(:send_message) do |options|
expect(options).to be(job.sqs_send_message_parameters)
end
subject.enqueue(job)
end
context 'when fifo' do
let(:fifo) { true }
it 'does not include job_id and enqueued_at in the deduplication_id' do
expect(queue).to receive(:send_message) do |hash|
message_deduplication_id = Digest::SHA256.hexdigest(
JSON.dump(job.serialize.except('job_id', 'enqueued_at'))
)
expect(hash[:message_deduplication_id]).to eq(message_deduplication_id)
end
expect(Shoryuken).to receive(:register_worker).with(job.queue_name, described_class::JobWrapper)
subject.enqueue(job)
end
context 'with message_deduplication_id' do
context 'when message_deduplication_id is specified in options' do
it 'should enqueue a message with the deduplication_id specified in options' do
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_deduplication_id]).to eq('options-dedupe-id')
end
subject.enqueue(job, message_deduplication_id: 'options-dedupe-id')
end
end
context 'when message_deduplication_id is specified on the job' do
let(:job_sqs_send_message_parameters) { { message_deduplication_id: 'job-dedupe-id' } }
it 'should enqueue a message with the deduplication_id specified on the job' do
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_deduplication_id]).to eq('job-dedupe-id')
end
subject.enqueue job
end
end
context 'when message_deduplication_id is specified on the job and also in options' do
let(:job_sqs_send_message_parameters) { { message_deduplication_id: 'job-dedupe-id' } }
it 'should enqueue a message with the deduplication_id specified in options' do
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_deduplication_id]).to eq('options-dedupe-id')
end
subject.enqueue(job, message_deduplication_id: 'options-dedupe-id')
end
end
end
end
context 'with message_group_id' do
context 'when message_group_id is specified in options' do
it 'should enqueue a message with the group_id specified in options' do
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_group_id]).to eq('options-group-id')
end
subject.enqueue(job, message_group_id: 'options-group-id')
end
end
context 'when message_group_id is specified on the job' do
let(:job_sqs_send_message_parameters) { { message_group_id: 'job-group-id' } }
it 'should enqueue a message with the group_id specified on the job' do
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_group_id]).to eq('job-group-id')
end
subject.enqueue job
end
end
context 'when message_group_id is specified on the job and also in options' do
let(:job_sqs_send_message_parameters) { { message_group_id: 'job-group-id' } }
it 'should enqueue a message with the group_id specified in options' do
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_group_id]).to eq('options-group-id')
end
subject.enqueue(job, message_group_id: 'options-group-id')
end
end
end
context 'with additional message attributes' do
it 'should combine with activejob attributes' do
custom_message_attributes = {
'tracer_id' => {
string_value: SecureRandom.hex,
data_type: 'String'
}
}
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_attributes]['shoryuken_class'][:string_value]).to eq(described_class::JobWrapper.to_s)
expect(hash[:message_attributes]['shoryuken_class'][:data_type]).to eq("String")
expect(hash[:message_attributes]['tracer_id'][:string_value]).to eq(custom_message_attributes['tracer_id'][:string_value])
expect(hash[:message_attributes]['tracer_id'][:data_type]).to eq("String")
end
expect(Shoryuken).to receive(:register_worker).with(job.queue_name, described_class::JobWrapper)
subject.enqueue(job, message_attributes: custom_message_attributes)
end
context 'when message_attributes are specified on the job' do
let(:job_sqs_send_message_parameters) do
{
message_attributes: {
'tracer_id' => {
data_type: 'String',
string_value: 'job-value'
}
}
}
end
it 'should enqueue a message with the message_attributes specified on the job' do
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_attributes]['tracer_id']).to eq({ data_type: 'String', string_value: 'job-value' })
expect(hash[:message_attributes]['shoryuken_class']).to eq({ data_type: 'String', string_value: described_class::JobWrapper.to_s })
end
subject.enqueue job
end
end
context 'when message_attributes are specified on the job and also in options' do
let(:job_sqs_send_message_parameters) do
{
message_attributes: {
'tracer_id' => {
data_type: 'String',
string_value: 'job-value'
}
}
}
end
it 'should enqueue a message with the message_attributes speficied in options' do
custom_message_attributes = {
'options_tracer_id' => {
string_value: 'options-value',
data_type: 'String'
}
}
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_attributes]['tracer_id']).to be_nil
expect(hash[:message_attributes]['options_tracer_id']).to eq({ data_type: 'String', string_value: 'options-value' })
expect(hash[:message_attributes]['shoryuken_class']).to eq({ data_type: 'String', string_value: described_class::JobWrapper.to_s })
end
subject.enqueue job, message_attributes: custom_message_attributes
end
end
end
end
context 'with message_system_attributes' do
context 'when message_system_attributes are specified in options' do
it 'should enqueue a message with message_system_attributes specified in options' do
system_attributes = {
'AWSTraceHeader' => {
string_value: 'trace_id',
data_type: 'String'
}
}
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_system_attributes]['AWSTraceHeader'][:string_value]).to eq('trace_id')
expect(hash[:message_system_attributes]['AWSTraceHeader'][:data_type]).to eq('String')
end
subject.enqueue(job, message_system_attributes: system_attributes)
end
end
context 'when message_system_attributes are specified on the job' do
let(:job_sqs_send_message_parameters) do
{
message_system_attributes: {
'AWSTraceHeader' => {
string_value: 'job-value',
data_type: 'String'
}
}
}
end
it 'should enqueue a message with the message_system_attributes specified on the job' do
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_system_attributes]['AWSTraceHeader']).to eq({ data_type: 'String', string_value: 'job-value' })
end
subject.enqueue job
end
end
context 'when message_system_attributes are specified on the job and also in options' do
let(:job_sqs_send_message_parameters) do
{
message_system_attributes: {
'job_trace_header' => {
string_value: 'job-value',
data_type: 'String'
}
}
}
end
it 'should enqueue a message with the message_system_attributes speficied in options' do
custom_message_attributes = {
'options_trace_header' => {
string_value: 'options-value',
data_type: 'String'
}
}
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_system_attributes]['job_trace_header']).to be_nil
expect(hash[:message_system_attributes]['options_trace_header']).to eq({ data_type: 'String', string_value: 'options-value' })
end
subject.enqueue job, message_system_attributes: custom_message_attributes
end
end
end
describe '#enqueue_at' do
specify do
delay = 1
expect(queue).to receive(:send_message) do |hash|
expect(hash[:message_deduplication_id]).to_not be
expect(hash[:delay_seconds]).to eq(delay)
end
expect(Shoryuken).to receive(:register_worker).with(job.queue_name, described_class::JobWrapper)
# need to figure out what to require Time.current and N.minutes to remove the stub
allow(subject).to receive(:calculate_delay).and_return(delay)
subject.enqueue_at(job, nil)
end
end
end
# rubocop:enable Metrics/BlockLength