@@ -626,22 +626,35 @@ describe('Job', () => {
626
626
627
627
describe ( 'start/stop' , ( ) => {
628
628
it ( 'starts/stops the job queue' , async ( ) => {
629
- // @TODO : this lint issue should be looked into: https://eslint.org/docs/rules/no-async-promise-executor
630
- // eslint-disable-next-line no-async-promise-executor
631
- return new Promise ( async resolve => {
632
- agenda . define ( 'jobQueueTest' , async ( job , cb ) => {
633
- await agenda . stop ( ) ;
634
- await clearJobs ( ) ;
635
- cb ( ) ;
636
- agenda . define ( 'jobQueueTest' , ( job , cb ) => {
637
- cb ( ) ;
638
- } ) ;
629
+ const processed = new Promise ( resolve => {
630
+ agenda . define ( 'jobQueueTest' , async _job => {
631
+ resolve ( 'processed' ) ;
632
+ } ) ;
633
+ } ) ;
634
+ await agenda . every ( '1 second' , 'jobQueueTest' ) ;
635
+ agenda . processEvery ( '1 second' ) ;
636
+ await agenda . start ( ) ;
637
+
638
+ expect (
639
+ await Promise . race ( [
640
+ processed ,
641
+ new Promise ( resolve => setTimeout ( ( ) => resolve ( `not processed` ) , 1100 ) )
642
+ ] )
643
+ ) . to . eq ( 'processed' ) ;
644
+
645
+ await agenda . stop ( ) ;
646
+ const processedStopped = new Promise ( resolve => {
647
+ agenda . define ( 'jobQueueTest' , async _job => {
639
648
resolve ( ) ;
640
649
} ) ;
641
- await agenda . every ( '1 second' , 'jobQueueTest' ) ;
642
- agenda . processEvery ( '1 second' ) ;
643
- await agenda . start ( ) ;
644
650
} ) ;
651
+
652
+ expect (
653
+ await Promise . race ( [
654
+ processedStopped ,
655
+ new Promise ( resolve => setTimeout ( ( ) => resolve ( `not processed` ) , 1100 ) )
656
+ ] )
657
+ ) . to . eq ( 'not processed' ) ;
645
658
} ) ;
646
659
647
660
it ( 'does not run disabled jobs' , async ( ) => {
@@ -795,27 +808,23 @@ describe('Job', () => {
795
808
796
809
describe ( 'job lock' , ( ) => {
797
810
it ( 'runs a recurring job after a lock has expired' , async ( ) => {
798
- let startCounter = 0 ;
799
-
800
- // @TODO : this lint issue should be looked into: https://eslint.org/docs/rules/no-async-promise-executor
801
- // eslint-disable-next-line no-async-promise-executor
802
- const processorPromise = new Promise ( async resolve =>
811
+ const processorPromise = new Promise ( resolve => {
812
+ let startCounter = 0 ;
803
813
agenda . define (
804
814
'lock job' ,
805
815
async ( ) => {
806
816
startCounter ++ ;
807
817
808
818
if ( startCounter !== 1 ) {
809
- expect ( startCounter ) . to . equal ( 2 ) ;
810
819
await agenda . stop ( ) ;
811
- resolve ( ) ;
820
+ resolve ( startCounter ) ;
812
821
}
813
822
} ,
814
823
{
815
824
lockLifetime : 50
816
825
}
817
- )
818
- ) ;
826
+ ) ;
827
+ } ) ;
819
828
820
829
expect ( agenda . definitions [ 'lock job' ] . lockLifetime ) . to . equal ( 50 ) ;
821
830
@@ -824,24 +833,21 @@ describe('Job', () => {
824
833
agenda . every ( '0.02 seconds' , 'lock job' ) ;
825
834
await agenda . stop ( ) ;
826
835
await agenda . start ( ) ;
827
- await processorPromise ;
836
+ expect ( await processorPromise ) . to . equal ( 2 ) ;
828
837
} ) ;
829
838
830
839
it ( 'runs a one-time job after its lock expires' , async ( ) => {
831
840
let runCount = 0 ;
832
841
833
- const processorPromise = new Promise ( async resolve =>
842
+ const processorPromise = new Promise ( resolve =>
834
843
agenda . define (
835
844
'lock job' ,
836
- async job => {
837
- // eslint-disable-line no-unused-vars
845
+ async _job => {
838
846
runCount ++ ;
839
-
840
- if ( runCount !== 1 ) {
841
- await agenda . stop ( ) ;
842
- resolve ( runCount ) ;
843
- } else {
847
+ if ( runCount === 1 ) {
844
848
await new Promise ( longResolve => setTimeout ( longResolve , 1000 ) ) ;
849
+ } else {
850
+ resolve ( runCount ) ;
845
851
}
846
852
} ,
847
853
{
@@ -850,21 +856,17 @@ describe('Job', () => {
850
856
)
851
857
) ;
852
858
853
- let errorHasBeenThrown = false ;
859
+ let errorHasBeenThrown ;
854
860
agenda . on ( 'error' , err => {
855
- if ( err . message . includes ( "execution of 'lock job' canceled" ) ) {
856
- errorHasBeenThrown = true ;
857
- } else {
858
- console . error ( err ) ;
859
- }
861
+ errorHasBeenThrown = err ;
860
862
} ) ;
861
863
agenda . processEvery ( 50 ) ;
862
864
await agenda . start ( ) ;
863
865
agenda . now ( 'lock job' , {
864
866
i : 1
865
867
} ) ;
866
868
expect ( await processorPromise ) . to . equal ( 2 ) ;
867
- expect ( errorHasBeenThrown ) . to . be . true ;
869
+ expect ( errorHasBeenThrown ?. message ) . to . includes ( "execution of 'lock job' canceled" ) ;
868
870
} ) ;
869
871
870
872
it ( 'does not process locked jobs' , async ( ) => {
@@ -915,7 +917,6 @@ describe('Job', () => {
915
917
await delay ( 200 ) ;
916
918
917
919
expect ( ( await agenda . getRunningStats ( ) ) . lockedJobs ) . to . equal ( 1 ) ;
918
- await agenda . stop ( ) ;
919
920
} ) ;
920
921
921
922
it ( 'does not on-the-fly lock more than definition.lockLimit jobs' , async ( ) => {
@@ -927,7 +928,6 @@ describe('Job', () => {
927
928
928
929
await delay ( 500 ) ;
929
930
expect ( ( await agenda . getRunningStats ( ) ) . lockedJobs ) . to . equal ( 1 ) ;
930
- await agenda . stop ( ) ;
931
931
} ) ;
932
932
933
933
it ( 'does not lock more than agenda._lockLimit jobs during processing interval' , async ( ) => {
@@ -947,7 +947,6 @@ describe('Job', () => {
947
947
948
948
await delay ( 500 ) ;
949
949
expect ( ( await agenda . getRunningStats ( ) ) . lockedJobs ) . to . equal ( 1 ) ;
950
- await agenda . stop ( ) ;
951
950
} ) ;
952
951
953
952
it ( 'does not lock more than definition.lockLimit jobs during processing interval' , async ( ) => {
0 commit comments