import { range } from 'lodash' ; import { BlockedQueue } from './BlockedQueue' ; describe ( 'BlockedQueue' , ( ) => { it ( 'first in and first out' , async ( ) => { const queue = new BlockedQueue < number > ( 10 ) ; const sequence = range ( 0 , 10 ) ; for ( const i of sequence ) { queue . put ( i ) ; } for ( const i of sequence ) { await expect ( queue . take ( ) ) . resolves . toEqual ( i ) ; } } ) ; it ( 'throw error when put items more than maxSize' , ( ) => { const size = 10 ; const queue = new BlockedQueue < number > ( 10 ) ; const sequence = range ( 0 , 10 ) ; for ( const i of sequence ) { queue . put ( i ) ; } expect ( ( ) => queue . put ( 0 ) ) . toThrow ( 'BlockedQueue exceed max size' ) ; } ) ; it ( 'block take() when queue is empty' , async ( ) => { const queue = new BlockedQueue < number > ( 10 ) ; const delay = 1000 ; const startTs = new Date ( ) ; let msecondTooks ; const takePromise = queue . take ( ) . then ( ( ) => ( msecondTooks = new Date ( ) . getTime ( ) - startTs . getTime ( ) ) ) ; setTimeout ( ( ) => queue . put ( 0 ) , delay ) ; await takePromise ; expect ( msecondTooks ) . toBeGreaterThanOrEqual ( delay ) ; } ) ; it ( 'block takeAll() with batchsize' , async ( ) => { const queue = new BlockedQueue < number > ( 10 ) ; const sequence = range ( 0 , 10 ) ; for ( const i of sequence ) { queue . put ( i ) ; } await expect ( queue . takeAll ( 6 ) ) . resolves . toEqual ( [ 0 , 1 , 2 , 3 , 4 , 5 ] ) ; await expect ( queue . takeAll ( 6 ) ) . resolves . toEqual ( [ 6 , 7 , 8 , 9 ] ) ; } ) ; it ( 'block takeAll() without max batchsize' , async ( ) => { const queue = new BlockedQueue < number > ( 10 ) ; const sequence = range ( 0 , 10 ) ; for ( const i of sequence ) { queue . put ( i ) ; } await expect ( queue . takeAll ( ) ) . resolves . toEqual ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , ] ) ; } ) ; it ( 'block takeAll() when queue is empty' , async ( ) => { const queue = new BlockedQueue < number > ( 10 ) ; const delay = 1000 ; const startTs = new Date ( ) ; let msecondTooks ; const takePromise = queue . takeAll ( ) . then ( ( ) => ( msecondTooks = new Date ( ) . getTime ( ) - startTs . getTime ( ) ) ) ; setTimeout ( ( ) => queue . put ( 0 ) , delay ) ; await takePromise ; expect ( msecondTooks ) . toBeGreaterThanOrEqual ( delay ) ; } ) ; it ( 'throw error when putAll items more than maxSize' , ( ) => { const size = 10 ; const queue = new BlockedQueue < number > ( 10 ) ; const sequence = range ( 0 , 10 ) ; queue . putAll ( sequence ) ; expect ( ( ) => queue . putAll ( [ 11 , 12 , 13 ] ) ) . toThrow ( 'BlockedQueue exceed max size' ) ; } ) ; } ) ;