sidekiq-cron 1.9.1 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,1258 +0,0 @@
1
- require './test/test_helper'
2
-
3
- describe "Cron Job" do
4
- before do
5
- # Clear all previous saved data from Redis.
6
- Sidekiq.redis do |conn|
7
- conn.keys("cron_job*").each do |key|
8
- conn.del(key)
9
- end
10
- end
11
-
12
- # Clear all queues.
13
- Sidekiq::Queue.all.each do |queue|
14
- queue.clear
15
- end
16
- end
17
-
18
- it "be initialized" do
19
- job = Sidekiq::Cron::Job.new()
20
- assert_nil job.last_enqueue_time
21
- assert job.is_a?(Sidekiq::Cron::Job)
22
- end
23
-
24
- describe "class methods" do
25
- it "have create method" do
26
- assert Sidekiq::Cron::Job.respond_to?(:create)
27
- end
28
-
29
- it "have destroy method" do
30
- assert Sidekiq::Cron::Job.respond_to?(:destroy)
31
- end
32
-
33
- it "have count" do
34
- assert Sidekiq::Cron::Job.respond_to?(:count)
35
- end
36
-
37
- it "have all" do
38
- assert Sidekiq::Cron::Job.respond_to?(:all)
39
- end
40
-
41
- it "have find" do
42
- assert Sidekiq::Cron::Job.respond_to?(:find)
43
- end
44
- end
45
-
46
- describe "instance methods" do
47
- before do
48
- @job = Sidekiq::Cron::Job.new()
49
- end
50
-
51
- it "have save method" do
52
- assert @job.respond_to?(:save)
53
- end
54
-
55
- it "have valid? method" do
56
- assert @job.respond_to?("valid?".to_sym)
57
- end
58
-
59
- it "have destroy method" do
60
- assert @job.respond_to?(:destroy)
61
- end
62
-
63
- it "have enabled? method" do
64
- assert @job.respond_to?(:enabled?)
65
- end
66
-
67
- it "have disabled? method" do
68
- assert @job.respond_to?(:disabled?)
69
- end
70
-
71
- it 'have sort_name - used for sorting enabled disbaled jobs on frontend' do
72
- job = Sidekiq::Cron::Job.new(name: "TestName")
73
- assert_equal job.sort_name, "0_testname"
74
- end
75
- end
76
-
77
- describe "invalid job" do
78
- before do
79
- @job = Sidekiq::Cron::Job.new()
80
- end
81
-
82
- it "allow a class instance for the klass" do
83
- @job.klass = CronTestClass
84
-
85
- refute @job.valid?
86
- refute @job.errors.any?{|e| e.include?("klass")}, "Should not have error for klass"
87
- end
88
-
89
- it "return false on valid? and errors" do
90
- refute @job.valid?
91
- assert @job.errors.is_a?(Array)
92
-
93
- assert @job.errors.any?{|e| e.include?("name")}, "Should have error for name"
94
- assert @job.errors.any?{|e| e.include?("cron")}, "Should have error for cron"
95
- assert @job.errors.any?{|e| e.include?("klass")}, "Should have error for klass"
96
- end
97
-
98
- it "return false on valid? with invalid cron" do
99
- @job.cron = "* s *"
100
- refute @job.valid?
101
- assert @job.errors.is_a?(Array)
102
- assert @job.errors.any?{|e| e.include?("cron")}, "Should have error for cron"
103
- end
104
-
105
- it "return false on save" do
106
- refute @job.save
107
- end
108
- end
109
-
110
- describe "new" do
111
- before do
112
- @args = {
113
- name: "Test",
114
- cron: "* * * * *"
115
- }
116
- @job = Sidekiq::Cron::Job.new(@args)
117
- end
118
-
119
- it "have all setted attributes" do
120
- @args.each do |key, value|
121
- assert_equal @job.send(key), value, "New job should have #{key} with value #{value} but it has: #{@job.send(key)}"
122
- end
123
- end
124
-
125
- it "have to_hash method" do
126
- [:name,:klass,:cron,:description,:args,:message,:status].each do |key|
127
- assert @job.to_hash.has_key?(key), "to_hash must have key: #{key}"
128
- end
129
- end
130
- end
131
-
132
- describe 'cron formats' do
133
- before do
134
- @args = {
135
- name: "Test",
136
- klass: "CronTestClass"
137
- }
138
- end
139
-
140
- it 'should support natural language format' do
141
- @args[:cron] = "every 3 hours"
142
- @job = Sidekiq::Cron::Job.new(@args)
143
- assert @job.valid?
144
- assert_equal Fugit::Cron.new("0 */3 * * *"), @job.send(:parsed_cron)
145
- end
146
- end
147
-
148
- describe 'parse_enqueue_time' do
149
- before do
150
- @args = {
151
- name: "Test",
152
- cron: "* * * * *"
153
- }
154
- @job = Sidekiq::Cron::Job.new(@args)
155
- end
156
-
157
- it 'should correctly parse new format' do
158
- assert_equal @job.send(:parse_enqueue_time, '2017-01-02 15:23:43 UTC'), Time.new(2017, 1, 2, 15, 23, 43, '+00:00')
159
- end
160
-
161
- it 'should correctly parse new format with different timezone' do
162
- assert_equal @job.send(:parse_enqueue_time, '2017-01-02 15:23:43 +01:00'), Time.new(2017, 1, 2, 15, 23, 43, '+01:00')
163
- end
164
-
165
- it 'should correctly parse old format' do
166
- assert_equal @job.send(:parse_enqueue_time, '2017-01-02 15:23:43'), Time.new(2017, 1, 2, 15, 23, 43, '+00:00')
167
- end
168
- end
169
-
170
- describe 'formatted time' do
171
- before do
172
- @args = {
173
- name: "Test",
174
- cron: "* * * * *"
175
- }
176
- @job = Sidekiq::Cron::Job.new(@args)
177
- @time = Time.new(2015, 1, 2, 3, 4, 5, '+01:00')
178
- end
179
-
180
- it 'returns formatted_last_time' do
181
- assert_equal '2015-01-02T02:04:00Z', @job.formatted_last_time(@time)
182
- end
183
-
184
- it 'returns formatted_enqueue_time' do
185
- assert_equal '1420164240.0', @job.formatted_enqueue_time(@time)
186
- end
187
- end
188
-
189
- describe "new with different class inputs" do
190
- it "be initialized by 'klass' and Class" do
191
- job = Sidekiq::Cron::Job.new('klass' => CronTestClass)
192
- assert_equal job.message['class'], 'CronTestClass'
193
- end
194
-
195
- it "be initialized by 'klass' and string Class" do
196
- job = Sidekiq::Cron::Job.new('klass' => 'CronTestClass')
197
- assert_equal job.message['class'], 'CronTestClass'
198
- end
199
-
200
- it "be initialized by 'class' and string Class" do
201
- job = Sidekiq::Cron::Job.new('class' => 'CronTestClass')
202
- assert_equal job.message['class'], 'CronTestClass'
203
- end
204
-
205
- it "be initialized by 'class' and Class" do
206
- job = Sidekiq::Cron::Job.new('class' => CronTestClass)
207
- assert_equal job.message['class'], 'CronTestClass'
208
- end
209
- end
210
-
211
- describe "new should find klass specific settings (queue, retry ...)" do
212
- it "nothing raise on unknown klass" do
213
- job = Sidekiq::Cron::Job.new('klass' => 'UnknownCronClass')
214
- assert_equal job.message, {"class"=>"UnknownCronClass", "args"=>[], "queue"=>"default"}
215
- end
216
-
217
- it "be initialized with default attributes" do
218
- job = Sidekiq::Cron::Job.new('klass' => 'CronTestClass')
219
- assert_equal job.message, {"retry"=>true, "queue"=>"default", "class"=>"CronTestClass", "args"=>[]}
220
- end
221
-
222
- it "be initialized with class specified attributes" do
223
- job = Sidekiq::Cron::Job.new('class' => 'CronTestClassWithQueue')
224
- assert_equal job.message, {"retry"=>false,
225
- "queue"=>:super,
226
- "backtrace"=>true,
227
- "class"=>"CronTestClassWithQueue",
228
- "args"=>[]}
229
- end
230
-
231
- it "be initialized with 'class' and overwrite queue by settings" do
232
- job = Sidekiq::Cron::Job.new('class' => CronTestClassWithQueue, queue: 'my_testing_queue')
233
-
234
- assert_equal job.message, {"retry"=>false,
235
- "queue"=>'my_testing_queue',
236
- "backtrace"=>true,
237
- "class"=>"CronTestClassWithQueue",
238
- "args"=>[]}
239
- end
240
-
241
- it "be initialized with 'class' and date_as_argument" do
242
- job = Sidekiq::Cron::Job.new('class' => 'CronTestClassWithQueue', "date_as_argument" => true)
243
-
244
- job_message = job.message
245
- job_args = job_message.delete("args")
246
- assert_equal job_message, {"retry"=>false,
247
- "queue"=>:super,
248
- "backtrace"=>true,
249
- "class"=>"CronTestClassWithQueue"}
250
- assert job_args.empty?
251
-
252
- enqueue_args = job.enqueue_args
253
- assert enqueue_args[-1].is_a?(Float)
254
- assert enqueue_args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
255
- end
256
-
257
- it "be initialized with 'class', 2 arguments and date_as_argument" do
258
- job = Sidekiq::Cron::Job.new('class' => 'CronTestClassWithQueue', "date_as_argument" => true, "args"=> ["arg1", :arg2])
259
-
260
- job_message = job.message
261
- job_args = job_message.delete("args")
262
- assert_equal job_message, {"retry"=>false,
263
- "queue"=>:super,
264
- "backtrace"=>true,
265
- "class"=>"CronTestClassWithQueue"}
266
- assert_equal job_args, ["arg1", :arg2]
267
-
268
- enqueue_args = job.enqueue_args
269
- assert_equal enqueue_args[0..-2], ["arg1", :arg2]
270
- assert enqueue_args[-1].is_a?(Float)
271
- assert enqueue_args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
272
- end
273
- end
274
-
275
- describe "cron test" do
276
- before do
277
- @job = Sidekiq::Cron::Job.new()
278
- end
279
-
280
- it "return previous minute" do
281
- @job.cron = "* * * * *"
282
- time = Time.new(2018, 8, 10, 13, 24, 56).utc
283
- assert_equal @job.last_time(time).strftime("%Y-%m-%d-%H-%M-%S"), time.strftime("%Y-%m-%d-%H-%M-00")
284
- end
285
-
286
- it "return previous hour" do
287
- @job.cron = "1 * * * *"
288
- time = Time.new(2018, 8, 10, 13, 24, 56).utc
289
- assert_equal @job.last_time(time).strftime("%Y-%m-%d-%H-%M-%S"), time.strftime("%Y-%m-%d-%H-01-00")
290
- end
291
-
292
- it "return previous day" do
293
- @job.cron = "1 2 * * * Etc/GMT"
294
- time = Time.new(2018, 8, 10, 13, 24, 56).utc
295
-
296
- if time.hour >= 2
297
- assert_equal @job.last_time(time).strftime("%Y-%m-%d-%H-%M-%S"), time.strftime("%Y-%m-%d-02-01-00")
298
- else
299
- yesterday = time - 1.day
300
- assert_equal @job.last_time(time).strftime("%Y-%m-%d-%H-%M-%S"), yesterday.strftime("%Y-%m-%d-02-01-00")
301
- end
302
- end
303
- end
304
-
305
- describe '#sidekiq_worker_message' do
306
- before do
307
- @args = {
308
- name: 'Test',
309
- cron: '* * * * *',
310
- queue: 'super_queue',
311
- klass: 'CronTestClass',
312
- args: { foo: 'bar' }
313
- }
314
- @job = Sidekiq::Cron::Job.new(@args)
315
- end
316
-
317
- it 'should return valid payload for Sidekiq::Client' do
318
- payload = {
319
- "retry" => true,
320
- "queue" => "super_queue",
321
- "class" => "CronTestClass",
322
- "args" => [{:foo=>"bar"}]
323
- }
324
- assert_equal @job.sidekiq_worker_message, payload
325
- end
326
-
327
- describe 'with date_as_argument' do
328
- before do
329
- @args.merge!(date_as_argument: true)
330
- @job = Sidekiq::Cron::Job.new(@args)
331
- end
332
-
333
- let(:args) { @job.sidekiq_worker_message['args'] }
334
-
335
- it 'should add timestamp to args' do
336
- assert_equal args[0], {foo: 'bar'}
337
- assert args[-1].is_a?(Float)
338
- assert args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
339
- end
340
- end
341
- end
342
-
343
- describe '#sidekiq_worker_message settings overwrite queue name' do
344
- before do
345
- @args = {
346
- name: 'Test',
347
- cron: '* * * * *',
348
- queue: 'super_queue',
349
- klass: 'CronTestClassWithQueue',
350
- args: { foo: 'bar' }
351
- }
352
- @job = Sidekiq::Cron::Job.new(@args)
353
- end
354
-
355
- it 'should return valid payload for Sidekiq::Client with overwrite queue name' do
356
- payload = {
357
- "retry" => false,
358
- "backtrace"=>true,
359
- "queue" => "super_queue",
360
- "class" => "CronTestClassWithQueue",
361
- "args" => [{:foo=>"bar"}]
362
- }
363
- assert_equal @job.sidekiq_worker_message, payload
364
- end
365
- end
366
-
367
- describe '#active_job_message' do
368
- before do
369
- SecureRandom.stubs(:uuid).returns('XYZ')
370
- ActiveJob::Base.queue_name_prefix = ''
371
-
372
- @args = {
373
- name: 'Test',
374
- cron: '* * * * *',
375
- klass: 'ActiveJobCronTestClass',
376
- queue: 'super_queue',
377
- description: nil,
378
- args: { foo: 'bar' }
379
- }
380
- @job = Sidekiq::Cron::Job.new(@args)
381
- end
382
-
383
- it 'should return valid payload for Sidekiq::Client' do
384
- payload = {
385
- 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
386
- 'wrapped' => 'ActiveJobCronTestClass',
387
- 'queue' => 'super_queue',
388
- 'description' => nil,
389
- 'args' => [{
390
- 'job_class' => 'ActiveJobCronTestClass',
391
- 'job_id' => 'XYZ',
392
- 'queue_name' => 'super_queue',
393
- 'arguments' => [{foo: 'bar'}]
394
- }]
395
- }
396
- assert_equal @job.active_job_message, payload
397
- end
398
-
399
- describe 'with date_as_argument' do
400
- before do
401
- @args.merge!(date_as_argument: true)
402
- @job = Sidekiq::Cron::Job.new(@args)
403
- end
404
-
405
- let(:args) { @job.active_job_message['args'][0]['arguments'] }
406
-
407
- it 'should add timestamp to args' do
408
- args = @job.active_job_message['args'][0]['arguments']
409
- assert_equal args[0], {foo: 'bar'}
410
- assert args[-1].is_a?(Float)
411
- assert args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
412
- end
413
- end
414
- end
415
-
416
- describe '#active_job_message - unknown Active Job Worker class' do
417
- before do
418
- SecureRandom.stubs(:uuid).returns('XYZ')
419
- ActiveJob::Base.queue_name_prefix = ''
420
-
421
- @args = {
422
- name: 'Test',
423
- cron: '* * * * *',
424
- klass: 'UnknownActiveJobCronTestClass',
425
- active_job: true,
426
- queue: 'super_queue',
427
- description: nil,
428
- args: { foo: 'bar' }
429
- }
430
- @job = Sidekiq::Cron::Job.new(@args)
431
- end
432
-
433
- it 'should return valid payload for Sidekiq::Client' do
434
- payload = {
435
- 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
436
- 'wrapped' => 'UnknownActiveJobCronTestClass',
437
- 'queue' => 'super_queue',
438
- 'description' => nil,
439
- 'args' => [{
440
- 'job_class' => 'UnknownActiveJobCronTestClass',
441
- 'job_id' => 'XYZ',
442
- 'queue_name' => 'super_queue',
443
- 'arguments' => [{foo: 'bar'}]
444
- }]
445
- }
446
- assert_equal @job.active_job_message, payload
447
- end
448
- end
449
-
450
- describe '#active_job_message with symbolize_args (hash)' do
451
- before do
452
- SecureRandom.stubs(:uuid).returns('XYZ')
453
- ActiveJob::Base.queue_name_prefix = ''
454
-
455
- @args = {
456
- name: 'Test',
457
- cron: '* * * * *',
458
- klass: 'ActiveJobCronTestClass',
459
- queue: 'super_queue',
460
- description: nil,
461
- symbolize_args: true,
462
- args: { 'foo' => 'bar' }
463
- }
464
- @job = Sidekiq::Cron::Job.new(@args)
465
- end
466
-
467
- it 'should return valid payload for Sidekiq::Client' do
468
- payload = {
469
- 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
470
- 'wrapped' => 'ActiveJobCronTestClass',
471
- 'queue' => 'super_queue',
472
- 'description' => nil,
473
- 'args' => [{
474
- 'job_class' => 'ActiveJobCronTestClass',
475
- 'job_id' => 'XYZ',
476
- 'queue_name' => 'super_queue',
477
- 'arguments' => [{foo: 'bar'}]
478
- }]
479
- }
480
- assert_equal @job.active_job_message, payload
481
- end
482
- end
483
-
484
- describe '#active_job_message with symbolize_args (array)' do
485
- before do
486
- SecureRandom.stubs(:uuid).returns('XYZ')
487
- ActiveJob::Base.queue_name_prefix = ''
488
-
489
- @args = {
490
- name: 'Test',
491
- cron: '* * * * *',
492
- klass: 'ActiveJobCronTestClass',
493
- queue: 'super_queue',
494
- description: nil,
495
- symbolize_args: true,
496
- args: [{ 'foo' => 'bar' }]
497
- }
498
- @job = Sidekiq::Cron::Job.new(@args)
499
- end
500
-
501
- it 'should return valid payload for Sidekiq::Client' do
502
- payload = {
503
- 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
504
- 'wrapped' => 'ActiveJobCronTestClass',
505
- 'queue' => 'super_queue',
506
- 'description' => nil,
507
- 'args' => [{
508
- 'job_class' => 'ActiveJobCronTestClass',
509
- 'job_id' => 'XYZ',
510
- 'queue_name' => 'super_queue',
511
- 'arguments' => [{foo: 'bar'}]
512
- }]
513
- }
514
- assert_equal @job.active_job_message, payload
515
- end
516
- end
517
-
518
- describe '#active_job_message with queue_name_prefix' do
519
- before do
520
- SecureRandom.stubs(:uuid).returns('XYZ')
521
- ActiveJob::Base.queue_name_prefix = "prefix"
522
-
523
- @args = {
524
- name: 'Test',
525
- cron: '* * * * *',
526
- klass: 'ActiveJobCronTestClass',
527
- queue: 'super_queue',
528
- queue_name_prefix: 'prefix',
529
- args: { foo: 'bar' }
530
- }
531
- @job = Sidekiq::Cron::Job.new(@args)
532
- end
533
-
534
- it 'should return valid payload for Sidekiq::Client' do
535
- payload = {
536
- 'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
537
- 'wrapped' => 'ActiveJobCronTestClass',
538
- 'queue' => 'prefix_super_queue',
539
- 'description' => nil,
540
- 'args' => [{
541
- 'job_class' => 'ActiveJobCronTestClass',
542
- 'job_id' => 'XYZ',
543
- 'queue_name' => 'prefix_super_queue',
544
- 'arguments' => [{foo: 'bar'}]
545
- }]
546
- }
547
- assert_equal @job.active_job_message, payload
548
- end
549
- end
550
-
551
- describe '#enque!' do
552
- describe 'active job' do
553
- before do
554
- @args = {
555
- name: 'Test',
556
- cron: '* * * * *',
557
- klass: 'ActiveJobCronTestClass'
558
- }
559
- @job = Sidekiq::Cron::Job.new(@args)
560
- end
561
-
562
- it 'pushes to queue active jobs message' do
563
- @job.expects(:enqueue_active_job)
564
- .returns(ActiveJobCronTestClass.new)
565
- @job.enque!
566
- end
567
-
568
- describe 'with date_as_argument' do
569
- before do
570
- @args.merge!(date_as_argument: true)
571
- @job = Sidekiq::Cron::Job.new(@args)
572
- end
573
-
574
- it 'should add timestamp to args' do
575
- ActiveJobCronTestClass.expects(:perform_later)
576
- .returns(ActiveJobCronTestClass.new)
577
- .with { |*args|
578
- assert args[-1].is_a?(Float)
579
- assert args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
580
- }
581
- @job.enque!
582
- end
583
- end
584
-
585
- describe 'with active_job == true' do
586
- before do
587
- @args.merge!(active_job: true)
588
- end
589
-
590
- describe 'with active_job job class' do
591
- before do
592
- @job = Sidekiq::Cron::Job.new(@args.merge(klass: 'ActiveJobCronTestClass'))
593
- end
594
-
595
- it 'enques via active_job interface' do
596
- @job.expects(:enqueue_active_job)
597
- .returns(ActiveJobCronTestClass.new)
598
- @job.enque!
599
- end
600
- end
601
-
602
- describe 'with non sidekiq job class' do
603
- before do
604
- @job = Sidekiq::Cron::Job.new(@args.merge(klass: 'CronTestClass'))
605
- end
606
-
607
- it 'enques via active_job interface' do
608
- @job.expects(:enqueue_active_job)
609
- .returns(ActiveJobCronTestClass.new)
610
- @job.enque!
611
- end
612
- end
613
- end
614
- end
615
-
616
- describe 'active job with queue_name_prefix' do
617
- before do
618
- @args = {
619
- name: 'Test',
620
- cron: '* * * * *',
621
- klass: 'ActiveJobCronTestClass',
622
- queue: 'cron'
623
- }
624
- @job = Sidekiq::Cron::Job.new(@args)
625
- end
626
-
627
- it 'pushes to queue active jobs message with queue_name_prefix' do
628
- @job.expects(:enqueue_active_job)
629
- .returns(ActiveJobCronTestClass.new)
630
- @job.enque!
631
- end
632
- end
633
-
634
- describe 'active job via configuration (bool: true) [unknown class]' do
635
- before do
636
- @args = {
637
- name: 'Test',
638
- cron: '* * * * *',
639
- klass: 'UnknownClass',
640
- active_job: true
641
- }
642
- @job = Sidekiq::Cron::Job.new(@args)
643
- end
644
-
645
- it 'pushes to queue active jobs message' do
646
- @job.expects(:active_job_message)
647
- .returns('class' => 'UnknownClass', 'args' => [])
648
- @job.enque!
649
- end
650
- end
651
-
652
- describe 'active job via configuration (string: true) [unknown class]' do
653
- before do
654
- @args = {
655
- name: 'Test',
656
- cron: '* * * * *',
657
- klass: 'UnknownClass',
658
- active_job: 'true'
659
- }
660
- @job = Sidekiq::Cron::Job.new(@args)
661
- end
662
-
663
- it 'pushes to queue active jobs message' do
664
- @job.expects(:active_job_message)
665
- .returns('class' => 'UnknownClass', 'args' => [])
666
- @job.enque!
667
- end
668
- end
669
-
670
- describe 'active job via configuration (string: yes) [unknown class]' do
671
- before do
672
- @args = {
673
- name: 'Test',
674
- cron: '* * * * *',
675
- klass: 'UnknownClass',
676
- active_job: 'yes'
677
- }
678
- @job = Sidekiq::Cron::Job.new(@args)
679
- end
680
-
681
- it 'pushes to queue active jobs message' do
682
- @job.expects(:active_job_message)
683
- .returns('class' => 'UnknownClass', 'args' => [])
684
- @job.enque!
685
- end
686
- end
687
-
688
- describe 'active job via configuration (number: 1) [unknown class]' do
689
- before do
690
- @args = {
691
- name: 'Test',
692
- cron: '* * * * *',
693
- klass: 'UnknownClass',
694
- active_job: 1
695
- }
696
- @job = Sidekiq::Cron::Job.new(@args)
697
- end
698
-
699
- it 'pushes to queue active jobs message' do
700
- @job.expects(:active_job_message)
701
- .returns('class' => 'UnknownClass', 'args' => [])
702
- @job.enque!
703
- end
704
- end
705
-
706
- describe 'active job via configuration with queue_name_prefix option [unknown class]' do
707
- before do
708
- @args = {
709
- name: 'Test',
710
- cron: '* * * * *',
711
- klass: 'UnknownClass',
712
- queue: 'cron',
713
- active_job: true,
714
- queue_name_prefix: 'prefix'
715
- }
716
- @job = Sidekiq::Cron::Job.new(@args)
717
- end
718
-
719
- it 'pushes to queue active jobs message with queue_name_prefix' do
720
- @job.expects(:active_job_message)
721
- .returns('class' => 'UnknownClass', 'args' => [], 'queue' => 'prefix_cron')
722
- @job.enque!
723
- end
724
- end
725
-
726
- describe 'sidekiq worker' do
727
- before do
728
- @args = {
729
- name: 'Test',
730
- cron: '* * * * *',
731
- klass: 'CronTestClass'
732
- }
733
- @job = Sidekiq::Cron::Job.new(@args)
734
- end
735
-
736
- it 'pushes to queue active jobs message' do
737
- @job.expects(:enqueue_sidekiq_worker)
738
- .returns(true)
739
- @job.enque!
740
- end
741
-
742
- describe 'with date_as_argument' do
743
- before do
744
- @args.merge!(date_as_argument: true)
745
- @job = Sidekiq::Cron::Job.new(@args)
746
- end
747
-
748
- it 'should add timestamp to args' do
749
- CronTestClass::Setter.any_instance
750
- .expects(:perform_async)
751
- .returns(true)
752
- .with { |*args|
753
- assert args[-1].is_a?(Float)
754
- assert args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
755
- }
756
- @job.enque!
757
- end
758
- end
759
- end
760
-
761
- describe 'sidekiq worker unknown class' do
762
- before do
763
- @args = {
764
- name: 'Test',
765
- cron: '* * * * *',
766
- klass: 'UnknownClass',
767
- queue: 'another'
768
- }
769
- @job = Sidekiq::Cron::Job.new(@args)
770
- end
771
-
772
- it 'pushes to queue sidekiq worker message' do
773
- @job.expects(:sidekiq_worker_message)
774
- .returns('class' => 'UnknownClass', 'args' => [], 'queue' => 'another')
775
- @job.enque!
776
- end
777
- end
778
- end
779
-
780
- describe "save" do
781
- before do
782
- @args = {
783
- name: "Test",
784
- cron: "* * * * *",
785
- klass: "CronTestClass"
786
- }
787
- @job = Sidekiq::Cron::Job.new(@args)
788
- end
789
-
790
- it "be saved" do
791
- assert @job.save
792
- end
793
-
794
- it "be saved and found by name" do
795
- assert @job.save, "not saved"
796
- assert Sidekiq::Cron::Job.find("Test").is_a?(Sidekiq::Cron::Job)
797
- end
798
- end
799
-
800
- describe "nonexisting job" do
801
- it "not be found" do
802
- assert Sidekiq::Cron::Job.find("nonexisting").nil?, "should return nil"
803
- end
804
- end
805
-
806
- describe "disabled/enabled" do
807
- before do
808
- @args = {
809
- name: "Test",
810
- cron: "* * * * *",
811
- klass: "CronTestClass"
812
- }
813
- end
814
-
815
- it "be created and enabled" do
816
- Sidekiq::Cron::Job.create(@args)
817
- job = Sidekiq::Cron::Job.find(@args)
818
- assert_equal job.status, "enabled"
819
- end
820
-
821
- it "be created and then enabled and disabled" do
822
- Sidekiq::Cron::Job.create(@args)
823
- job = Sidekiq::Cron::Job.find(@args)
824
- assert_equal job.status, "enabled"
825
-
826
- job.enable!
827
- assert_equal job.status, "enabled"
828
-
829
- job.disable!
830
- assert_equal job.status, "disabled"
831
- end
832
-
833
- it "be created with status disabled" do
834
- Sidekiq::Cron::Job.create(@args.merge(status: "disabled"))
835
- job = Sidekiq::Cron::Job.find(@args)
836
- assert_equal job.status, "disabled"
837
- assert_equal job.disabled?, true
838
- assert_equal job.enabled?, false
839
- end
840
-
841
- it "be created with status enabled and disable it afterwards" do
842
- Sidekiq::Cron::Job.create(@args)
843
- job = Sidekiq::Cron::Job.find(@args)
844
- assert_equal job.status, "enabled"
845
- assert_equal job.enabled?, true
846
- job.disable!
847
- assert_equal job.status, "disabled", "directly after call"
848
- assert_equal job.disabled?, true
849
- job = Sidekiq::Cron::Job.find(@args)
850
- assert_equal job.status, "disabled", "after find"
851
- end
852
-
853
- it "status shouldn't be rewritten after save without status" do
854
- Sidekiq::Cron::Job.create(@args)
855
- job = Sidekiq::Cron::Job.find(@args)
856
- assert_equal job.status, "enabled"
857
- job.disable!
858
- assert_equal job.status, "disabled", "directly after call"
859
- job = Sidekiq::Cron::Job.find(@args)
860
- assert_equal job.status, "disabled", "after find"
861
-
862
- Sidekiq::Cron::Job.create(@args)
863
- assert_equal job.status, "disabled", "after second create"
864
- job = Sidekiq::Cron::Job.find(@args)
865
- assert_equal job.status, "disabled", "after second find"
866
- end
867
-
868
- it "last_enqueue_time shouldn't be rewritten after save" do
869
- # Adding last_enqueue_time to initialize is only for testing purposes.
870
- last_enqueue_time = '2013-01-01 23:59:59 +0000'
871
- expected_enqueue_time = DateTime.parse(last_enqueue_time).to_time.utc
872
- Sidekiq::Cron::Job.create(@args.merge('last_enqueue_time' => last_enqueue_time))
873
- job = Sidekiq::Cron::Job.find(@args)
874
- assert_equal job.last_enqueue_time, expected_enqueue_time
875
-
876
- Sidekiq::Cron::Job.create(@args)
877
- job = Sidekiq::Cron::Job.find(@args)
878
- assert_equal job.last_enqueue_time, expected_enqueue_time, "after second create should have same time"
879
- end
880
- end
881
-
882
- describe "initialize args" do
883
- it "from JSON" do
884
- args = {
885
- name: "Test",
886
- cron: "* * * * *",
887
- klass: "CronTestClass",
888
- args: JSON.dump(["123"])
889
- }
890
- Sidekiq::Cron::Job.new(args).tap do |job|
891
- assert_equal job.args, ["123"]
892
- assert_equal job.name, "Test"
893
- end
894
- end
895
-
896
- it "from String" do
897
- args = {
898
- name: "Test",
899
- cron: "* * * * *",
900
- klass: "CronTestClass",
901
- args: "(my funny string)"
902
- }
903
- Sidekiq::Cron::Job.new(args).tap do |job|
904
- assert_equal job.args, ["(my funny string)"]
905
- assert_equal job.name, "Test"
906
- end
907
- end
908
-
909
- it "from Array" do
910
- args = {
911
- name: "Test",
912
- cron: "* * * * *",
913
- klass: "CronTestClass",
914
- args: ["This is array"]
915
- }
916
- Sidekiq::Cron::Job.new(args).tap do |job|
917
- assert_equal job.args, ["This is array"]
918
- assert_equal job.name, "Test"
919
- end
920
- end
921
- end
922
-
923
- describe "create & find methods" do
924
- before do
925
- @args = {
926
- name: "Test",
927
- cron: "* * * * *",
928
- klass: "CronTestClass"
929
- }
930
- end
931
-
932
- it "create first three jobs" do
933
- assert_equal Sidekiq::Cron::Job.count, 0, "Should have 0 jobs"
934
- Sidekiq::Cron::Job.create(@args)
935
- Sidekiq::Cron::Job.create(@args.merge(name: "Test2"))
936
- Sidekiq::Cron::Job.create(@args.merge(name: "Test3"))
937
- assert_equal Sidekiq::Cron::Job.count, 3, "Should have 3 jobs"
938
- end
939
-
940
- it "create first three jobs - 1 has same name" do
941
- assert_equal Sidekiq::Cron::Job.count, 0, "Should have 0 jobs"
942
- Sidekiq::Cron::Job.create(@args)
943
- Sidekiq::Cron::Job.create(@args.merge(name: "Test2"))
944
- Sidekiq::Cron::Job.create(@args.merge(cron: "1 * * * *"))
945
- assert_equal Sidekiq::Cron::Job.count, 2, "Should have 2 jobs"
946
- end
947
-
948
- it "be found by method all" do
949
- Sidekiq::Cron::Job.create(@args)
950
- Sidekiq::Cron::Job.create(@args.merge(name: "Test2"))
951
- Sidekiq::Cron::Job.create(@args.merge(name: "Test3"))
952
- assert_equal Sidekiq::Cron::Job.all.size, 3, "Should have 3 jobs"
953
- assert Sidekiq::Cron::Job.all.all?{|j| j.is_a?(Sidekiq::Cron::Job)}, "All returned jobs should be Job class"
954
- end
955
-
956
- it "be found by method all - defect in set" do
957
- Sidekiq::Cron::Job.create(@args)
958
- Sidekiq::Cron::Job.create(@args.merge(name: "Test2"))
959
- Sidekiq::Cron::Job.create(@args.merge(name: "Test3"))
960
-
961
- Sidekiq.redis do |conn|
962
- conn.sadd Sidekiq::Cron::Job.jobs_key, ["some_other_key"]
963
- end
964
-
965
- assert_equal Sidekiq::Cron::Job.all.size, 3, "All have to return only valid 3 jobs"
966
- end
967
-
968
- it "be found by string name" do
969
- Sidekiq::Cron::Job.create(@args)
970
- assert Sidekiq::Cron::Job.find("Test")
971
- end
972
-
973
- it "be found by hash with key name" do
974
- Sidekiq::Cron::Job.create(@args)
975
- assert Sidekiq::Cron::Job.find(name: "Test"), "symbol keys keys"
976
-
977
- Sidekiq::Cron::Job.create(@args)
978
- assert Sidekiq::Cron::Job.find('name' => "Test"), "String keys"
979
- end
980
- end
981
-
982
- describe "destroy" do
983
- before do
984
- @args = {
985
- name: "Test",
986
- cron: "* * * * *",
987
- klass: "CronTestClass"
988
- }
989
- end
990
-
991
- it "create and then destroy by hash" do
992
- Sidekiq::Cron::Job.create(@args)
993
- assert_equal Sidekiq::Cron::Job.all.size, 1, "Should have 1 job"
994
-
995
- assert Sidekiq::Cron::Job.destroy(@args)
996
- assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 job after destroy"
997
- end
998
-
999
- it "return false on destroying nonexisting" do
1000
- assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs"
1001
- refute Sidekiq::Cron::Job.destroy("nonexisting")
1002
- end
1003
-
1004
- it "return destroy by string name" do
1005
- Sidekiq::Cron::Job.create(@args)
1006
- assert Sidekiq::Cron::Job.destroy("Test")
1007
- end
1008
-
1009
- it "return destroy by hash with key name" do
1010
- Sidekiq::Cron::Job.create(@args)
1011
- assert Sidekiq::Cron::Job.destroy(name: "Test"), "symbol keys keys"
1012
-
1013
- Sidekiq::Cron::Job.create(@args)
1014
- assert Sidekiq::Cron::Job.destroy('name' => "Test"), "String keys"
1015
- end
1016
- end
1017
-
1018
- describe "destroy_removed_jobs" do
1019
- before do
1020
- args1 = {
1021
- name: "WillBeErasedJob",
1022
- cron: "* * * * *",
1023
- klass: "CronTestClass"
1024
- }
1025
- Sidekiq::Cron::Job.create(args1)
1026
-
1027
- args2 = {
1028
- name: "ContinueRemainingJob",
1029
- cron: "* * * * *",
1030
- klass: "CronTestClass"
1031
- }
1032
- Sidekiq::Cron::Job.create(args2)
1033
- end
1034
-
1035
- it "be destroied removed job that not exists in args" do
1036
- assert_equal Sidekiq::Cron::Job.destroy_removed_jobs(["ContinueRemainingJob"]), ["WillBeErasedJob"], "Should be destroyed WillBeErasedJob"
1037
- end
1038
- end
1039
-
1040
- describe "test of enque" do
1041
- before do
1042
- @args = {
1043
- name: "Test",
1044
- cron: "* * * * *",
1045
- klass: "CronTestClass"
1046
- }
1047
- # First time is always after next cron time!
1048
- @time = Time.now.utc + 120
1049
- end
1050
-
1051
- it "be always false when status is disabled" do
1052
- refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time
1053
- refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time - 60
1054
- refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time - 120
1055
- assert_equal Sidekiq::Queue.all.size, 0, "Sidekiq 0 queues"
1056
- end
1057
-
1058
- it "be false for same times" do
1059
- assert Sidekiq::Cron::Job.new(@args).should_enque?(@time), "First time - true"
1060
- refute Sidekiq::Cron::Job.new(@args).should_enque? @time
1061
- refute Sidekiq::Cron::Job.new(@args).should_enque? @time
1062
- end
1063
-
1064
- it "be false for same times but true for next time" do
1065
- assert Sidekiq::Cron::Job.new(@args).should_enque?(@time), "First time - true"
1066
- refute Sidekiq::Cron::Job.new(@args).should_enque? @time
1067
- assert Sidekiq::Cron::Job.new(@args).should_enque? @time + 135
1068
- refute Sidekiq::Cron::Job.new(@args).should_enque? @time + 135
1069
- assert Sidekiq::Cron::Job.new(@args).should_enque? @time + 235
1070
- refute Sidekiq::Cron::Job.new(@args).should_enque? @time + 235
1071
-
1072
- refute Sidekiq::Cron::Job.new(@args).should_enque? @time
1073
- refute Sidekiq::Cron::Job.new(@args).should_enque? @time + 135
1074
- refute Sidekiq::Cron::Job.new(@args).should_enque? @time + 235
1075
- end
1076
-
1077
- it "should not enqueue jobs that are past" do
1078
- assert Sidekiq::Cron::Job.new(@args.merge(cron: "*/1 * * * *")).should_enque? @time
1079
- refute Sidekiq::Cron::Job.new(@args.merge(cron: "0 1,13 * * *")).should_enque? @time
1080
- end
1081
-
1082
- it 'doesnt skip enqueuing if job is resaved near next enqueue time' do
1083
- job = Sidekiq::Cron::Job.new(@args)
1084
- assert job.test_and_enque_for_time!(@time), "should enqueue"
1085
-
1086
- future_now = @time + 1 * 60 * 60
1087
- Time.stubs(:now).returns(future_now) # Save uses Time.now.utc
1088
- job.save
1089
- assert Sidekiq::Cron::Job.new(@args).test_and_enque_for_time!(future_now + 30), "should enqueue"
1090
- end
1091
-
1092
- it "remove old enque times + should be enqeued" do
1093
- job = Sidekiq::Cron::Job.new(@args)
1094
- assert_nil job.last_enqueue_time
1095
- assert job.test_and_enque_for_time!(@time), "should enqueue"
1096
- assert job.last_enqueue_time
1097
-
1098
- refute Sidekiq::Cron::Job.new(@args).test_and_enque_for_time!(@time), "should not enqueue"
1099
- Sidekiq.redis do |conn|
1100
- assert_equal conn.zcard(Sidekiq::Cron::Job.new(@args).send(:job_enqueued_key)), 1, "Should have one enqueued job"
1101
- end
1102
- assert_equal Sidekiq::Queue.all.first.size, 1, "Sidekiq queue 1 job in queue"
1103
-
1104
- # 20 hours after.
1105
- assert Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 1 * 60 * 60
1106
- refute Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 1 * 60 * 60
1107
-
1108
- Sidekiq.redis do |conn|
1109
- assert_equal conn.zcard(Sidekiq::Cron::Job.new(@args).send(:job_enqueued_key)), 2, "Should have two enqueued job"
1110
- end
1111
- assert_equal Sidekiq::Queue.all.first.size, 2, "Sidekiq queue 2 jobs in queue"
1112
-
1113
- # 26 hour after.
1114
- assert Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 26 * 60 * 60
1115
- refute Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 26 * 60 * 60
1116
-
1117
- Sidekiq.redis do |conn|
1118
- assert_equal conn.zcard(Sidekiq::Cron::Job.new(@args).send(:job_enqueued_key)), 1, "Should have one enqueued job - old jobs should be deleted"
1119
- end
1120
- assert_equal Sidekiq::Queue.all.first.size, 3, "Sidekiq queue 3 jobs in queue"
1121
- end
1122
- end
1123
-
1124
- describe "load" do
1125
- describe "from hash" do
1126
- before do
1127
- @jobs_hash = {
1128
- 'name_of_job' => {
1129
- 'class' => 'MyClass',
1130
- 'cron' => '1 * * * *',
1131
- 'args' => '(OPTIONAL) [Array or Hash]'
1132
- },
1133
- 'My super iber cool job' => {
1134
- 'class' => 'SecondClass',
1135
- 'cron' => '*/5 * * * *'
1136
- }
1137
- }
1138
- end
1139
-
1140
- it "create new jobs and update old one with same settings" do
1141
- assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
1142
- out = Sidekiq::Cron::Job.load_from_hash @jobs_hash
1143
- assert_equal out.size, 0, "should have no errors"
1144
- assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
1145
- end
1146
-
1147
- it "duplicate jobs are not loaded" do
1148
- out = Sidekiq::Cron::Job.load_from_hash @jobs_hash
1149
- assert_equal out.size, 0, "should have no errors"
1150
- assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
1151
-
1152
- out_2 = Sidekiq::Cron::Job.load_from_hash @jobs_hash
1153
- assert_equal out_2.size, 0, "should have no errors"
1154
- assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after loading again"
1155
- end
1156
-
1157
- it "return errors on loaded jobs" do
1158
- assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
1159
- # Set something bad to hash.
1160
- @jobs_hash['name_of_job']['cron'] = "bad cron"
1161
- out = Sidekiq::Cron::Job.load_from_hash @jobs_hash
1162
- assert_equal 1, out.size, "should have 1 error"
1163
- assert_includes out['name_of_job'].first, "bad cron"
1164
- assert_includes out['name_of_job'].first, "ArgumentError:"
1165
- assert_equal 1, Sidekiq::Cron::Job.all.size, "Should have only 1 job after load"
1166
- end
1167
-
1168
- it "create new jobs and then destroy them all" do
1169
- assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
1170
- out = Sidekiq::Cron::Job.load_from_hash @jobs_hash
1171
- assert_equal out.size, 0, "should have no errors"
1172
- assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
1173
- Sidekiq::Cron::Job.destroy_all!
1174
- assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs after destroy all"
1175
- end
1176
-
1177
- it "create new jobs and update old one with same settings with load_from_hash!" do
1178
- assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
1179
- out = Sidekiq::Cron::Job.load_from_hash! @jobs_hash
1180
- assert_equal out.size, 0, "should have no errors"
1181
- assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
1182
- end
1183
- end
1184
-
1185
- describe "from array" do
1186
- before do
1187
- @jobs_array = [
1188
- {
1189
- 'name' => 'name_of_job',
1190
- 'class' => 'MyClass',
1191
- 'cron' => '1 * * * *',
1192
- 'args' => '(OPTIONAL) [Array or Hash]'
1193
- },
1194
- {
1195
- 'name' => 'Cool Job for Second Class',
1196
- 'class' => 'SecondClass',
1197
- 'cron' => '*/5 * * * *'
1198
- }
1199
- ]
1200
- end
1201
-
1202
- it "create new jobs and update old one with same settings" do
1203
- assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
1204
- out = Sidekiq::Cron::Job.load_from_array @jobs_array
1205
- assert_equal out.size, 0, "should have 0 error"
1206
- assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
1207
- end
1208
-
1209
- it "duplicate jobs are not loaded" do
1210
- out = Sidekiq::Cron::Job.load_from_array @jobs_array
1211
- assert_equal out.size, 0, "should have no errors"
1212
- assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
1213
-
1214
- out_2 = Sidekiq::Cron::Job.load_from_array @jobs_array
1215
- assert_equal out_2.size, 0, "should have no errors"
1216
- assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after loading again"
1217
- end
1218
-
1219
- it "create new jobs and update old one with same settings with load_from_array" do
1220
- assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
1221
- out = Sidekiq::Cron::Job.load_from_array! @jobs_array
1222
- assert_equal out.size, 0, "should have 0 error"
1223
- assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
1224
- end
1225
- end
1226
-
1227
- describe "from array with queue_name" do
1228
- before do
1229
- @jobs_array = [
1230
- {
1231
- 'name' => 'name_of_job',
1232
- 'class' => 'CronTestClassWithQueue',
1233
- 'cron' => '1 * * * *',
1234
- 'args' => '(OPTIONAL) [Array or Hash]',
1235
- 'queue' => 'from_array'
1236
- }
1237
- ]
1238
- end
1239
-
1240
- it "create new jobs and update old one with same settings" do
1241
- assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
1242
- out = Sidekiq::Cron::Job.load_from_array @jobs_array
1243
- assert_equal out.size, 0, "should have 0 error"
1244
- assert_equal Sidekiq::Cron::Job.all.size, 1, "Should have 2 jobs after load"
1245
-
1246
- payload = {
1247
- "retry" => false,
1248
- "backtrace"=>true,
1249
- "queue" => "from_array",
1250
- "class" => "CronTestClassWithQueue",
1251
- "args" => ['(OPTIONAL) [Array or Hash]']
1252
- }
1253
-
1254
- assert_equal Sidekiq::Cron::Job.all.first.sidekiq_worker_message, payload
1255
- end
1256
- end
1257
- end
1258
- end