import Mutex from './Mutex' ; import * as expect from 'expect' ; describe ( 'Mutex' , ( ) => { function wait ( time ) { return new Promise ( resolve => setTimeout ( ( ) => resolve ( time ) , time ) ) ; } function now ( task ) { return new Promise ( ( resolve , reject ) => { setImmediate ( ( ) => { task ( ) . then ( resolve ) . catch ( reject ) ; } ) ; } ) ; } it ( 'should process tasks in order' , ( ) => { let outs = [ ] ; let mutex = new Mutex ( ) ; return Promise . all ( [ now ( ( ) => mutex . synchronize ( ( ) => wait ( 2 ) ) . then ( ( num ) => outs . push ( num ) ) ) , now ( ( ) => mutex . synchronize ( ( ) => wait ( 7 ) ) . then ( ( num ) => outs . push ( num ) ) ) , now ( ( ) => mutex . synchronize ( ( ) => wait ( 3 ) ) . then ( ( num ) => outs . push ( num ) ) ) ] ) . then ( ( ) => expect ( outs ) . toEqual ( [ 2 , 7 , 3 ] ) ) ; } ) ; it ( 'should process tasks in order by queue name' , ( ) => { let outs = [ ] ; let mutex = new Mutex ( ) ; return Promise . all ( [ now ( ( ) => mutex . synchronizeOn ( 'honk' , ( ) => wait ( 2 ) ) . then ( ( num ) => outs . push ( num ) ) ) , now ( ( ) => mutex . synchronizeOn ( 'beep' , ( ) => wait ( 7 ) ) . then ( ( num ) => outs . push ( num ) ) ) , now ( ( ) => mutex . synchronizeOn ( 'honk' , ( ) => wait ( 3 ) ) . then ( ( num ) => outs . push ( num ) ) ) ] ) . then ( ( ) => expect ( outs ) . toEqual ( [ 2 , 3 , 7 ] ) ) ; } ) ; } ) ;