sidekiq-cron 1.10.1 → 1.11.0

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