sidekiq 4.2.9 → 4.2.10

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sidekiq might be problematic. Click here for more details.

Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.github/issue_template.md +6 -1
  3. data/.travis.yml +10 -5
  4. data/Changes.md +9 -0
  5. data/Ent-Changes.md +9 -0
  6. data/Pro-Changes.md +29 -0
  7. data/bin/sidekiqload +11 -11
  8. data/lib/sidekiq.rb +1 -1
  9. data/lib/sidekiq/api.rb +20 -12
  10. data/lib/sidekiq/middleware/server/logging.rb +8 -17
  11. data/lib/sidekiq/processor.rb +16 -7
  12. data/lib/sidekiq/testing.rb +6 -0
  13. data/lib/sidekiq/version.rb +1 -1
  14. data/lib/sidekiq/web/application.rb +2 -2
  15. data/lib/sidekiq/web/helpers.rb +2 -1
  16. data/sidekiq.gemspec +8 -2
  17. data/web/assets/javascripts/dashboard.js +1 -1
  18. data/web/locales/fa.yml +79 -0
  19. data/web/views/_job_info.erb +1 -1
  20. data/web/views/dashboard.erb +2 -2
  21. data/web/views/morgue.erb +2 -0
  22. data/web/views/queue.erb +2 -2
  23. data/web/views/queues.erb +2 -2
  24. data/web/views/retry.erb +1 -1
  25. metadata +74 -63
  26. data/test/config.yml +0 -9
  27. data/test/env_based_config.yml +0 -11
  28. data/test/fake_env.rb +0 -1
  29. data/test/fixtures/en.yml +0 -2
  30. data/test/helper.rb +0 -75
  31. data/test/test_actors.rb +0 -138
  32. data/test/test_api.rb +0 -528
  33. data/test/test_cli.rb +0 -418
  34. data/test/test_client.rb +0 -266
  35. data/test/test_exception_handler.rb +0 -56
  36. data/test/test_extensions.rb +0 -129
  37. data/test/test_fetch.rb +0 -50
  38. data/test/test_launcher.rb +0 -92
  39. data/test/test_logging.rb +0 -35
  40. data/test/test_manager.rb +0 -50
  41. data/test/test_middleware.rb +0 -158
  42. data/test/test_processor.rb +0 -249
  43. data/test/test_rails.rb +0 -22
  44. data/test/test_redis_connection.rb +0 -132
  45. data/test/test_retry.rb +0 -326
  46. data/test/test_retry_exhausted.rb +0 -149
  47. data/test/test_scheduled.rb +0 -115
  48. data/test/test_scheduling.rb +0 -58
  49. data/test/test_sidekiq.rb +0 -107
  50. data/test/test_testing.rb +0 -143
  51. data/test/test_testing_fake.rb +0 -359
  52. data/test/test_testing_inline.rb +0 -94
  53. data/test/test_util.rb +0 -13
  54. data/test/test_web.rb +0 -679
  55. data/test/test_web_helpers.rb +0 -54
@@ -1,418 +0,0 @@
1
- # frozen_string_literal: true
2
- require_relative 'helper'
3
- require 'sidekiq/cli'
4
- require 'tempfile'
5
-
6
- class Sidekiq::CLI
7
- def die(code)
8
- @code = code
9
- end
10
-
11
- def valid?
12
- !@code
13
- end
14
- end
15
-
16
- class TestCli < Sidekiq::Test
17
- describe 'CLI#parse' do
18
-
19
- before do
20
- @cli = Sidekiq::CLI.new
21
- @opts = Sidekiq.options.dup
22
- end
23
-
24
- after do
25
- Sidekiq.options = @opts
26
- end
27
-
28
- it 'does not require the specified Ruby code' do
29
- @cli.parse(['sidekiq', '-r', './test/fake_env.rb'])
30
- refute($LOADED_FEATURES.any? { |x| x =~ /fake_env/ })
31
- assert @cli.valid?
32
- end
33
-
34
- it 'does not boot rails' do
35
- refute defined?(::Rails::Application)
36
- @cli.parse(['sidekiq', '-r', './myapp'])
37
- refute defined?(::Rails::Application)
38
- end
39
-
40
- it 'changes concurrency' do
41
- @cli.parse(['sidekiq', '-c', '60', '-r', './test/fake_env.rb'])
42
- assert_equal 60, Sidekiq.options[:concurrency]
43
- end
44
-
45
- it 'changes concurrency with ENV' do
46
- begin
47
- ENV['RAILS_MAX_THREADS'] = '9'
48
- @cli.parse(['sidekiq', '-c', '60', '-r', './test/fake_env.rb'])
49
- assert_equal 60, Sidekiq.options[:concurrency]
50
- @cli.parse(['sidekiq', '-r', './test/fake_env.rb'])
51
- assert_equal 9, Sidekiq.options[:concurrency]
52
- ensure
53
- ENV.delete('RAILS_MAX_THREADS')
54
- end
55
- end
56
-
57
- it 'changes queues' do
58
- @cli.parse(['sidekiq', '-q', 'foo', '-r', './test/fake_env.rb'])
59
- assert_equal ['foo'], Sidekiq.options[:queues]
60
- end
61
-
62
- it 'accepts a process index' do
63
- @cli.parse(['sidekiq', '-i', '7', '-r', './test/fake_env.rb'])
64
- assert_equal 7, Sidekiq.options[:index]
65
- end
66
-
67
- it 'accepts a stringy process index' do
68
- @cli.parse(['sidekiq', '-i', 'worker.7', '-r', './test/fake_env.rb'])
69
- assert_equal 7, Sidekiq.options[:index]
70
- end
71
-
72
- it 'sets strictly ordered queues if weights are not present' do
73
- @cli.parse(['sidekiq', '-q', 'foo', '-q', 'bar', '-r', './test/fake_env.rb'])
74
- assert_equal true, !!Sidekiq.options[:strict]
75
- end
76
-
77
- it 'does not set strictly ordered queues if weights are present' do
78
- @cli.parse(['sidekiq', '-q', 'foo,3', '-r', './test/fake_env.rb'])
79
- assert_equal false, !!Sidekiq.options[:strict]
80
- end
81
-
82
- it 'does not set strictly ordered queues if weights are present with multiple queues' do
83
- @cli.parse(['sidekiq', '-q', 'foo,3', '-q', 'bar', '-r', './test/fake_env.rb'])
84
- assert_equal false, !!Sidekiq.options[:strict]
85
- end
86
-
87
- it 'changes timeout' do
88
- @cli.parse(['sidekiq', '-t', '30', '-r', './test/fake_env.rb'])
89
- assert_equal 30, Sidekiq.options[:timeout]
90
- end
91
-
92
- it 'handles multiple queues with weights' do
93
- @cli.parse(['sidekiq', '-q', 'foo,3', '-q', 'bar', '-r', './test/fake_env.rb'])
94
- assert_equal %w(foo foo foo bar), Sidekiq.options[:queues]
95
- end
96
-
97
- it 'handles queues with multi-word names' do
98
- @cli.parse(['sidekiq', '-q', 'queue_one', '-q', 'queue-two', '-r', './test/fake_env.rb'])
99
- assert_equal %w(queue_one queue-two), Sidekiq.options[:queues]
100
- end
101
-
102
- it 'handles queues with dots in the name' do
103
- @cli.parse(['sidekiq', '-q', 'foo.bar', '-r', './test/fake_env.rb'])
104
- assert_equal ['foo.bar'], Sidekiq.options[:queues]
105
- end
106
-
107
- it 'sets verbose' do
108
- old = Sidekiq.logger.level
109
- @cli.parse(['sidekiq', '-v', '-r', './test/fake_env.rb'])
110
- assert_equal Logger::DEBUG, Sidekiq.logger.level
111
- # If we leave the logger at DEBUG it'll add a lot of noise to the test output
112
- Sidekiq.options.delete(:verbose)
113
- Sidekiq.logger.level = old
114
- end
115
-
116
- describe 'with logfile' do
117
- before do
118
- @old_logger = Sidekiq.logger
119
- @tmp_log_path = '/tmp/sidekiq.log'
120
- end
121
-
122
- after do
123
- Sidekiq.logger = @old_logger
124
- Sidekiq.options.delete(:logfile)
125
- File.unlink @tmp_log_path if File.exist?(@tmp_log_path)
126
- end
127
-
128
- it 'sets the logfile path' do
129
- @cli.parse(['sidekiq', '-L', @tmp_log_path, '-r', './test/fake_env.rb'])
130
-
131
- assert_equal @tmp_log_path, Sidekiq.options[:logfile]
132
- end
133
-
134
- it 'creates and writes to a logfile' do
135
- @cli.parse(['sidekiq', '-L', @tmp_log_path, '-r', './test/fake_env.rb'])
136
-
137
- Sidekiq.logger.info('test message')
138
-
139
- assert_match(/test message/, File.read(@tmp_log_path), "didn't include the log message")
140
- end
141
-
142
- it 'appends messages to a logfile' do
143
- File.open(@tmp_log_path, 'w') do |f|
144
- f.puts 'already existent log message'
145
- end
146
-
147
- @cli.parse(['sidekiq', '-L', @tmp_log_path, '-r', './test/fake_env.rb'])
148
-
149
- Sidekiq.logger.info('test message')
150
-
151
- log_file_content = File.read(@tmp_log_path)
152
- assert_match(/already existent/, log_file_content, "didn't include the old message")
153
- assert_match(/test message/, log_file_content, "didn't include the new message")
154
- end
155
- end
156
-
157
- describe 'with pidfile' do
158
- before do
159
- @tmp_file = Tempfile.new('sidekiq-test')
160
- @tmp_path = @tmp_file.path
161
- @tmp_file.close!
162
-
163
- @cli.parse(['sidekiq', '-P', @tmp_path, '-r', './test/fake_env.rb'])
164
- end
165
-
166
- after do
167
- File.unlink @tmp_path if File.exist? @tmp_path
168
- end
169
-
170
- it 'sets pidfile path' do
171
- assert_equal @tmp_path, Sidekiq.options[:pidfile]
172
- end
173
-
174
- it 'writes pidfile' do
175
- assert_equal File.read(@tmp_path).strip.to_i, Process.pid
176
- end
177
- end
178
-
179
- describe 'with config file' do
180
- before do
181
- @cli.parse(['sidekiq', '-C', './test/config.yml'])
182
- end
183
-
184
- it 'parses as expected' do
185
- assert_equal './test/config.yml', Sidekiq.options[:config_file]
186
- refute Sidekiq.options[:verbose]
187
- assert_equal './test/fake_env.rb', Sidekiq.options[:require]
188
- assert_nil Sidekiq.options[:environment]
189
- assert_equal 50, Sidekiq.options[:concurrency]
190
- assert_equal '/tmp/sidekiq-config-test.pid', Sidekiq.options[:pidfile]
191
- assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile]
192
- assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
193
- assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
194
- end
195
- end
196
-
197
- describe 'with env based config file' do
198
- before do
199
- @cli.parse(['sidekiq', '-e', 'staging', '-C', './test/env_based_config.yml'])
200
- end
201
-
202
- it 'parses as expected' do
203
- assert_equal './test/env_based_config.yml', Sidekiq.options[:config_file]
204
- refute Sidekiq.options[:verbose]
205
- assert_equal './test/fake_env.rb', Sidekiq.options[:require]
206
- assert_equal 'staging', Sidekiq.options[:environment]
207
- assert_equal 5, Sidekiq.options[:concurrency]
208
- assert_equal '/tmp/sidekiq-config-test.pid', Sidekiq.options[:pidfile]
209
- assert_equal '/tmp/sidekiq.log', Sidekiq.options[:logfile]
210
- assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
211
- assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
212
- end
213
- end
214
-
215
- describe 'with an empty config file' do
216
- before do
217
- @tmp_file = Tempfile.new('sidekiq-test')
218
- @tmp_path = @tmp_file.path
219
- @tmp_file.close!
220
- end
221
-
222
- after do
223
- File.unlink @tmp_path if File.exist? @tmp_path
224
- end
225
-
226
- it 'takes a path' do
227
- @cli.parse(['sidekiq', '-C', @tmp_path])
228
- assert_equal @tmp_path, Sidekiq.options[:config_file]
229
- end
230
-
231
- it 'should have an identical options hash, except for config_file' do
232
- @cli.parse(['sidekiq'])
233
- old_options = Sidekiq.options.clone
234
-
235
- @cli.parse(['sidekiq', '-C', @tmp_path])
236
- new_options = Sidekiq.options.clone
237
- refute_equal old_options, new_options
238
-
239
- new_options.delete(:config_file)
240
- assert_equal old_options, new_options
241
- end
242
- end
243
-
244
- describe 'with config file and flags' do
245
- before do
246
- # We need an actual file here.
247
- @tmp_lib_path = '/tmp/require-me.rb'
248
- File.open(@tmp_lib_path, 'w') do |f|
249
- f.puts "# do work"
250
- end
251
-
252
- @tmp_file = Tempfile.new('sidekiqr')
253
- @tmp_path = @tmp_file.path
254
- @tmp_file.close!
255
-
256
- @cli.parse(['sidekiq',
257
- '-C', './test/config.yml',
258
- '-e', 'snoop',
259
- '-c', '100',
260
- '-r', @tmp_lib_path,
261
- '-P', @tmp_path,
262
- '-q', 'often,7',
263
- '-q', 'seldom,3'])
264
- end
265
-
266
- after do
267
- File.unlink @tmp_lib_path if File.exist? @tmp_lib_path
268
- File.unlink @tmp_path if File.exist? @tmp_path
269
- end
270
-
271
- it 'gives the expected options' do
272
- assert_equal 100, Sidekiq.options[:concurrency]
273
- assert_equal @tmp_lib_path, Sidekiq.options[:require]
274
- assert_equal 'snoop', Sidekiq.options[:environment]
275
- assert_equal @tmp_path, Sidekiq.options[:pidfile]
276
- assert_equal 7, Sidekiq.options[:queues].count { |q| q == 'often' }
277
- assert_equal 3, Sidekiq.options[:queues].count { |q| q == 'seldom' }
278
- end
279
- end
280
-
281
- describe 'Sidekiq::CLI#parse_queues' do
282
- describe 'when weight is present' do
283
- it 'concatenates queues by factor of weight and sets strict to false' do
284
- opts = { strict: true }
285
- @cli.__send__ :parse_queues, opts, [['often', 7], ['repeatedly', 3]]
286
- @cli.__send__ :parse_queues, opts, [['once']]
287
- assert_equal (%w[often] * 7 + %w[repeatedly] * 3 + %w[once]), opts[:queues]
288
- assert !opts[:strict]
289
- end
290
- end
291
-
292
- describe 'when weight is not present' do
293
- it 'returns queues and sets strict' do
294
- opts = { strict: true }
295
- @cli.__send__ :parse_queues, opts, [['once'], ['one_time']]
296
- @cli.__send__ :parse_queues, opts, [['einmal']]
297
- assert_equal %w[once one_time einmal], opts[:queues]
298
- assert opts[:strict]
299
- end
300
- end
301
- end
302
-
303
- describe 'Sidekiq::CLI#parse_queue' do
304
- describe 'when weight is present' do
305
- it 'concatenates queue to opts[:queues] weight number of times and sets strict to false' do
306
- opts = { strict: true }
307
- @cli.__send__ :parse_queue, opts, 'often', 7
308
- assert_equal %w[often] * 7, opts[:queues]
309
- assert !opts[:strict]
310
- end
311
- end
312
-
313
- describe 'when weight is not present' do
314
- it 'concatenates queue to opts[:queues] once and leaves strict true' do
315
- opts = { strict: true }
316
- @cli.__send__ :parse_queue, opts, 'once', nil
317
- assert_equal %w[once], opts[:queues]
318
- assert opts[:strict]
319
- end
320
- end
321
- end
322
- end
323
-
324
- describe 'misc' do
325
- before do
326
- @cli = Sidekiq::CLI.new
327
- end
328
-
329
- it 'handles interrupts' do
330
- assert_raises Interrupt do
331
- @cli.handle_signal('INT')
332
- end
333
- assert_raises Interrupt do
334
- @cli.handle_signal('TERM')
335
- end
336
- end
337
-
338
- describe 'handles USR1 and USR2' do
339
- before do
340
- @tmp_log_path = '/tmp/sidekiq.log'
341
- @cli.parse(['sidekiq', '-L', @tmp_log_path, '-r', './test/fake_env.rb'])
342
- end
343
-
344
- after do
345
- File.unlink @tmp_log_path if File.exist? @tmp_log_path
346
- end
347
-
348
- it 'shuts down the worker' do
349
- count = 0
350
- Sidekiq.options[:lifecycle_events][:quiet] = [proc {
351
- count += 1
352
- }]
353
- @cli.launcher = Sidekiq::Launcher.new(Sidekiq.options)
354
- @cli.handle_signal('USR1')
355
-
356
- assert_equal 1, count
357
- end
358
-
359
- it 'reopens logs' do
360
- mock = MiniTest::Mock.new
361
- # reopen_logs returns number of files reopened so mock that
362
- mock.expect(:call, 1)
363
-
364
- Sidekiq::Logging.stub(:reopen_logs, mock) do
365
- @cli.handle_signal('USR2')
366
- end
367
- mock.verify
368
- end
369
- end
370
-
371
- describe 'handles TTIN' do
372
- before do
373
- @tmp_log_path = '/tmp/sidekiq.log'
374
- @cli.parse(['sidekiq', '-L', @tmp_log_path, '-r', './test/fake_env.rb'])
375
- @mock_thread = MiniTest::Mock.new
376
- @mock_thread.expect(:[], 'interrupt_test', ['sidekiq_label'])
377
- end
378
-
379
- after do
380
- File.unlink @tmp_log_path if File.exist? @tmp_log_path
381
- end
382
-
383
- describe 'with backtrace' do
384
- it 'logs backtrace' do
385
- 2.times { @mock_thread.expect(:backtrace, ['something went wrong']) }
386
-
387
- Thread.stub(:list, [@mock_thread]) do
388
- @cli.handle_signal('TTIN')
389
- assert_match(/something went wrong/, File.read(@tmp_log_path), "didn't include the log message")
390
- end
391
- end
392
- end
393
-
394
- describe 'without backtrace' do
395
- it 'logs no backtrace available' do
396
- @mock_thread.expect(:backtrace, nil)
397
-
398
- Thread.stub(:list, [@mock_thread]) do
399
- @cli.handle_signal('TTIN')
400
- assert_match(/no backtrace available/, File.read(@tmp_log_path), "didn't include the log message")
401
- end
402
- end
403
- end
404
- end
405
-
406
-
407
- it 'can fire events' do
408
- count = 0
409
- Sidekiq.options[:lifecycle_events][:startup] = [proc {
410
- count += 1
411
- }]
412
- cli = Sidekiq::CLI.new
413
- cli.fire_event(:startup)
414
- assert_equal 1, count
415
- end
416
- end
417
-
418
- end
@@ -1,266 +0,0 @@
1
- # frozen_string_literal: true
2
- require_relative 'helper'
3
-
4
- class TestClient < Sidekiq::Test
5
- describe 'errors' do
6
- it 'raises ArgumentError with invalid params' do
7
- assert_raises ArgumentError do
8
- Sidekiq::Client.push('foo', 1)
9
- end
10
-
11
- assert_raises ArgumentError do
12
- Sidekiq::Client.push('foo', :class => 'Foo', :noargs => [1, 2])
13
- end
14
-
15
- assert_raises ArgumentError do
16
- Sidekiq::Client.push('queue' => 'foo', 'class' => MyWorker, 'noargs' => [1, 2])
17
- end
18
-
19
- assert_raises ArgumentError do
20
- Sidekiq::Client.push('queue' => 'foo', 'class' => 42, 'args' => [1, 2])
21
- end
22
-
23
- assert_raises ArgumentError do
24
- Sidekiq::Client.push('queue' => 'foo', 'class' => MyWorker, 'args' => 1)
25
- end
26
- end
27
- end
28
-
29
- describe 'as instance' do
30
- it 'can push' do
31
- client = Sidekiq::Client.new
32
- jid = client.push('class' => 'Blah', 'args' => [1,2,3])
33
- assert_equal 24, jid.size
34
- end
35
-
36
- it 'allows middleware to stop bulk jobs' do
37
- mware = Class.new do
38
- def call(worker_klass,msg,q,r)
39
- msg['args'][0] == 1 ? yield : false
40
- end
41
- end
42
- client = Sidekiq::Client.new
43
- client.middleware do |chain|
44
- chain.add mware
45
- end
46
- q = Sidekiq::Queue.new
47
- q.clear
48
- result = client.push_bulk('class' => 'Blah', 'args' => [[1],[2],[3]])
49
- assert_equal 1, result.size
50
- assert_equal 1, q.size
51
- end
52
-
53
- it 'allows local middleware modification' do
54
- $called = false
55
- mware = Class.new { def call(worker_klass,msg,q,r); $called = true; msg;end }
56
- client = Sidekiq::Client.new
57
- client.middleware do |chain|
58
- chain.add mware
59
- end
60
- client.push('class' => 'Blah', 'args' => [1,2,3])
61
-
62
- assert $called
63
- assert client.middleware.exists?(mware)
64
- refute Sidekiq.client_middleware.exists?(mware)
65
- end
66
- end
67
-
68
- describe 'client' do
69
- it 'pushes messages to redis' do
70
- q = Sidekiq::Queue.new('foo')
71
- pre = q.size
72
- jid = Sidekiq::Client.push('queue' => 'foo', 'class' => MyWorker, 'args' => [1, 2])
73
- assert jid
74
- assert_equal 24, jid.size
75
- assert_equal pre + 1, q.size
76
- end
77
-
78
- it 'pushes messages to redis using a String class' do
79
- q = Sidekiq::Queue.new('foo')
80
- pre = q.size
81
- jid = Sidekiq::Client.push('queue' => 'foo', 'class' => 'MyWorker', 'args' => [1, 2])
82
- assert jid
83
- assert_equal 24, jid.size
84
- assert_equal pre + 1, q.size
85
- end
86
-
87
- class MyWorker
88
- include Sidekiq::Worker
89
- end
90
-
91
- class QueuedWorker
92
- include Sidekiq::Worker
93
- sidekiq_options :queue => :flimflam
94
- end
95
-
96
- it 'enqueues' do
97
- Sidekiq.redis {|c| c.flushdb }
98
- assert_equal Sidekiq.default_worker_options, MyWorker.get_sidekiq_options
99
- assert MyWorker.perform_async(1, 2)
100
- assert Sidekiq::Client.enqueue(MyWorker, 1, 2)
101
- assert Sidekiq::Client.enqueue_to(:custom_queue, MyWorker, 1, 2)
102
- assert_equal 1, Sidekiq::Queue.new('custom_queue').size
103
- assert Sidekiq::Client.enqueue_to_in(:custom_queue, 3.minutes, MyWorker, 1, 2)
104
- assert Sidekiq::Client.enqueue_to_in(:custom_queue, -3.minutes, MyWorker, 1, 2)
105
- assert_equal 2, Sidekiq::Queue.new('custom_queue').size
106
- assert Sidekiq::Client.enqueue_in(3.minutes, MyWorker, 1, 2)
107
- assert QueuedWorker.perform_async(1, 2)
108
- assert_equal 1, Sidekiq::Queue.new('flimflam').size
109
- end
110
- end
111
-
112
- describe 'bulk' do
113
- after do
114
- Sidekiq::Queue.new.clear
115
- end
116
- it 'can push a large set of jobs at once' do
117
- jids = Sidekiq::Client.push_bulk('class' => QueuedWorker, 'args' => (1..1_000).to_a.map { |x| Array(x) })
118
- assert_equal 1_000, jids.size
119
- end
120
- it 'can push a large set of jobs at once using a String class' do
121
- jids = Sidekiq::Client.push_bulk('class' => 'QueuedWorker', 'args' => (1..1_000).to_a.map { |x| Array(x) })
122
- assert_equal 1_000, jids.size
123
- end
124
- it 'returns the jids for the jobs' do
125
- Sidekiq::Client.push_bulk('class' => 'QueuedWorker', 'args' => (1..2).to_a.map { |x| Array(x) }).each do |jid|
126
- assert_match(/[0-9a-f]{12}/, jid)
127
- end
128
- end
129
- it 'handles no jobs' do
130
- result = Sidekiq::Client.push_bulk('class' => 'QueuedWorker', 'args' => [])
131
- assert_equal 0, result.size
132
- end
133
- end
134
-
135
- class BaseWorker
136
- include Sidekiq::Worker
137
- sidekiq_options 'retry' => 'base'
138
- end
139
- class AWorker < BaseWorker
140
- end
141
- class BWorker < BaseWorker
142
- sidekiq_options 'retry' => 'b'
143
- end
144
- class CWorker < BaseWorker
145
- sidekiq_options 'retry' => 2
146
- end
147
-
148
- describe 'client middleware' do
149
- class Stopper
150
- def call(worker_class, job, queue, r)
151
- raise ArgumentError unless r
152
- yield if job['args'].first.odd?
153
- end
154
- end
155
-
156
- it 'can stop some of the jobs from pushing' do
157
- client = Sidekiq::Client.new
158
- client.middleware do |chain|
159
- chain.add Stopper
160
- end
161
-
162
- assert_nil client.push('class' => MyWorker, 'args' => [0])
163
- assert_match(/[0-9a-f]{12}/, client.push('class' => MyWorker, 'args' => [1]))
164
- client.push_bulk('class' => MyWorker, 'args' => [[0], [1]]).each do |jid|
165
- assert_match(/[0-9a-f]{12}/, jid)
166
- end
167
- end
168
- end
169
-
170
- describe 'inheritance' do
171
- it 'inherits sidekiq options' do
172
- assert_equal 'base', AWorker.get_sidekiq_options['retry']
173
- assert_equal 'b', BWorker.get_sidekiq_options['retry']
174
- end
175
- end
176
-
177
- describe 'sharding' do
178
- class DWorker < BaseWorker
179
- end
180
-
181
- it 'allows sidekiq_options to point to different Redi' do
182
- conn = MiniTest::Mock.new
183
- conn.expect(:multi, [0, 1])
184
- DWorker.sidekiq_options('pool' => ConnectionPool.new(size: 1) { conn })
185
- DWorker.perform_async(1,2,3)
186
- conn.verify
187
- end
188
-
189
- it 'allows #via to point to same Redi' do
190
- conn = MiniTest::Mock.new
191
- conn.expect(:multi, [0, 1])
192
- sharded_pool = ConnectionPool.new(size: 1) { conn }
193
- Sidekiq::Client.via(sharded_pool) do
194
- Sidekiq::Client.via(sharded_pool) do
195
- CWorker.perform_async(1,2,3)
196
- end
197
- end
198
- conn.verify
199
- end
200
-
201
- it 'allows #via to point to different Redi' do
202
- conn = MiniTest::Mock.new
203
- conn.expect(:multi, [0, 1])
204
- default = Sidekiq::Client.new.redis_pool
205
- sharded_pool = ConnectionPool.new(size: 1) { conn }
206
- Sidekiq::Client.via(sharded_pool) do
207
- CWorker.perform_async(1,2,3)
208
- assert_equal sharded_pool, Sidekiq::Client.new.redis_pool
209
- assert_raises RuntimeError do
210
- Sidekiq::Client.via(default) do
211
- # nothing
212
- end
213
- end
214
- end
215
- assert_equal default, Sidekiq::Client.new.redis_pool
216
- conn.verify
217
- end
218
-
219
- it 'allows Resque helpers to point to different Redi' do
220
- conn = MiniTest::Mock.new
221
- conn.expect(:multi, []) { |*args, &block| block.call }
222
- conn.expect(:zadd, 1, [String, Array])
223
- DWorker.sidekiq_options('pool' => ConnectionPool.new(size: 1) { conn })
224
- Sidekiq::Client.enqueue_in(10, DWorker, 3)
225
- conn.verify
226
- end
227
- end
228
-
229
- describe 'Sidekiq::Worker#set' do
230
- class SetWorker
231
- include Sidekiq::Worker
232
- sidekiq_options :queue => :foo, 'retry' => 12
233
- end
234
-
235
- def setup
236
- Sidekiq.redis {|c| c.flushdb }
237
- end
238
-
239
- it 'allows option overrides' do
240
- q = Sidekiq::Queue.new('bar')
241
- assert_equal 0, q.size
242
- assert SetWorker.set(queue: :bar).perform_async(1)
243
- job = q.first
244
- assert_equal 'bar', job['queue']
245
- assert_equal 12, job['retry']
246
- end
247
-
248
- it 'handles symbols and strings' do
249
- q = Sidekiq::Queue.new('bar')
250
- assert_equal 0, q.size
251
- assert SetWorker.set('queue' => 'bar', :retry => 11).perform_async(1)
252
- job = q.first
253
- assert_equal 'bar', job['queue']
254
- assert_equal 11, job['retry']
255
-
256
- q.clear
257
- assert SetWorker.perform_async(1)
258
- assert_equal 0, q.size
259
-
260
- q = Sidekiq::Queue.new('foo')
261
- job = q.first
262
- assert_equal 'foo', job['queue']
263
- assert_equal 12, job['retry']
264
- end
265
- end
266
- end