File: pg.t

package info (click to toggle)
libminion-perl 10.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,004 kB
  • sloc: javascript: 3,572; perl: 1,013; sql: 79; makefile: 10
file content (1421 lines) | stat: -rw-r--r-- 63,468 bytes parent folder | download
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
use Mojo::Base -strict;

BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;

plan skip_all => 'set TEST_ONLINE to enable this test' unless $ENV{TEST_ONLINE};

use Mojo::File qw(curfile);
use lib curfile->sibling('lib')->to_string;

use Minion;
use Mojo::IOLoop;
use Mojo::Promise;
use Sys::Hostname qw(hostname);
use Time::HiRes qw(usleep);

# Isolate tests
require Mojo::Pg;
my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
$pg->db->query('DROP SCHEMA IF EXISTS minion_test CASCADE');
$pg->db->query('CREATE SCHEMA minion_test');
my $minion = Minion->new(Pg => $ENV{TEST_ONLINE});
$minion->backend->pg->search_path(['minion_test']);

subtest 'Nothing to repair' => sub {
  my $worker = $minion->repair->worker;
  isa_ok $worker->minion->app, 'Mojolicious', 'has default application';
};

subtest 'Migrate up and down' => sub {
  is $minion->backend->pg->migrations->active, 23, 'active version is 23';
  is $minion->backend->pg->migrations->migrate(0)->active, 0, 'active version is 0';
  is $minion->backend->pg->migrations->migrate->active, 23, 'active version is 23';
};

subtest 'Register and unregister' => sub {
  my $worker = $minion->worker;
  $worker->register;
  like $worker->info->{started}, qr/^[\d.]+$/, 'has timestamp';
  my $notified = $worker->info->{notified};
  like $notified, qr/^[\d.]+$/, 'has timestamp';
  my $id = $worker->id;
  is $worker->register->id, $id, 'same id';
  usleep 50000;
  ok $worker->register->info->{notified} > $notified, 'new timestamp';
  is $worker->unregister->info, undef, 'no information';
  my $host = hostname;
  is $worker->register->info->{host}, $host, 'right host';
  is $worker->info->{pid}, $$, 'right pid';
  is $worker->unregister->info, undef, 'no information';
};

subtest 'Job results' => sub {
  $minion->add_task(test => sub { });
  my $worker = $minion->worker->register;
  my $id     = $minion->enqueue('test');
  my (@finished, @failed);
  my $promise = $minion->result_p($id, {interval => 0})->then(sub { @finished = @_ })->catch(sub { @failed = @_ });
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'same id';
  Mojo::IOLoop->one_tick;
  is_deeply \@finished, [], 'not finished';
  is_deeply \@failed,   [], 'not failed';
  $job->finish({just => 'works!'});
  $job->note(foo => 'bar');
  $promise->wait;
  is_deeply $finished[0]{result}, {just => 'works!'}, 'right result';
  is_deeply $finished[0]{notes},  {foo  => 'bar'},    'right note';
  ok !$finished[1], 'no more results';
  is_deeply \@failed, [], 'not failed';

  (@finished, @failed) = ();
  my $id2 = $minion->enqueue('test');
  $promise = $minion->result_p($id2, {interval => 0})->then(sub { @finished = @_ })->catch(sub { @failed = @_ });
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id2, 'same id';
  $job->fail({works => 'too!'});
  $promise->wait;
  is_deeply \@finished, [], 'not finished';
  is_deeply $failed[0]{result}, {works => 'too!'}, 'right result';
  ok !$failed[1], 'no more results';
  $worker->unregister;

  (@finished, @failed) = ();
  $minion->result_p($id)->then(sub { @finished = @_ })->catch(sub { @failed = @_ })->wait;
  is_deeply $finished[0]{result}, {just => 'works!'}, 'right result';
  is_deeply $finished[0]{notes},  {foo  => 'bar'},    'right note';
  ok !$finished[1], 'no more results';
  is_deeply \@failed, [], 'not failed';

  (@finished, @failed) = ();
  $minion->job($id)->retry;
  $minion->result_p($id)->timeout(0.25)->then(sub { @finished = @_ })->catch(sub { @failed = @_ })->wait;
  is_deeply \@finished, [], 'not finished';
  is_deeply \@failed, ['Promise timeout'], 'failed';
  Mojo::IOLoop->start;

  (@finished, @failed) = ();
  $minion->job($id)->remove;
  $minion->result_p($id)->then(sub { @finished = (@_, 'finished') })->catch(sub { @failed = (@_, 'failed') })->wait;
  is_deeply \@finished, ['finished'], 'job no longer exists';
  is_deeply \@failed, [], 'not failed';
};

subtest 'Repair missing worker' => sub {
  my $worker  = $minion->worker->register;
  my $worker2 = $minion->worker->register;
  isnt $worker2->id, $worker->id, 'new id';
  my $id = $minion->enqueue('test');
  ok my $job = $worker2->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $worker2->info->{jobs}[0], $job->id, 'right id';
  $id = $worker2->id;
  undef $worker2;
  is $job->info->{state}, 'active', 'job is still active';
  ok !!$minion->backend->list_workers(0, 1, {ids => [$id]})->{workers}[0], 'is registered';
  $minion->backend->pg->db->query("UPDATE minion_workers SET notified = NOW() - INTERVAL '1 second' * ? WHERE id = ?",
    $minion->missing_after + 1, $id);
  $minion->repair;
  ok !$minion->backend->list_workers(0, 1, {ids => [$id]})->{workers}[0], 'not registered';
  like $job->info->{finished}, qr/^[\d.]+$/,       'has finished timestamp';
  is $job->info->{state},      'failed',           'job is no longer active';
  is $job->info->{result},     'Worker went away', 'right result';
  $worker->unregister;
};

subtest 'Repair abandoned job' => sub {
  my $worker = $minion->worker->register;
  my $id     = $minion->enqueue('test');
  ok my $job = $worker->dequeue(1), 'job dequeued';
  is $job->id, $id, 'right id';
  $worker->unregister;
  $minion->repair;
  is $job->info->{state},  'failed',           'job is no longer active';
  is $job->info->{result}, 'Worker went away', 'right result';
};

subtest 'Repair abandoned job in minion_foreground queue (have to be handled manually)' => sub {
  my $worker = $minion->worker->register;
  my $id     = $minion->enqueue('test', [], {queue => 'minion_foreground'});
  ok my $job = $worker->dequeue(0, {queues => ['minion_foreground']}), 'job dequeued';
  is $job->id, $id, 'right id';
  $worker->unregister;
  $minion->repair;
  is $job->info->{state},  'active', 'job is still active';
  is $job->info->{result}, undef,    'no result';
};

subtest 'Repair old jobs' => sub {
  is $minion->remove_after, 172800, 'right default';
  my $worker = $minion->worker->register;
  my $id     = $minion->enqueue('test');
  my $id2    = $minion->enqueue('test');
  my $id3    = $minion->enqueue('test');
  $worker->dequeue(0)->perform for 1 .. 3;
  my $finished
    = $minion->backend->pg->db->query('SELECT EXTRACT(EPOCH FROM finished) AS finished FROM minion_jobs WHERE id = ?',
    $id2)->hash->{finished};
  $minion->backend->pg->db->query('UPDATE minion_jobs SET finished = TO_TIMESTAMP(?) WHERE id = ?',
    $finished - ($minion->remove_after + 1), $id2);
  $finished
    = $minion->backend->pg->db->query('SELECT EXTRACT(EPOCH FROM finished) AS finished FROM minion_jobs WHERE id = ?',
    $id3)->hash->{finished};
  $minion->backend->pg->db->query('UPDATE minion_jobs SET finished = TO_TIMESTAMP(?) WHERE id = ?',
    $finished - ($minion->remove_after + 1), $id3);
  $worker->unregister;
  $minion->repair;
  ok $minion->job($id), 'job has not been cleaned up';
  ok !$minion->job($id2), 'job has been cleaned up';
  ok !$minion->job($id3), 'job has been cleaned up';
};

subtest 'Repair stuck jobs' => sub {
  is $minion->stuck_after, 172800, 'right default';
  my $worker = $minion->worker->register;
  my $id     = $minion->enqueue('test');
  my $id2    = $minion->enqueue('test');
  my $id3    = $minion->enqueue('test');
  my $id4    = $minion->enqueue('test');
  $minion->backend->pg->db->query("UPDATE minion_jobs SET delayed = NOW() - ? * INTERVAL '1 second' WHERE id = ?",
    $minion->stuck_after + 1, $_)
    for $id, $id2, $id3, $id4;
  ok $worker->dequeue(0, {id => $id4})->finish('Works!'), 'job finished';
  ok my $job2 = $worker->dequeue(0, {id => $id2}), 'job dequeued';
  $minion->repair;
  is $job2->info->{state}, 'active', 'job is still active';
  ok $job2->finish, 'job finished';
  my $job = $minion->job($id);
  is $job->info->{state},  'failed',                     'job is no longer active';
  is $job->info->{result}, 'Job appears stuck in queue', 'right result';
  my $job3 = $minion->job($id3);
  is $job3->info->{state},  'failed',                     'job is no longer active';
  is $job3->info->{result}, 'Job appears stuck in queue', 'right result';
  my $job4 = $minion->job($id4);
  is $job4->info->{state},  'finished', 'job is still finished';
  is $job4->info->{result}, 'Works!',   'right result';
  $worker->unregister;
};

subtest 'List workers' => sub {
  my $worker  = $minion->worker->register;
  my $worker2 = $minion->worker->status({whatever => 'works!'})->register;
  my $results = $minion->backend->list_workers(0, 10);
  my $host    = hostname;
  is $results->{total}, 2, 'two workers total';

  my $batch = $results->{workers};
  ok $batch->[0]{id},        'has id';
  is $batch->[0]{host},      $host, 'right host';
  is $batch->[0]{pid},       $$,    'right pid';
  like $batch->[0]{started}, qr/^[\d.]+$/, 'has timestamp';
  is $batch->[1]{host},      $host, 'right host';
  is $batch->[1]{pid},       $$,    'right pid';
  ok !$batch->[2], 'no more results';

  $results = $minion->backend->list_workers(0, 1);
  $batch   = $results->{workers};
  is $results->{total}, 2, 'two workers total';
  is $batch->[0]{id}, $worker2->id, 'right id';
  is_deeply $batch->[0]{status}, {whatever => 'works!'}, 'right status';
  ok !$batch->[1], 'no more results';
  $worker2->status({whatever => 'works too!'})->register;
  $batch = $minion->backend->list_workers(0, 1)->{workers};
  is_deeply $batch->[0]{status}, {whatever => 'works too!'}, 'right status';
  $batch = $minion->backend->list_workers(1, 1)->{workers};
  is $batch->[0]{id}, $worker->id, 'right id';
  ok !$batch->[1], 'no more results';
  $worker->unregister;
  $worker2->unregister;

  $minion->reset({all => 1});
  $worker  = $minion->worker->status({test => 'one'})->register;
  $worker2 = $minion->worker->status({test => 'two'})->register;
  my $worker3 = $minion->worker->status({test => 'three'})->register;
  my $worker4 = $minion->worker->status({test => 'four'})->register;
  my $worker5 = $minion->worker->status({test => 'five'})->register;
  my $workers = $minion->workers->fetch(2);
  is $workers->options->{before}, undef, 'no before';
  is $workers->next->{status}{test}, 'five', 'right status';
  is $workers->options->{before}, 4, 'before 4';
  is $workers->next->{status}{test}, 'four',  'right status';
  is $workers->next->{status}{test}, 'three', 'right status';
  is $workers->options->{before}, 2, 'before 2';
  is $workers->next->{status}{test}, 'two', 'right status';
  is $workers->next->{status}{test}, 'one', 'right status';
  is $workers->options->{before}, 1, 'before 1';
  is $workers->next, undef, 'no more results';

  $workers = $minion->workers({ids => [2, 4, 1]});
  is $workers->options->{before}, undef, 'no before';
  is $workers->next->{status}{test}, 'four', 'right status';
  is $workers->options->{before}, 1, 'before 1';
  is $workers->next->{status}{test}, 'two', 'right status';
  is $workers->next->{status}{test}, 'one', 'right status';
  is $workers->next, undef, 'no more results';

  $workers = $minion->workers->fetch(2);
  is $workers->next->{status}{test}, 'five', 'right status';
  is $workers->next->{status}{test}, 'four', 'right status';
  is $workers->total, 5, 'five workers';
  $worker5->unregister;
  $worker4->unregister;
  $worker3->unregister;
  is $workers->next->{status}{test}, 'two', 'right status';
  is $workers->next->{status}{test}, 'one', 'right status';
  is $workers->next,  undef, 'no more results';
  is $workers->total, 4,     'four workers';
  is $minion->workers->total, 2, 'two workers';
  $worker->unregister;
  $worker2->unregister;
};

subtest 'Exclusive lock' => sub {
  ok $minion->lock('foo', 3600), 'locked';
  ok !$minion->lock('foo', 3600), 'not locked again';
  ok $minion->unlock('foo'), 'unlocked';
  ok !$minion->unlock('foo'), 'not unlocked again';
  ok $minion->lock('foo', -3600), 'locked';
  ok $minion->lock('foo', 0),     'locked again';
  ok !$minion->is_locked('foo'), 'lock does not exist';
  ok $minion->lock('foo', 3600), 'locked again';
  ok $minion->is_locked('foo'), 'lock exists';
  ok !$minion->lock('foo', -3600), 'not locked again';
  ok !$minion->lock('foo', 3600),  'not locked again';
  ok $minion->unlock('foo'), 'unlocked';
  ok !$minion->unlock('foo'), 'not unlocked again';
  ok $minion->lock('yada', 3600, {limit => 1}), 'locked';
  ok !$minion->lock('yada', 3600, {limit => 1}), 'not locked again';
};

subtest 'Shared lock' => sub {
  ok $minion->lock('bar', 3600, {limit => 3}), 'locked';
  ok $minion->lock('bar', 3600, {limit => 3}), 'locked again';
  ok $minion->is_locked('bar'), 'lock exists';
  ok $minion->lock('bar', -3600, {limit => 3}), 'locked again';
  ok $minion->lock('bar', 3600, {limit => 3}),  'locked again';
  ok !$minion->lock('bar', 3600, {limit => 2}), 'not locked again';
  ok $minion->lock('baz', 3600, {limit => 3}), 'locked';
  ok $minion->unlock('bar'), 'unlocked';
  ok $minion->lock('bar', 3600, {limit => 3}), 'locked again';
  ok $minion->unlock('bar'), 'unlocked again';
  ok $minion->unlock('bar'), 'unlocked again';
  ok $minion->unlock('bar'), 'unlocked again';
  ok !$minion->unlock('bar'),    'not unlocked again';
  ok !$minion->is_locked('bar'), 'lock does not exist';
  ok $minion->unlock('baz'), 'unlocked';
  ok !$minion->unlock('baz'), 'not unlocked again';
};

subtest 'List locks' => sub {
  is $minion->stats->{active_locks}, 1, 'one active lock';
  my $results = $minion->backend->list_locks(0, 2);
  is $results->{locks}[0]{name},      'yada',       'right name';
  like $results->{locks}[0]{expires}, qr/^[\d.]+$/, 'expires';
  is $results->{locks}[1], undef, 'no more locks';
  is $results->{total}, 1, 'one result';
  $minion->unlock('yada');
  $minion->lock('yada', 3600, {limit => 2});
  $minion->lock('test', 3600, {limit => 1});
  $minion->lock('yada', 3600, {limit => 2});
  is $minion->stats->{active_locks}, 3, 'three active locks';
  $results = $minion->backend->list_locks(1, 1);
  is $results->{locks}[0]{name},      'test',       'right name';
  like $results->{locks}[0]{expires}, qr/^[\d.]+$/, 'expires';
  is $results->{locks}[1], undef, 'no more locks';
  is $results->{total}, 3, 'three results';
  $results = $minion->backend->list_locks(0, 10, {names => ['yada']});
  is $results->{locks}[0]{name},      'yada',       'right name';
  like $results->{locks}[0]{expires}, qr/^[\d.]+$/, 'expires';
  is $results->{locks}[1]{name},      'yada',       'right name';
  like $results->{locks}[1]{expires}, qr/^[\d.]+$/, 'expires';
  is $results->{locks}[2], undef, 'no more locks';
  is $results->{total}, 2, 'two results';
  $minion->backend->pg->db->query(
    "UPDATE minion_locks SET expires = NOW() - INTERVAL '1 second' * 1 WHERE name = 'yada'",
  );
  is $minion->backend->list_locks(0, 10, {names => ['yada']})->{total}, 0, 'no results';
  $minion->unlock('test');
  is $minion->backend->list_locks(0, 10)->{total}, 0, 'no results';
};

subtest 'Lock with guard' => sub {
  ok my $guard = $minion->guard('foo', 3600, {limit => 1}), 'locked';
  ok !$minion->guard('foo', 3600, {limit => 1}), 'not locked again';
  undef $guard;
  ok $guard = $minion->guard('foo', 3600), 'locked';
  ok !$minion->guard('foo', 3600), 'not locked again';
  undef $guard;
  ok $minion->guard('foo', 3600, {limit => 1}), 'locked again';
  ok $minion->guard('foo', 3600, {limit => 1}), 'locked again';
  ok $guard     = $minion->guard('bar', 3600, {limit => 2}), 'locked';
  ok my $guard2 = $minion->guard('bar', 0,    {limit => 2}), 'locked';
  ok my $guard3 = $minion->guard('bar', 3600, {limit => 2}), 'locked';
  undef $guard2;
  ok !$minion->guard('bar', 3600, {limit => 2}), 'not locked again';
  undef $guard;
  undef $guard3;
};

subtest 'Reset (locks)' => sub {
  $minion->enqueue('test');
  $minion->lock('test', 3600);
  $minion->worker->register;
  ok $minion->backend->list_jobs(0, 1)->{total},    'jobs';
  ok $minion->backend->list_locks(0, 1)->{total},   'locks';
  ok $minion->backend->list_workers(0, 1)->{total}, 'workers';
  $minion->reset({locks => 1});
  ok $minion->backend->list_jobs(0, 1)->{total}, 'jobs';
  ok !$minion->backend->list_locks(0, 1)->{total}, 'no locks';
  ok $minion->backend->list_workers(0, 1)->{total}, 'workers';
};

subtest 'Reset (all)' => sub {
  $minion->lock('test', 3600);
  ok $minion->backend->list_jobs(0, 1)->{total},    'jobs';
  ok $minion->backend->list_locks(0, 1)->{total},   'locks';
  ok $minion->backend->list_workers(0, 1)->{total}, 'workers';
  $minion->reset({all => 1})->repair;
  ok !$minion->backend->list_jobs(0, 1)->{total},    'no jobs';
  ok !$minion->backend->list_locks(0, 1)->{total},   'no locks';
  ok !$minion->backend->list_workers(0, 1)->{total}, 'no workers';
};

subtest 'Stats' => sub {
  $minion->add_task(
    add => sub {
      my ($job, $first, $second) = @_;
      $job->finish({added => $first + $second});
    }
  );
  $minion->add_task(fail => sub { die "Intentional failure!\n" });
  my $stats = $minion->stats;
  is $stats->{active_workers},   0, 'no active workers';
  is $stats->{inactive_workers}, 0, 'no inactive workers';
  is $stats->{enqueued_jobs},    0, 'no enqueued jobs';
  is $stats->{active_jobs},      0, 'no active jobs';
  is $stats->{failed_jobs},      0, 'no failed jobs';
  is $stats->{finished_jobs},    0, 'no finished jobs';
  is $stats->{inactive_jobs},    0, 'no inactive jobs';
  is $stats->{delayed_jobs},     0, 'no delayed jobs';
  is $stats->{active_locks},     0, 'no active locks';
  ok $stats->{uptime},           'has uptime';
  my $worker = $minion->worker->register;
  is $minion->stats->{inactive_workers}, 1, 'one inactive worker';
  $minion->enqueue('fail');
  is $minion->stats->{enqueued_jobs}, 1, 'one enqueued job';
  $minion->enqueue('fail');
  is $minion->stats->{enqueued_jobs}, 2, 'two enqueued jobs';
  is $minion->stats->{inactive_jobs}, 2, 'two inactive jobs';
  ok my $job = $worker->dequeue(0), 'job dequeued';
  $stats = $minion->stats;
  is $stats->{active_workers}, 1, 'one active worker';
  is $stats->{active_jobs},    1, 'one active job';
  is $stats->{inactive_jobs},  1, 'one inactive job';
  $minion->enqueue('fail');
  ok my $job2 = $worker->dequeue(0), 'job dequeued';
  $stats = $minion->stats;
  is $stats->{active_workers}, 1, 'one active worker';
  is $stats->{active_jobs},    2, 'two active jobs';
  is $stats->{inactive_jobs},  1, 'one inactive job';
  ok $job2->finish, 'job finished';
  ok $job->finish,  'job finished';
  is $minion->stats->{finished_jobs}, 2, 'two finished jobs';
  ok $job = $worker->dequeue(0), 'job dequeued';
  ok $job->fail, 'job failed';
  is $minion->stats->{failed_jobs}, 1, 'one failed job';
  ok $job->retry, 'job retried';
  is $minion->stats->{failed_jobs}, 0, 'no failed jobs';
  ok $worker->dequeue(0)->finish(['works']), 'job finished';
  $worker->unregister;
  $stats = $minion->stats;
  is $stats->{active_workers},   0, 'no active workers';
  is $stats->{inactive_workers}, 0, 'no inactive workers';
  is $stats->{active_jobs},      0, 'no active jobs';
  is $stats->{failed_jobs},      0, 'no failed jobs';
  is $stats->{finished_jobs},    3, 'three finished jobs';
  is $stats->{inactive_jobs},    0, 'no inactive jobs';
  is $stats->{delayed_jobs},     0, 'no delayed jobs';
};

subtest 'History' => sub {
  $minion->enqueue('fail');
  my $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  ok $job->fail, 'job failed';
  $worker->unregister;
  my $history = $minion->history;
  is $#{$history->{daily}}, 23, 'data for 24 hours';
  is $history->{daily}[-1]{finished_jobs} + $history->{daily}[-2]{finished_jobs}, 3, 'one failed job in the last hour';
  is $history->{daily}[-1]{failed_jobs} + $history->{daily}[-2]{failed_jobs}, 1, 'three finished jobs in the last hour';
  is $history->{daily}[0]{finished_jobs}, 0, 'no finished jobs 24 hours ago';
  is $history->{daily}[0]{failed_jobs},   0, 'no failed jobs 24 hours ago';
  ok defined $history->{daily}[0]{epoch},  'has epoch value';
  ok defined $history->{daily}[1]{epoch},  'has epoch value';
  ok defined $history->{daily}[12]{epoch}, 'has epoch value';
  ok defined $history->{daily}[-1]{epoch}, 'has epoch value';
  $job->remove;
};

subtest 'List jobs' => sub {
  my $id = $minion->enqueue('add');
  is $minion->backend->list_jobs(1, 1)->{total}, 4, 'four total with offset and limit';
  my $results = $minion->backend->list_jobs(0, 10);
  my $batch   = $results->{jobs};
  is $results->{total}, 4, 'four jobs total';
  ok $batch->[0]{id},          'has id';
  is $batch->[0]{task},        'add',        'right task';
  is $batch->[0]{state},       'inactive',   'right state';
  is $batch->[0]{retries},     0,            'job has not been retried';
  like $batch->[0]{created},   qr/^[\d.]+$/, 'has created timestamp';
  is $batch->[1]{task},        'fail',       'right task';
  is_deeply $batch->[1]{args}, [], 'right arguments';
  is_deeply $batch->[1]{notes}, {}, 'right metadata';
  is_deeply $batch->[1]{result},   ['works'], 'right result';
  is $batch->[1]{state},           'finished', 'right state';
  is $batch->[1]{priority},        0,          'right priority';
  is_deeply $batch->[1]{parents},  [], 'right parents';
  is_deeply $batch->[1]{children}, [], 'right children';
  is $batch->[1]{retries},         1,            'job has been retried';
  like $batch->[1]{created},       qr/^[\d.]+$/, 'has created timestamp';
  like $batch->[1]{delayed},       qr/^[\d.]+$/, 'has delayed timestamp';
  like $batch->[1]{finished},      qr/^[\d.]+$/, 'has finished timestamp';
  like $batch->[1]{retried},       qr/^[\d.]+$/, 'has retried timestamp';
  like $batch->[1]{started},       qr/^[\d.]+$/, 'has started timestamp';
  is $batch->[2]{task},            'fail',       'right task';
  is $batch->[2]{state},           'finished',   'right state';
  is $batch->[2]{retries},         0,            'job has not been retried';
  is $batch->[3]{task},            'fail',       'right task';
  is $batch->[3]{state},           'finished',   'right state';
  is $batch->[3]{retries},         0,            'job has not been retried';
  ok !$batch->[4], 'no more results';

  $batch = $minion->backend->list_jobs(0, 10, {states => ['inactive']})->{jobs};
  is $batch->[0]{state},   'inactive', 'right state';
  is $batch->[0]{retries}, 0,          'job has not been retried';
  ok !$batch->[1], 'no more results';

  $batch = $minion->backend->list_jobs(0, 10, {tasks => ['add']})->{jobs};
  is $batch->[0]{task},    'add', 'right task';
  is $batch->[0]{retries}, 0,     'job has not been retried';
  ok !$batch->[1], 'no more results';

  $batch = $minion->backend->list_jobs(0, 10, {tasks => ['add', 'fail']})->{jobs};
  is $batch->[0]{task}, 'add',  'right task';
  is $batch->[1]{task}, 'fail', 'right task';
  is $batch->[2]{task}, 'fail', 'right task';
  is $batch->[3]{task}, 'fail', 'right task';
  ok !$batch->[4], 'no more results';

  $batch = $minion->backend->list_jobs(0, 10, {queues => ['default']})->{jobs};
  is $batch->[0]{queue}, 'default', 'right queue';
  is $batch->[1]{queue}, 'default', 'right queue';
  is $batch->[2]{queue}, 'default', 'right queue';
  is $batch->[3]{queue}, 'default', 'right queue';
  ok !$batch->[4], 'no more results';

  my $id2 = $minion->enqueue('test' => [] => {notes => {is_test => 1}});
  $batch = $minion->backend->list_jobs(0, 10, {notes => ['is_test']})->{jobs};
  is $batch->[0]{task}, 'test', 'right task';
  ok !$batch->[4], 'no more results';
  ok $minion->job($id2)->remove, 'job removed';

  $batch = $minion->backend->list_jobs(0, 10, {queues => ['does_not_exist']})->{jobs};
  is_deeply $batch, [], 'no results';

  $results = $minion->backend->list_jobs(0, 1);
  $batch   = $results->{jobs};
  is $results->{total}, 4, 'four jobs total';
  is $batch->[0]{state},   'inactive', 'right state';
  is $batch->[0]{retries}, 0,          'job has not been retried';
  ok !$batch->[1], 'no more results';

  $batch = $minion->backend->list_jobs(1, 1)->{jobs};
  is $batch->[0]{state},   'finished', 'right state';
  is $batch->[0]{retries}, 1,          'job has been retried';
  ok !$batch->[1], 'no more results';

  my $jobs = $minion->jobs;
  is $jobs->next->{task},      'add',  'right task';
  is $jobs->options->{before}, 1,      'before 1';
  is $jobs->next->{task},      'fail', 'right task';
  is $jobs->next->{task},      'fail', 'right task';
  is $jobs->next->{task},      'fail', 'right task';
  is $jobs->next,  undef, 'no more results';
  is $jobs->total, 4,     'four jobs';

  my @tasks;
  $minion->jobs->each(sub { push @tasks, [shift->{task}, $_->{task}] });
  is_deeply \@tasks, [['add', 'add'], ['fail', 'fail'], ['fail', 'fail'], ['fail', 'fail']], 'right structure';

  $jobs = $minion->jobs->fetch(2);
  is $jobs->options->{before}, undef,  'no before';
  is $jobs->next->{task},      'add',  'right task';
  is $jobs->options->{before}, 3,      'before 3';
  is $jobs->next->{task},      'fail', 'right task';
  is $jobs->options->{before}, 3,      'before 3';
  is $jobs->next->{task},      'fail', 'right task';
  is $jobs->options->{before}, 1,      'before 1';
  is $jobs->next->{task},      'fail', 'right task';
  is $jobs->options->{before}, 1,      'before 1';
  is $jobs->next,  undef, 'no more results';
  is $jobs->total, 4,     'four jobs';

  $jobs = $minion->jobs({states => ['inactive']});
  is $jobs->total, 1, 'one job';
  is $jobs->next->{task}, 'add', 'right task';
  is $jobs->next, undef, 'no more results';

  $jobs = $minion->jobs({states => ['active']});
  is $jobs->next, undef, 'no more results';

  $jobs = $minion->jobs->fetch(1);
  is $jobs->next->{task}, 'add', 'right task';
  is $jobs->total, 4, 'four jobs';
  $id2 = $minion->enqueue('add');
  my $next = $jobs->next;
  is $next->{task}, 'fail', 'right task';
  ok $minion->job($next->{id} - 1)->remove, 'job removed';
  is $jobs->total, 4, 'four jobs';
  is $jobs->next->{task}, 'fail', 'right task';
  is $jobs->next,  undef, 'no more results';
  is $jobs->total, 3,     'three jobs';
  ok $minion->job($id)->remove,  'job removed';
  ok $minion->job($id2)->remove, 'job removed';
};

subtest 'Enqueue, dequeue and perform' => sub {
  is $minion->job(12345), undef, 'job does not exist';
  my $id = $minion->enqueue(add => [2, 2]);
  ok $minion->job($id), 'job does exist';
  my $info = $minion->job($id)->info;
  is_deeply $info->{args}, [2, 2], 'right arguments';
  is $info->{priority},    0,          'right priority';
  is $info->{state},       'inactive', 'right state';
  my $worker = $minion->worker;
  is $worker->dequeue(0), undef, 'not registered';
  ok !$minion->job($id)->info->{started}, 'no started timestamp';
  $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $worker->info->{jobs}[0], $job->id, 'right job';
  like $job->info->{created}, qr/^[\d.]+$/, 'has created timestamp';
  like $job->info->{started}, qr/^[\d.]+$/, 'has started timestamp';
  like $job->info->{time},    qr/^[\d.]+$/, 'has server time';
  is_deeply $job->args, [2, 2], 'right arguments';
  is $job->info->{state}, 'active', 'right state';
  is $job->task,    'add', 'right task';
  is $job->retries, 0,     'job has not been retried';
  $id = $job->info->{worker};
  is $minion->backend->list_workers(0, 1, {ids => [$id]})->{workers}[0]{pid}, $$, 'right worker';
  ok !$job->info->{finished}, 'no finished timestamp';
  $job->perform;
  is $worker->info->{jobs}[0], undef, 'no jobs';
  like $job->info->{finished}, qr/^[\d.]+$/, 'has finished timestamp';
  is_deeply $job->info->{result}, {added => 4}, 'right result';
  is $job->info->{state}, 'finished', 'right state';
  $worker->unregister;
  $job = $minion->job($job->id);
  is_deeply $job->args, [2, 2], 'right arguments';
  is $job->retries, 0, 'job has not been retried';
  is $job->info->{state}, 'finished', 'right state';
  is $job->task, 'add', 'right task';
};

subtest 'Retry and remove' => sub {
  my $id     = $minion->enqueue(add => [5, 6]);
  my $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->info->{attempts}, 1, 'job will be attempted once';
  is $job->info->{retries},  0, 'job has not been retried';
  is $job->id, $id, 'right id';
  ok $job->finish, 'job finished';
  ok !$worker->dequeue(0), 'no more jobs';
  $job = $minion->job($id);
  ok !$job->info->{retried}, 'no retried timestamp';
  ok $job->retry, 'job retried';
  like $job->info->{retried}, qr/^[\d.]+$/, 'has retried timestamp';
  is $job->info->{state},     'inactive',   'right state';
  is $job->info->{retries},   1,            'job has been retried once';
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->retries, 1, 'job has been retried once';
  ok $job->retry,   'job retried';
  is $job->id,      $id, 'right id';
  is $job->info->{retries}, 2, 'job has been retried twice';
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->info->{state}, 'active', 'right state';
  ok $job->finish, 'job finished';
  ok $job->remove, 'job has been removed';
  ok !$job->retry, 'job not retried';
  is $job->info, undef, 'no information';
  $id  = $minion->enqueue(add => [6, 5]);
  $job = $minion->job($id);
  is $job->info->{state},   'inactive', 'right state';
  is $job->info->{retries}, 0,          'job has not been retried';
  ok $job->retry, 'job retried';
  is $job->info->{state},   'inactive', 'right state';
  is $job->info->{retries}, 1,          'job has been retried once';
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id,     $id, 'right id';
  ok $job->fail,   'job failed';
  ok $job->remove, 'job has been removed';
  is $job->info,   undef, 'no information';
  $id  = $minion->enqueue(add => [5, 5]);
  $job = $minion->job("$id");
  ok $job->remove, 'job has been removed';
  $worker->unregister;
};

subtest 'Jobs with priority' => sub {
  $minion->enqueue(add => [1, 2]);
  my $id     = $minion->enqueue(add => [2, 4], {priority => 1});
  my $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->info->{priority}, 1, 'right priority';
  ok $job->finish, 'job finished';
  isnt $worker->dequeue(0)->id, $id, 'different id';
  $id = $minion->enqueue(add => [2, 5]);
  ok $job = $worker->register->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->info->{priority}, 0, 'right priority';
  ok $job->finish, 'job finished';
  ok $job->retry({priority => 100}), 'job retried with higher priority';
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->info->{retries},  1,   'job has been retried once';
  is $job->info->{priority}, 100, 'high priority';
  ok $job->finish, 'job finished';
  ok $job->retry({priority => 0}), 'job retried with lower priority';
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->info->{retries},  2, 'job has been retried twice';
  is $job->info->{priority}, 0, 'low priority';
  ok $job->finish, 'job finished';

  $id = $minion->enqueue(add => [2, 6], {priority => 2});
  ok !$worker->dequeue(0, {min_priority => 5});
  ok !$worker->dequeue(0, {min_priority => 3});
  ok $job = $worker->dequeue(0, {min_priority => 2});
  is $job->id, $id, 'right id';
  is $job->info->{priority}, 2, 'expected priority';
  ok $job->finish, 'job finished';
  $minion->enqueue(add => [2, 8], {priority => 0});
  $minion->enqueue(add => [2, 7], {priority => 5});
  $minion->enqueue(add => [2, 8], {priority => -2});
  ok !$worker->dequeue(0, {min_priority => 6});
  ok $job = $worker->dequeue(0, {min_priority => 0});
  is $job->info->{priority}, 5, 'expected priority';
  ok $job->finish, 'job finished';
  ok $job = $worker->dequeue(0, {min_priority => 0});
  is $job->info->{priority}, 0, 'expected priority';
  ok $job->finish, 'job finished';
  ok !$worker->dequeue(0, {min_priority => 0});
  ok $job = $worker->dequeue(0, {min_priority => -10});
  is $job->info->{priority}, -2, 'expected priority';
  ok $job->finish, 'job finished';
  $worker->unregister;
};

subtest 'Delayed jobs' => sub {
  my $id = $minion->enqueue(add => [2, 1] => {delay => 100});
  is $minion->stats->{delayed_jobs}, 1, 'one delayed job';
  my $worker = $minion->worker->register;
  is $worker->dequeue(0), undef, 'too early for job';
  my $job = $minion->job($id);
  ok $job->info->{delayed} > $job->info->{created}, 'delayed timestamp';
  $minion->backend->pg->db->query("UPDATE minion_jobs SET delayed = NOW() - INTERVAL '1 day' WHERE id = ?", $id);
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  like $job->info->{delayed}, qr/^[\d.]+$/, 'has delayed timestamp';
  ok $job->finish, 'job finished';
  ok $job->retry,  'job retried';
  my $info = $minion->job($id)->info;
  ok $info->{delayed} <= $info->{retried}, 'no delayed timestamp';
  ok $job->remove, 'job removed';
  ok !$job->retry, 'job not retried';
  $id = $minion->enqueue(add => [6, 9]);
  ok $job = $worker->dequeue(0), 'job dequeued';
  $info = $minion->job($id)->info;
  ok $info->{delayed} <= $info->{created}, 'no delayed timestamp';
  ok $job->fail, 'job failed';
  ok $job->retry({delay => 100}), 'job retried with delay';
  $info = $minion->job($id)->info;
  is $info->{retries}, 1, 'job has been retried once';
  ok $info->{delayed} > $info->{retried}, 'delayed timestamp';
  ok $minion->job($id)->remove, 'job has been removed';
  $worker->unregister;
};

subtest 'Events' => sub {
  my ($enqueue, $pid_start, $pid_stop);
  my ($failed, $finished) = (0, 0);
  $minion->once(enqueue => sub { $enqueue = pop });
  $minion->once(
    worker => sub {
      my ($minion, $worker) = @_;
      $worker->on(
        dequeue => sub {
          my ($worker, $job) = @_;
          $job->on(failed   => sub { $failed++ });
          $job->on(finished => sub { $finished++ });
          $job->on(spawn    => sub { $pid_start = pop });
          $job->on(reap     => sub { $pid_stop  = pop });
          $job->on(
            start => sub {
              my $job = shift;
              return unless $job->task eq 'switcheroo';
              $job->task('add')->args->[-1] += 1;
            }
          );
          $job->on(
            finish => sub {
              my $job = shift;
              return unless defined(my $old = $job->info->{notes}{finish_count});
              $job->note(finish_count => $old + 1, finish_pid => $$);
            }
          );
          $job->on(
            cleanup => sub {
              my $job = shift;
              return unless defined(my $old = $job->info->{notes}{finish_count});
              $job->note(cleanup_count => $old + 1, cleanup_pid => $$);
            }
          );
        }
      );
    }
  );
  my $worker = $minion->worker->register;
  my $id     = $minion->enqueue(add => [3, 3]);
  is $enqueue, $id, 'enqueue event has been emitted';
  $minion->enqueue(add => [4, 3]);
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $failed,   0, 'failed event has not been emitted';
  is $finished, 0, 'finished event has not been emitted';
  my $result;
  $job->on(finished => sub { $result = pop });
  ok $job->finish('Everything is fine!'), 'job finished';
  $job->perform;
  is $result,      'Everything is fine!', 'right result';
  is $failed,      0,                     'failed event has not been emitted';
  is $finished,    1,                     'finished event has been emitted once';
  isnt $pid_start, $$,        'new process id';
  isnt $pid_stop,  $$,        'new process id';
  is $pid_start,   $pid_stop, 'same process id';
  ok $job = $worker->dequeue(0), 'job dequeued';
  my $err;
  $job->on(failed => sub { $err = pop });
  $job->fail("test\n");
  $job->fail;
  is $err,      "test\n", 'right error';
  is $failed,   1,        'failed event has been emitted once';
  is $finished, 1,        'finished event has been emitted once';
  $minion->add_task(switcheroo => sub { });
  $minion->enqueue(switcheroo => [5, 3] => {notes => {finish_count => 0, before => 23}});
  ok $job = $worker->dequeue(0), 'job dequeued';
  $job->perform;
  is_deeply $job->info->{result}, {added => 9}, 'right result';
  is $job->info->{notes}{finish_count},  1, 'finish event has been emitted once';
  ok $job->info->{notes}{finish_pid},    'has a process id';
  isnt $job->info->{notes}{finish_pid},  $$, 'different process id';
  is $job->info->{notes}{before},        23, 'value still exists';
  is $job->info->{notes}{cleanup_count}, 2,  'cleanup event has been emitted once';
  ok $job->info->{notes}{cleanup_pid},   'has a process id';
  isnt $job->info->{notes}{cleanup_pid}, $$, 'different process id';
  $worker->unregister;
};

subtest 'Queues' => sub {
  my $id     = $minion->enqueue(add => [100, 1]);
  my $worker = $minion->worker->register;
  is $worker->dequeue(0 => {queues => ['test1']}), undef, 'wrong queue';
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->info->{queue}, 'default', 'right queue';
  ok $job->finish, 'job finished';
  $id = $minion->enqueue(add => [100, 3] => {queue => 'test1'});
  is $worker->dequeue(0), undef, 'wrong queue';
  ok $job = $worker->dequeue(0 => {queues => ['test1']}), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->info->{queue}, 'test1', 'right queue';
  ok $job->finish, 'job finished';
  ok $job->retry({queue => 'test2'}), 'job retried';
  ok $job = $worker->dequeue(0 => {queues => ['default', 'test2']}), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->info->{queue}, 'test2', 'right queue';
  ok $job->finish, 'job finished';
  $worker->unregister;
};

subtest 'Failed jobs' => sub {
  my $id     = $minion->enqueue(add => [5, 6]);
  my $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->info->{result}, undef, 'no result';
  ok $job->fail, 'job failed';
  ok !$job->finish, 'job not finished';
  is $job->info->{state},  'failed',        'right state';
  is $job->info->{result}, 'Unknown error', 'right result';
  $id = $minion->enqueue(add => [6, 7]);
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  ok $job->fail('Something bad happened!'), 'job failed';
  is $job->info->{state},  'failed',                  'right state';
  is $job->info->{result}, 'Something bad happened!', 'right result';
  $id = $minion->enqueue('fail');
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  $job->perform;
  is $job->info->{state},  'failed',                 'right state';
  is $job->info->{result}, "Intentional failure!\n", 'right result';
  $worker->unregister;
};

subtest 'Nested data structures' => sub {
  $minion->add_task(
    nested => sub {
      my ($job, $hash, $array) = @_;
      $job->note(bar => {baz => [1, 2, 3]});
      $job->note(baz => 'yada');
      $job->finish([{23 => $hash->{first}[0]{second} x $array->[0][0]}]);
    }
  );
  $minion->enqueue('nested', [{first => [{second => 'test'}]}, [[3]]], {notes => {foo => [4, 5, 6]}});
  my $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  $job->perform;
  is $job->info->{state}, 'finished', 'right state';
  ok $job->note(yada => ['works']), 'added metadata';
  ok !$minion->backend->note(-1, {yada => ['failed']}), 'not added metadata';
  my $notes = {foo => [4, 5, 6], bar => {baz => [1, 2, 3]}, baz => 'yada', yada => ['works']};
  is_deeply $job->info->{notes}, $notes, 'right metadata';
  is_deeply $job->info->{result}, [{23 => 'testtesttest'}], 'right structure';
  ok $job->note(yada => undef, bar => undef), 'removed metadata';
  $notes = {foo => [4, 5, 6], baz => 'yada'};
  is_deeply $job->info->{notes}, $notes, 'right metadata';
  $worker->unregister;
};

subtest 'Perform job in a running event loop' => sub {
  my $id = $minion->enqueue(add => [8, 9]);
  Mojo::Promise->new->resolve->then(sub { $minion->perform_jobs })->wait;
  is $minion->job($id)->info->{state}, 'finished', 'right state';
  is_deeply $minion->job($id)->info->{result}, {added => 17}, 'right result';
};

subtest 'Job terminated unexpectedly' => sub {
  $minion->add_task(exit => sub { exit 1 });
  my $id     = $minion->enqueue('exit');
  my $worker = $minion->worker->register;
  ok my $job = $worker->register->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  $job->perform;
  is $job->info->{state},  'failed',                                                'right state';
  is $job->info->{result}, 'Job terminated unexpectedly (exit code: 1, signal: 0)', 'right result';
  $worker->unregister;
};

subtest 'Multiple attempts while processing' => sub {
  is $minion->backoff->(0),  15,     'right result';
  is $minion->backoff->(1),  16,     'right result';
  is $minion->backoff->(2),  31,     'right result';
  is $minion->backoff->(3),  96,     'right result';
  is $minion->backoff->(4),  271,    'right result';
  is $minion->backoff->(5),  640,    'right result';
  is $minion->backoff->(25), 390640, 'right result';

  my $id     = $minion->enqueue(exit => [] => {attempts => 3});
  my $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->retries, 0, 'job has not been retried';
  my $info = $job->info;
  is $info->{attempts}, 3,        'three attempts';
  is $info->{state},    'active', 'right state';
  $job->perform;
  $info = $job->info;
  is $info->{attempts}, 2,                                                       'two attempts';
  is $info->{state},    'inactive',                                              'right state';
  is $info->{result},   'Job terminated unexpectedly (exit code: 1, signal: 0)', 'right result';
  ok $info->{retried} < $job->info->{delayed}, 'delayed timestamp';

  $minion->backend->pg->db->query('UPDATE minion_jobs SET delayed = NOW() WHERE id = ?', $id);
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->retries, 1, 'job has been retried';
  $info = $job->info;
  is $info->{attempts}, 2,        'two attempts';
  is $info->{state},    'active', 'right state';
  $job->perform;
  $info = $job->info;
  is $info->{attempts}, 1,          'one attempt';
  is $info->{state},    'inactive', 'right state';

  $minion->backend->pg->db->query('UPDATE minion_jobs SET delayed = NOW() WHERE id = ?', $id);
  ok $job = $worker->register->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->retries, 2, 'two retries';
  $info = $job->info;
  is $info->{attempts}, 1,        'one attempt';
  is $info->{state},    'active', 'right state';
  $job->perform;
  $info = $job->info;
  is $info->{attempts}, 1,                                                       'one attempt';
  is $info->{state},    'failed',                                                'right state';
  is $info->{result},   'Job terminated unexpectedly (exit code: 1, signal: 0)', 'right result';

  ok $job->retry({attempts => 2}), 'job retried';
  ok $job = $worker->register->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  $job->perform;
  is $job->info->{state}, 'inactive', 'right state';
  $minion->backend->pg->db->query('UPDATE minion_jobs SET delayed = NOW() WHERE id = ?', $id);
  ok $job = $worker->register->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  $job->perform;
  is $job->info->{state}, 'failed', 'right state';
  $worker->unregister;
};

subtest 'Multiple attempts during maintenance' => sub {
  my $id     = $minion->enqueue(exit => [] => {attempts => 2});
  my $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->retries, 0, 'job has not been retried';
  is $job->info->{attempts}, 2,        'job will be attempted twice';
  is $job->info->{state},    'active', 'right state';
  $worker->unregister;
  $minion->repair;
  is $job->info->{state},  'inactive',         'right state';
  is $job->info->{result}, 'Worker went away', 'right result';
  ok $job->info->{retried} < $job->info->{delayed}, 'delayed timestamp';
  $minion->backend->pg->db->query('UPDATE minion_jobs SET delayed = NOW() WHERE id = ?', $id);
  ok $job = $worker->register->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is $job->retries, 1, 'job has been retried once';
  $worker->unregister;
  $minion->repair;
  is $job->info->{state},  'failed',           'right state';
  is $job->info->{result}, 'Worker went away', 'right result';
};

subtest 'A job needs to be dequeued again after a retry' => sub {
  $minion->add_task(restart => sub { });
  my $id     = $minion->enqueue('restart');
  my $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  ok $job->finish, 'job finished';
  is $job->info->{state}, 'finished', 'right state';
  ok $job->retry, 'job retried';
  is $job->info->{state}, 'inactive', 'right state';
  ok my $job2 = $worker->dequeue(0), 'job dequeued';
  is $job->info->{state}, 'active', 'right state';
  ok !$job->finish, 'job not finished';
  is $job->info->{state}, 'active', 'right state';
  is $job2->id, $id, 'right id';
  ok $job2->finish, 'job finished';
  ok !$job->retry, 'job not retried';
  is $job->info->{state}, 'finished', 'right state';
  $worker->unregister;
};

subtest 'Perform jobs concurrently' => sub {
  my $id     = $minion->enqueue(add => [10, 11]);
  my $id2    = $minion->enqueue(add => [12, 13]);
  my $id3    = $minion->enqueue('test');
  my $id4    = $minion->enqueue('exit');
  my $worker = $minion->worker->register;
  ok my $job  = $worker->dequeue(0), 'job dequeued';
  ok my $job2 = $worker->dequeue(0), 'job dequeued';
  ok my $job3 = $worker->dequeue(0), 'job dequeued';
  ok my $job4 = $worker->dequeue(0), 'job dequeued';
  $job->start;
  $job2->start;
  $job3->start;
  $job4->start;
  my ($first, $second, $third, $fourth);
  usleep 50000
    until $first ||= $job->is_finished
    and $second  ||= $job2->is_finished
    and $third   ||= $job3->is_finished
    and $fourth  ||= $job4->is_finished;
  is $minion->job($id)->info->{state}, 'finished', 'right state';
  is_deeply $minion->job($id)->info->{result}, {added => 21}, 'right result';
  is $minion->job($id2)->info->{state}, 'finished', 'right state';
  is_deeply $minion->job($id2)->info->{result}, {added => 25}, 'right result';
  is $minion->job($id3)->info->{state},  'finished',                                              'right state';
  is $minion->job($id3)->info->{result}, undef,                                                   'no result';
  is $minion->job($id4)->info->{state},  'failed',                                                'right state';
  is $minion->job($id4)->info->{result}, 'Job terminated unexpectedly (exit code: 1, signal: 0)', 'right result';
  $worker->unregister;
};

subtest 'Stopping jobs' => sub {
  $minion->add_task(
    long_running => sub {
      shift->note(started => 1);
      sleep 1000;
    }
  );
  my $worker = $minion->worker->register;
  $minion->enqueue('long_running');
  ok my $job = $worker->dequeue(0), 'job dequeued';
  ok $job->start->pid, 'has a process id';
  ok !$job->is_finished, 'job is not finished';
  $job->stop;
  usleep 5000 until $job->is_finished;
  is $job->info->{state},    'failed',                        'right state';
  like $job->info->{result}, qr/Job terminated unexpectedly/, 'right result';
  $minion->enqueue('long_running');
  ok $job = $worker->dequeue(0), 'job dequeued';
  ok $job->start->pid, 'has a process id';
  ok !$job->is_finished, 'job is not finished';
  usleep 5000 until $job->info->{notes}{started};
  $job->kill('USR1');
  $job->kill('USR2');
  is $job->info->{state}, 'active', 'right state';
  $job->kill('INT');
  usleep 5000 until $job->is_finished;
  is $job->info->{state},    'failed',                        'right state';
  like $job->info->{result}, qr/Job terminated unexpectedly/, 'right result';
  $worker->unregister;
};

subtest 'Job dependencies' => sub {
  my $worker = $minion->remove_after(0)->worker->register;
  is $minion->repair->stats->{finished_jobs}, 0, 'no finished jobs';
  my $id  = $minion->enqueue('test');
  my $id2 = $minion->enqueue('test');
  my $id3 = $minion->enqueue(test => [] => {parents => [$id, $id2]});
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is_deeply $job->info->{children}, [$id3], 'right children';
  is_deeply $job->info->{parents}, [], 'right parents';
  ok my $job2 = $worker->dequeue(0), 'job dequeued';
  is $job2->id, $id2, 'right id';
  is_deeply $job2->info->{children}, [$id3], 'right children';
  is_deeply $job2->info->{parents}, [], 'right parents';
  ok !$worker->dequeue(0), 'parents are not ready yet';
  ok $job->finish, 'job finished';
  ok !$worker->dequeue(0), 'parents are not ready yet';
  ok $job2->fail, 'job failed';
  ok !$worker->dequeue(0), 'parents are not ready yet';
  ok $job2->retry, 'job retried';
  ok $job2 = $worker->dequeue(0), 'job dequeued';
  is $job2->id, $id2, 'right id';
  ok $job2->finish, 'job finished';
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id3, 'right id';
  is_deeply $job->info->{children},   [], 'right children';
  is_deeply $job->info->{parents},    [$id, $id2], 'right parents';
  is $minion->stats->{finished_jobs}, 2, 'two finished jobs';
  is $minion->repair->stats->{finished_jobs}, 2, 'two finished jobs';
  ok $job->finish, 'job finished';
  is $minion->stats->{finished_jobs}, 3, 'three finished jobs';
  is $minion->repair->remove_after(172800)->stats->{finished_jobs}, 0, 'no finished jobs';
  $id = $minion->enqueue(test => [] => {parents => [-1]});
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  ok $job->finish, 'job finished';
  $id = $minion->enqueue(test => [] => {parents => [-1]});
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is_deeply $job->info->{parents}, [-1], 'right parents';
  $job->retry({parents => [-1, -2]});
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is_deeply $job->info->{parents}, [-1, -2], 'right parents';
  ok $job->finish, 'job finished';

  my $id4     = $minion->enqueue('test');
  my $id5     = $minion->enqueue('test');
  my $id6     = $minion->enqueue(test => [] => {parents => [$id4, $id5]});
  my $child   = $minion->job($id6);
  my $parents = $child->parents;
  is $parents->size, 2, 'two parents';
  is $parents->[0]->id, $id4, 'first parent';
  is $parents->[1]->id, $id5, 'second parent';
  $_->remove for $parents->each;
  is $child->parents->size, 0, 'no parents';
  ok $child->remove, 'job removed';
  $worker->unregister;
};

subtest 'Job dependencies (lax)' => sub {
  my $worker = $minion->worker->register;
  my $id     = $minion->enqueue('test');
  my $id2    = $minion->enqueue('test');
  my $id3    = $minion->enqueue(test => [] => {lax => 1, parents => [$id, $id2]});
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  is_deeply $job->info->{children}, [$id3], 'right children';
  is_deeply $job->info->{parents}, [], 'right parents';
  ok my $job2 = $worker->dequeue(0), 'job dequeued';
  is $job2->id, $id2, 'right id';
  is_deeply $job2->info->{children}, [$id3], 'right children';
  is_deeply $job2->info->{parents}, [], 'right parents';
  ok !$worker->dequeue(0), 'parents are not ready yet';
  ok $job->finish, 'job finished';
  ok !$worker->dequeue(0), 'parents are not ready yet';
  ok $job2->fail, 'job failed';
  ok my $job3 = $worker->dequeue(0), 'job dequeued';
  is $job3->id, $id3, 'right id';
  is_deeply $job3->info->{children}, [], 'right children';
  is_deeply $job3->info->{parents}, [$id, $id2], 'right parents';
  ok $job3->finish, 'job finished';

  my $id4 = $minion->enqueue('test');
  my $id5 = $minion->enqueue(test => [] => {parents => [$id4]});
  ok my $job4 = $worker->dequeue(0), 'job dequeued';
  is $job4->id, $id4, 'right id';
  ok !$worker->dequeue(0), 'parents are not ready yet';
  ok $job4->fail, 'job finished';
  ok !$worker->dequeue(0), 'parents are not ready yet';
  ok $minion->job($id5)->retry({lax => 1}), 'job is now lax';
  ok my $job5 = $worker->dequeue(0), 'job dequeued';
  is $job5->id, $id5, 'right id';
  is_deeply $job5->info->{children}, [], 'right children';
  is_deeply $job5->info->{parents}, [$id4], 'right parents';
  ok $job5->finish, 'job finished';
  ok $job4->remove, 'job removed';

  is $minion->jobs({ids => [$id5]})->next->{lax}, 1, 'lax';
  ok $minion->job($id5)->retry, 'job is still lax';
  is $minion->jobs({ids => [$id5]})->next->{lax}, 1, 'lax';
  ok $minion->job($id5)->retry({lax => 0}), 'job is not lax anymore';
  is $minion->jobs({ids => [$id5]})->next->{lax}, 0, 'not lax';
  ok $minion->job($id5)->retry, 'job is still not lax';
  is $minion->jobs({ids => [$id5]})->next->{lax}, 0, 'not lax';
  ok $minion->job($id5)->remove, 'job removed';
  $worker->unregister;
};

subtest 'Expiring jobs' => sub {
  my $id = $minion->enqueue('test');
  is $minion->job($id)->info->{expires}, undef, 'no expires timestamp';
  $minion->job($id)->remove;

  $id = $minion->enqueue('test' => [] => {expire => 300});
  like $minion->job($id)->info->{expires}, qr/^[\d.]+$/, 'has expires timestamp';
  my $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  my $expires = $job->info->{expires};
  like $expires, qr/^[\d.]+$/, 'has expires timestamp';
  ok $job->finish, 'job finished';
  ok $job->retry({expire => 600}), 'job retried';
  my $info = $minion->job($id)->info;
  is $info->{state},     'inactive',   'rigth state';
  like $info->{expires}, qr/^[\d.]+$/, 'has expires timestamp';
  isnt $info->{expires}, $expires, 'retried with new expires timestamp';
  is $minion->repair->jobs({states => ['inactive']})->total, 1, 'job has not expired yet';
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  ok $job->finish, 'job finished';

  $id = $minion->enqueue('test' => [] => {expire => 300});
  is $minion->repair->jobs({states => ['inactive']})->total, 1, 'job has not expired yet';
  my $pg = $minion->backend->pg;
  $pg->db->query("UPDATE minion_jobs SET expires = NOW() - INTERVAL '1 day' WHERE id = ?", $id);
  is $minion->jobs({states => ['inactive']})->total, 0, 'job has expired';
  ok !$worker->dequeue(0), 'job has expired';
  ok $pg->db->select('minion_jobs', '*', {id => $id})->hash, 'job still exists in database';
  $minion->repair;
  ok !$pg->db->select('minion_jobs', '*', {id => $id})->hash, 'job no longer exists in database';

  $id = $minion->enqueue('test' => [] => {expire => 300});
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  ok $job->finish, 'job finished';
  $pg->db->query("UPDATE minion_jobs SET expires = NOW() - INTERVAL '1 day' WHERE id = ?", $id);
  $minion->repair;
  ok $job = $minion->job($id), 'job still exists';
  is $job->info->{state}, 'finished', 'right state';

  $id = $minion->enqueue('test' => [] => {expire => 300});
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  ok $job->fail, 'job failed';
  $pg->db->query("UPDATE minion_jobs SET expires = NOW() - INTERVAL '1 day' WHERE id = ?", $id);
  $minion->repair;
  ok $job = $minion->job($id), 'job still exists';
  is $job->info->{state}, 'failed', 'right state';

  $id = $minion->enqueue('test' => [] => {expire => 300});
  ok $job = $worker->dequeue(0), 'job dequeued';
  is $job->id, $id, 'right id';
  $pg->db->query("UPDATE minion_jobs SET expires = NOW() - INTERVAL '1 day' WHERE id = ?", $id);
  $minion->repair;
  ok $job = $minion->job($id), 'job still exists';
  is $job->info->{state}, 'active', 'right state';
  ok $job->finish, 'job finished';

  $id = $minion->enqueue('test' => [] => {expire => 300});
  my $id2 = $minion->enqueue('test' => [] => {parents => [$id]});
  ok !$worker->dequeue(0, {id => $id2}), 'parent is still inactive';
  $pg->db->query("UPDATE minion_jobs SET expires = NOW() - INTERVAL '1 day' WHERE id = ?", $id);
  ok $job = $worker->dequeue(0, {id => $id2}), 'parent has expired';
  ok $job->finish, 'job finished';
  $worker->unregister;
};

subtest 'Custom task classes' => sub {
  $minion->add_task(my_add       => 'MinionTest::AddTestTask');
  $minion->add_task(my_other_add => 'MinionTest::AddTestTask');
  $minion->add_task(my_empty     => 'MinionTest::EmptyTestTask');
  $minion->add_task(my_no_result => 'MinionTest::NoResultTestTask');
  $minion->add_task(my_fail      => 'MinionTest::FailTestTask');
  is $minion->class_for_task('my_add'),       'MinionTest::AddTestTask',   'right class';
  is $minion->class_for_task('my_empty'),     'MinionTest::EmptyTestTask', 'right class';
  is $minion->class_for_task('long_running'), 'Minion::Job',               'right class';
  is $minion->class_for_task('unknown'),      'Minion::Job',               'right class';

  eval { $minion->add_task(my_missing => 'MinionTest::MissingTestTask') };
  like $@, qr/Task "MinionTest::MissingTestTask" missing/, 'right error';
  eval { $minion->add_task(my_syntax_error => 'MinionTest::SyntaxErrorTestTask') };
  like $@, qr/Missing right curly or square bracket/, 'right error';
  eval { $minion->add_task(my_bad => 'MinionTest::BadTestTask') };
  like $@, qr/Task "MinionTest::BadTestTask" is not a Minion::Job subclass/, 'right error';

  $minion->enqueue(my_add => [3, 5]);
  my $worker = $minion->worker->register;
  ok my $job = $worker->dequeue(0), 'job dequeued';
  isa_ok $job, 'MinionTest::AddTestTask', 'right class';
  is $job->task, 'my_add', 'right task';
  $job->perform;
  is $job->info->{state},  'finished',       'right state';
  is $job->info->{result}, 'My result is 8', 'right result';

  $minion->enqueue(my_other_add => [5, 4]);
  ok $job = $worker->dequeue(0), 'job dequeued';
  isa_ok $job, 'MinionTest::AddTestTask', 'right class';
  is $job->task, 'my_other_add', 'right task';
  $job->perform;
  is $job->info->{state},  'finished',       'right state';
  is $job->info->{result}, 'My result is 9', 'right result';

  my $id = $minion->enqueue('my_no_result');
  $minion->perform_jobs;
  $job = $minion->job($id);
  isa_ok $job, 'MinionTest::NoResultTestTask', 'right class';
  isa_ok $job, 'MinionTest::AddTestTask',      'right class';
  isa_ok $job, 'Minion::Job',                  'right class';
  is $job->task, 'my_no_result', 'right task';
  is $job->info->{state},  'finished', 'right state';
  is $job->info->{result}, undef,      'right result';

  $id = $minion->enqueue('my_fail');
  $minion->perform_jobs;
  $job = $minion->job($id);
  isa_ok $job, 'MinionTest::FailTestTask', 'right class';
  is $job->task, 'my_fail', 'right task';
  is $job->info->{state},  'failed',         'right state';
  is $job->info->{result}, "my_fail failed", 'right result';

  $id = $minion->enqueue('my_empty');
  $minion->perform_jobs;
  $job = $minion->job($id);
  isa_ok $job, 'MinionTest::EmptyTestTask', 'right class';
  is $job->task, 'my_empty', 'right task';
  is $job->info->{state},    'failed',                                     'right state';
  like $job->info->{result}, qr/Method "run" not implemented by subclass/, 'right result';
  $worker->unregister;
};

subtest 'Foreground' => sub {
  my $id  = $minion->enqueue(test => [] => {attempts => 2});
  my $id2 = $minion->enqueue('test');
  my $id3 = $minion->enqueue(test => [] => {parents => [$id, $id2]});
  ok !$minion->foreground($id3 + 1), 'job does not exist';
  ok !$minion->foreground($id3), 'job is not ready yet';
  my $info = $minion->job($id)->info;
  is $info->{attempts}, 2,          'job will be attempted twice';
  is $info->{state},    'inactive', 'right state';
  is $info->{queue},    'default',  'right queue';
  ok $minion->foreground($id), 'performed first job';
  $info = $minion->job($id)->info;
  is $info->{attempts}, 1,                   'job will be attempted once';
  is $info->{retries},  1,                   'job has been retried';
  is $info->{state},    'finished',          'right state';
  is $info->{queue},    'minion_foreground', 'right queue';
  ok $minion->foreground($id2), 'performed second job';
  $info = $minion->job($id2)->info;
  is $info->{retries}, 1,                   'job has been retried';
  is $info->{state},   'finished',          'right state';
  is $info->{queue},   'minion_foreground', 'right queue';
  ok $minion->foreground($id3), 'performed third job';
  $info = $minion->job($id3)->info;
  is $info->{retries}, 2,                   'job has been retried twice';
  is $info->{state},   'finished',          'right state';
  is $info->{queue},   'minion_foreground', 'right queue';
  $id = $minion->enqueue('fail');
  eval { $minion->foreground($id) };
  like $@, qr/Intentional failure!/, 'right error';
  $info = $minion->job($id)->info;
  ok $info->{worker}, 'has worker';
  ok !$minion->backend->list_workers(0, 1, {ids => [$info->{worker}]})->{workers}[0], 'not registered';
  is $info->{retries}, 1,                        'job has been retried';
  is $info->{state},   'failed',                 'right state';
  is $info->{queue},   'minion_foreground',      'right queue';
  is $info->{result},  "Intentional failure!\n", 'right result';
};

subtest 'Worker remote control commands' => sub {
  my $worker  = $minion->worker->register->process_commands;
  my $worker2 = $minion->worker->register;
  my @commands;
  $_->add_command(test_id => sub { push @commands, shift->id }) for $worker, $worker2;
  $worker->add_command(test_args => sub { shift and push @commands, [@_] })->register;
  ok $minion->broadcast('test_id', [], [$worker->id]), 'sent command';
  ok $minion->broadcast('test_id', [], [$worker->id, $worker2->id]), 'sent command';
  $worker->process_commands->register;
  $worker2->process_commands;
  is_deeply \@commands, [$worker->id, $worker->id, $worker2->id], 'right structure';
  @commands = ();
  ok $minion->broadcast('test_id'),       'sent command';
  ok $minion->broadcast('test_whatever'), 'sent command';
  ok $minion->broadcast('test_args', [23], []), 'sent command';
  ok $minion->broadcast('test_args', [1, [2], {3 => 'three'}], [$worker->id]), 'sent command';
  $_->process_commands for $worker, $worker2;
  is_deeply \@commands, [$worker->id, [23], [1, [2], {3 => 'three'}], $worker2->id], 'right structure';
  $_->unregister for $worker, $worker2;
  ok !$minion->broadcast('test_id', []), 'command not sent';
};

subtest 'Single process worker' => sub {
  my $worker = $minion->repair->worker->register;
  $minion->add_task(
    good_job => sub {
      my ($job, $message) = @_;
      $job->finish("$message Mojo!");
    }
  );
  $minion->add_task(
    bad_job => sub {
      my ($job, $message) = @_;
      die 'Bad job!';
    }
  );
  my $id  = $minion->enqueue('good_job', ['Hello']);
  my $id2 = $minion->enqueue('bad_job',  ['Hello']);
  while (my $job = $worker->dequeue(0)) {
    next unless my $err = $job->execute;
    $job->fail("Error: $err");
  }
  $worker->unregister;
  my $job = $minion->job($id);
  is $job->info->{state},  'finished',    'right state';
  is $job->info->{result}, 'Hello Mojo!', 'right result';
  my $job2 = $minion->job($id2);
  is $job2->info->{state},    'failed',            'right state';
  like $job2->info->{result}, qr/Error: Bad job!/, 'right error';
};

# Clean up once we are done
$pg->db->query('DROP SCHEMA minion_test CASCADE');

done_testing();