@@ -5,6 +5,7 @@ import sinon = require('sinon')
55import { noop } from 'lodash'
66import { DistributedCallbackQueue , MultiLockError , Semaphore } from '../src/distributed-callback-queue'
77import { LockAcquisitionError } from '@microfleet/ioredis-lock'
8+ import { setTimeout } from 'timers/promises'
89
910describe ( 'integration tests' , ( ) => {
1011 jest . setTimeout ( 10000 )
@@ -78,7 +79,7 @@ describe('integration tests', () => {
7879
7980 it ( '#push: job is performed only once' , ( ) => {
8081 const args = [ null , 'completed' ]
81- const job = sinon . spy ( ( next ) => setTimeout ( next , 500 , ...args ) )
82+ const job = sinon . spy ( ( next ) => global . setTimeout ( next , 500 , ...args ) )
8283 const onComplete = sinon . spy ( )
8384 const failedToQueue = sinon . spy ( )
8485 const unexpectedError = sinon . spy ( )
@@ -216,7 +217,7 @@ describe('integration tests', () => {
216217 it ( '#fanout: job is performed only once' , ( ) => {
217218 const args = [ 'completed' ]
218219 const job = sinon . spy ( async ( ) => {
219- await Promise . delay ( 500 )
220+ await setTimeout ( 500 )
220221 return [ ...args ]
221222 } )
222223 const onComplete = sinon . spy ( )
@@ -273,7 +274,7 @@ describe('integration tests', () => {
273274
274275 it ( '#fanout: fails after timeout' , async ( ) => {
275276 const job = sinon . spy ( async ( _ : any ) => {
276- await Promise . delay ( 3000 )
277+ await setTimeout ( 3000 )
277278 } )
278279 const arg1 = 'arg1'
279280 const onComplete = sinon . spy ( )
@@ -305,7 +306,7 @@ describe('integration tests', () => {
305306
306307 it ( '#fanout: fails after timeout even if lock has not been acquired' , async ( ) => {
307308 const job = sinon . spy ( async ( ) => {
308- await Promise . delay ( 3000 )
309+ await setTimeout ( 3000 )
309310 } )
310311 const onComplete = sinon . spy ( )
311312 const timeoutError = sinon . spy ( )
@@ -383,7 +384,7 @@ describe('integration tests', () => {
383384 await Promise . map ( queueManagers , async ( queueManager ) => {
384385 try {
385386 const lock = await queueManager . dlock . once ( 'once' )
386- await Promise . delay ( 1500 )
387+ await setTimeout ( 1500 )
387388 job ( )
388389 await lock . release ( )
389390 } catch ( err ) {
@@ -438,10 +439,13 @@ describe('integration tests', () => {
438439 const failedToQueue = sinon . spy ( )
439440 const unexpectedError = sinon . spy ( )
440441
441- await Promise . map ( queueManagers , async ( queueManager ) => {
442+ await Promise . map ( queueManagers , async ( queueManager , idx ) => {
442443 try {
443- const lock = await queueManager . dlock . multi ( '1' , '2' , '3' )
444- await Promise . delay ( 1500 )
444+ if ( idx !== 0 ) {
445+ await setTimeout ( 100 ) // give a chance for first idx to acquire _all_ locks so that test isnt flaky
446+ }
447+ const lock = await queueManager . dlock . multi ( '5' , '6' , '7' )
448+ await setTimeout ( 1500 )
445449 await lock . release ( )
446450 job ( )
447451 } catch ( err ) {
@@ -453,7 +457,7 @@ describe('integration tests', () => {
453457 }
454458 } )
455459
456- assert ( job . calledOnce , 'job was called more than once' )
460+ assert . equal ( job . callCount , 1 )
457461 assert . strictEqual ( failedToQueue . callCount , 9 , 'unexpected error was raised' )
458462 assert . strictEqual ( unexpectedError . called , false , 'fatal error was raised' )
459463 } )
@@ -479,7 +483,7 @@ describe('integration tests', () => {
479483 // if it's possible for other contestants
480484 // to run out of semaphore lock - counter will
481485 // increase multiple times before resolving following promise
482- await Promise . delay ( 10 )
486+ await setTimeout ( 10 )
483487
484488 // return the counter
485489 return counter - 1
0 commit comments