/
test_cli.rb
559 lines (451 loc) · 19.1 KB
/
test_cli.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# frozen_string_literal: true
require_relative 'helper'
require 'sidekiq/cli'
describe Sidekiq::CLI do
describe '#parse' do
before do
Sidekiq.options = Sidekiq::DEFAULTS.dup
@logger = Sidekiq.logger
@logdev = StringIO.new
Sidekiq.logger = Logger.new(@logdev)
end
after do
Sidekiq.logger = @logger
end
subject { Sidekiq::CLI.new }
def logdev
@logdev ||= StringIO.new
end
describe '#parse' do
describe 'options' do
describe 'require' do
it 'accepts with -r' do
subject.parse(%w[sidekiq -r ./test/fake_env.rb])
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
end
end
describe 'concurrency' do
it 'accepts with -c' do
subject.parse(%w[sidekiq -c 60 -r ./test/fake_env.rb])
assert_equal 60, Sidekiq.options[:concurrency]
end
describe 'when concurrency is empty and RAILS_MAX_THREADS env var is set' do
before do
ENV['RAILS_MAX_THREADS'] = '9'
end
after do
ENV.delete('RAILS_MAX_THREADS')
end
it 'sets concurrency from RAILS_MAX_THREADS env var' do
subject.parse(%w[sidekiq -r ./test/fake_env.rb])
assert_equal 9, Sidekiq.options[:concurrency]
end
it 'option overrides RAILS_MAX_THREADS env var' do
subject.parse(%w[sidekiq -c 60 -r ./test/fake_env.rb])
assert_equal 60, Sidekiq.options[:concurrency]
end
end
end
describe 'setting internal options via the config file' do
describe 'setting the `strict` option via the config file' do
it 'discards the `strict` option specified via the config file' do
subject.parse(%w[sidekiq -C ./test/config_with_internal_options.yml])
assert_equal true, !!Sidekiq.options[:strict]
end
end
end
describe 'queues' do
it 'accepts with -q' do
subject.parse(%w[sidekiq -q foo -r ./test/fake_env.rb])
assert_equal ['foo'], Sidekiq.options[:queues]
end
describe 'when weights are not present' do
it 'accepts queues without weights' do
subject.parse(%w[sidekiq -q foo -q bar -r ./test/fake_env.rb])
assert_equal ['foo', 'bar'], Sidekiq.options[:queues]
end
it 'sets strictly ordered queues' do
subject.parse(%w[sidekiq -q foo -q bar -r ./test/fake_env.rb])
assert_equal true, !!Sidekiq.options[:strict]
end
end
describe 'when weights are present' do
it 'accepts queues with weights' do
subject.parse(%w[sidekiq -q foo,3 -q bar -r ./test/fake_env.rb])
assert_equal ['foo', 'foo', 'foo', 'bar'], Sidekiq.options[:queues]
end
it 'does not set strictly ordered queues' do
subject.parse(%w[sidekiq -q foo,3 -q bar -r ./test/fake_env.rb])
assert_equal false, !!Sidekiq.options[:strict]
end
end
it 'accepts queues with multi-word names' do
subject.parse(%w[sidekiq -q queue_one -q queue-two -r ./test/fake_env.rb])
assert_equal ['queue_one', 'queue-two'], Sidekiq.options[:queues]
end
it 'accepts queues with dots in the name' do
subject.parse(%w[sidekiq -q foo.bar -r ./test/fake_env.rb])
assert_equal ['foo.bar'], Sidekiq.options[:queues]
end
describe 'when duplicate queue names' do
it 'raises an argument error' do
assert_raises(ArgumentError) { subject.parse(%w[sidekiq -q foo -q foo -r ./test/fake_env.rb]) }
assert_raises(ArgumentError) { subject.parse(%w[sidekiq -q foo,3 -q foo,1 -r ./test/fake_env.rb]) }
end
end
describe 'when queues are empty' do
describe 'when no queues are specified via -q' do
it "sets 'default' queue" do
subject.parse(%w[sidekiq -r ./test/fake_env.rb])
assert_equal ['default'], Sidekiq.options[:queues]
end
end
describe 'when no queues are specified via the config file' do
it "sets 'default' queue" do
subject.parse(%w[sidekiq -C ./test/config_empty.yml -r ./test/fake_env.rb])
assert_equal ['default'], Sidekiq.options[:queues]
end
end
end
end
describe 'timeout' do
it 'accepts with -t' do
subject.parse(%w[sidekiq -t 30 -r ./test/fake_env.rb])
assert_equal 30, Sidekiq.options[:timeout]
end
end
describe 'verbose' do
it 'accepts with -v' do
subject.parse(%w[sidekiq -v -r ./test/fake_env.rb])
assert_equal Logger::DEBUG, Sidekiq.logger.level
end
end
describe 'config file' do
it 'accepts with -C' do
subject.parse(%w[sidekiq -C ./test/config.yml])
assert_equal './test/config.yml', Sidekiq.options[:config_file]
refute Sidekiq.options[:verbose]
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_nil Sidekiq.options[:environment]
assert_equal 50, Sidekiq.options[:concurrency]
assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
end
it 'accepts stringy keys' do
subject.parse(%w[sidekiq -C ./test/config_string.yml])
assert_equal './test/config_string.yml', Sidekiq.options[:config_file]
refute Sidekiq.options[:verbose]
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_nil Sidekiq.options[:environment]
assert_equal 50, Sidekiq.options[:concurrency]
assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
end
it 'accepts environment specific config' do
subject.parse(%w[sidekiq -e staging -C ./test/config_environment.yml])
assert_equal './test/config_environment.yml', Sidekiq.options[:config_file]
refute Sidekiq.options[:verbose]
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_equal 'staging', Sidekiq.options[:environment]
assert_equal 50, Sidekiq.options[:concurrency]
assert_equal 2, Sidekiq.options[:queues].count { |q| q == 'very_often' }
assert_equal 1, Sidekiq.options[:queues].count { |q| q == 'seldom' }
end
end
describe 'default config file' do
describe 'when required path is a directory' do
it 'tries config/sidekiq.yml from required diretory' do
subject.parse(%w[sidekiq -r ./test/dummy])
assert_equal './test/dummy/config/sidekiq.yml', Sidekiq.options[:config_file]
assert_equal 25, Sidekiq.options[:concurrency]
end
end
describe 'when required path is a file' do
it 'tries config/sidekiq.yml from current diretory' do
Sidekiq.options[:require] = './test/dummy' # stub current dir – ./
subject.parse(%w[sidekiq -r ./test/fake_env.rb])
assert_equal './test/dummy/config/sidekiq.yml', Sidekiq.options[:config_file]
assert_equal 25, Sidekiq.options[:concurrency]
end
end
describe 'without any required path' do
it 'tries config/sidekiq.yml from current diretory' do
Sidekiq.options[:require] = './test/dummy' # stub current dir – ./
subject.parse(%w[sidekiq])
assert_equal './test/dummy/config/sidekiq.yml', Sidekiq.options[:config_file]
assert_equal 25, Sidekiq.options[:concurrency]
end
end
describe 'when config file and flags' do
it 'merges options' do
subject.parse(%w[sidekiq -C ./test/config.yml
-e snoop
-c 100
-r ./test/fake_env.rb
-q often,7
-q seldom,3])
assert_equal './test/config.yml', Sidekiq.options[:config_file]
refute Sidekiq.options[:verbose]
assert_equal './test/fake_env.rb', Sidekiq.options[:require]
assert_equal 'snoop', Sidekiq.options[:environment]
assert_equal 100, Sidekiq.options[:concurrency]
assert_equal 7, Sidekiq.options[:queues].count { |q| q == 'often' }
assert_equal 3, Sidekiq.options[:queues].count { |q| q == 'seldom' }
end
describe 'when the config file specifies queues with weights' do
describe 'when -q specifies queues without weights' do
it 'sets strictly ordered queues' do
subject.parse(%w[sidekiq -C ./test/config.yml
-r ./test/fake_env.rb
-q foo -q bar])
assert_equal true, !!Sidekiq.options[:strict]
end
end
describe 'when -q specifies no queues' do
it 'does not set strictly ordered queues' do
subject.parse(%w[sidekiq -C ./test/config.yml
-r ./test/fake_env.rb])
assert_equal false, !!Sidekiq.options[:strict]
end
end
describe 'when -q specifies queues with weights' do
it 'does not set strictly ordered queues' do
subject.parse(%w[sidekiq -C ./test/config.yml
-r ./test/fake_env.rb
-q foo,2 -q bar,3])
assert_equal false, !!Sidekiq.options[:strict]
end
end
end
describe 'when the config file specifies queues without weights' do
describe 'when -q specifies queues without weights' do
it 'sets strictly ordered queues' do
subject.parse(%w[sidekiq -C ./test/config_queues_without_weights.yml
-r ./test/fake_env.rb
-q foo -q bar])
assert_equal true, !!Sidekiq.options[:strict]
end
end
describe 'when -q specifies no queues' do
it 'sets strictly ordered queues' do
subject.parse(%w[sidekiq -C ./test/config_queues_without_weights.yml
-r ./test/fake_env.rb])
assert_equal true, !!Sidekiq.options[:strict]
end
end
describe 'when -q specifies queues with weights' do
it 'does not set strictly ordered queues' do
subject.parse(%w[sidekiq -C ./test/config_queues_without_weights.yml
-r ./test/fake_env.rb
-q foo,2 -q bar,3])
assert_equal false, !!Sidekiq.options[:strict]
end
end
end
describe 'when the config file specifies no queues' do
describe 'when -q specifies queues without weights' do
it 'sets strictly ordered queues' do
subject.parse(%w[sidekiq -C ./test/config_empty.yml
-r ./test/fake_env.rb
-q foo -q bar])
assert_equal true, !!Sidekiq.options[:strict]
end
end
describe 'when -q specifies no queues' do
it 'sets strictly ordered queues' do
subject.parse(%w[sidekiq -C ./test/config_empty.yml
-r ./test/fake_env.rb])
assert_equal true, !!Sidekiq.options[:strict]
end
end
describe 'when -q specifies queues with weights' do
it 'does not set strictly ordered queues' do
subject.parse(%w[sidekiq -C ./test/config_empty.yml
-r ./test/fake_env.rb
-q foo,2 -q bar,3])
assert_equal false, !!Sidekiq.options[:strict]
end
end
end
end
describe 'default config file' do
describe 'when required path is a directory' do
it 'tries config/sidekiq.yml' do
subject.parse(%w[sidekiq -r ./test/dummy])
assert_equal 'sidekiq.yml', File.basename(Sidekiq.options[:config_file])
assert_equal 25, Sidekiq.options[:concurrency]
end
end
end
end
end
describe 'validation' do
describe 'when required application path does not exist' do
it 'exits with status 1' do
exit = assert_raises(SystemExit) { subject.parse(%w[sidekiq -r /non/existent/path]) }
assert_equal 1, exit.status
end
end
describe 'when required path is a directory without config/application.rb' do
it 'exits with status 1' do
exit = assert_raises(SystemExit) { subject.parse(%w[sidekiq -r ./test/fixtures]) }
assert_equal 1, exit.status
end
describe 'when config file path does not exist' do
it 'raises argument error' do
assert_raises(ArgumentError) do
subject.parse(%w[sidekiq -r ./test/fake_env.rb -C /non/existent/path])
end
end
end
end
describe 'when concurrency is not valid' do
describe 'when set to 0' do
it 'raises argument error' do
assert_raises(ArgumentError) do
subject.parse(%w[sidekiq -r ./test/fake_env.rb -c 0])
end
end
end
describe 'when set to a negative number' do
it 'raises argument error' do
assert_raises(ArgumentError) do
subject.parse(%w[sidekiq -r ./test/fake_env.rb -c -2])
end
end
end
end
describe 'when timeout is not valid' do
describe 'when set to 0' do
it 'raises argument error' do
assert_raises(ArgumentError) do
subject.parse(%w[sidekiq -r ./test/fake_env.rb -t 0])
end
end
end
describe 'when set to a negative number' do
it 'raises argument error' do
assert_raises(ArgumentError) do
subject.parse(%w[sidekiq -r ./test/fake_env.rb -t -2])
end
end
end
end
end
end
describe '#run' do
before do
Sidekiq.options[:concurrency] = 2
Sidekiq.options[:require] = './test/fake_env.rb'
end
describe 'require workers' do
describe 'when path is a rails directory' do
before do
Sidekiq.options[:require] = './test/dummy'
subject.environment = 'test'
end
it 'requires sidekiq railtie and rails application with environment' do
subject.stub(:launch, nil) do
subject.run
end
assert defined?(Sidekiq::Rails)
assert defined?(Dummy::Application)
end
it 'tags with the app directory name' do
subject.stub(:launch, nil) do
subject.run
end
assert_equal 'dummy', Sidekiq.options[:tag]
end
end
describe 'when path is file' do
it 'requires application' do
subject.stub(:launch, nil) do
subject.run
end
assert $LOADED_FEATURES.any? { |x| x =~ /test\/fake_env/ }
end
end
end
describe 'when development environment and stdout tty' do
it 'prints banner' do
subject.stub(:environment, 'development') do
assert_output(/#{Regexp.escape(Sidekiq::CLI.banner)}/) do
$stdout.stub(:tty?, true) do
subject.stub(:launch, nil) do
subject.run
end
end
end
end
end
end
it 'prints rails info' do
subject.stub(:environment, 'production') do
subject.stub(:launch, nil) do
subject.run
end
assert_includes @logdev.string, "Booted Rails #{::Rails.version} application in production environment"
end
end
describe 'checking maxmemory policy' do
it 'warns if the policy is not noeviction' do
redis_info = { "maxmemory_policy" => "allkeys-lru", "redis_version" => "6" }
Sidekiq.stub(:redis_info, redis_info) do
subject.stub(:launch, nil) do
subject.run
end
end
assert_includes @logdev.string, "'noeviction' maxmemory policy is recommended (current policy: 'allkeys-lru')"
end
it 'silent if the policy is noeviction' do
redis_info = { "maxmemory_policy" => "noeviction", "redis_version" => "6" }
Sidekiq.stub(:redis_info, redis_info) do
subject.stub(:launch, nil) do
subject.run
end
end
refute_includes @logdev.string, "'noeviction' maxmemory policy is recommended"
end
end
end
describe 'signal handling' do
%w(INT TERM).each do |sig|
describe sig do
it 'raises interrupt error' do
assert_raises Interrupt do
subject.handle_signal(sig)
end
end
end
end
describe "TSTP" do
it 'quiets with a corresponding event' do
quiet = false
Sidekiq.on(:quiet) do
quiet = true
end
subject.launcher = Sidekiq::Launcher.new(Sidekiq.options)
subject.handle_signal("TSTP")
assert_match(/Got TSTP signal/, logdev.string)
assert_equal true, quiet
end
end
describe 'TTIN' do
it 'prints backtraces for all threads in the process to the logfile' do
subject.handle_signal('TTIN')
assert_match(/Got TTIN signal/, logdev.string)
assert_match(/\bbacktrace\b/, logdev.string)
end
end
describe 'UNKNOWN' do
it 'logs about' do
# subject.parse(%w[sidekiq -r ./test/fake_env.rb])
subject.handle_signal('UNKNOWN')
assert_match(/Got UNKNOWN signal/, logdev.string)
assert_match(/No signal handler registered/, logdev.string)
end
end
end
end
end