import should from "should" ; import { makeRetryDelay , rand , RetryDelay } from "../src/retries" ; describe ( "rand" , ( ) => { it ( "Should generate numbers between min and max" , ( ) => { for ( let i = 0 ; i < 100000 ; i ++ ) { const r = rand ( 1 , 2 ) ; should ( r ) . be . within ( 1 , 2 ) ; } } ) ; } ) ; describe ( "makeRetryDelay" , ( ) => { function getRetries ( f , n ) { const ret = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { ret . push ( f ( i ) ) ; } return ret ; } it ( "Should not retry when retries is 0" , ( ) => { const delay = makeRetryDelay ( { retries : 0 } ) ; should ( delay ( 0 ) ) . equal ( - 1 ) ; } ) ; it ( "Should generate retries for defaults without random" , ( ) => { const delay = makeRetryDelay ( { randomize : false } ) ; should ( getRetries ( delay , 10 ) ) . eql ( [ 100 , 200 , 400 , 800 , 1000 , 1000 , 1000 , 1000 , 1000 , 1000 ] ) ; } ) ; it ( "Should generate retries for defaults" , ( ) => { const delay = makeRetryDelay ( { } ) ; const retries = getRetries ( delay , 10 ) ; should ( retries . shift ( ) ) . be . within ( 100 , 200 ) ; should ( retries . shift ( ) ) . be . within ( 200 , 400 ) ; should ( retries . shift ( ) ) . be . within ( 400 , 800 ) ; should ( retries . shift ( ) ) . be . within ( 800 , 1000 ) ; should ( retries . shift ( ) ) . be . within ( 900 , 1000 ) ; should ( retries . shift ( ) ) . be . within ( 900 , 1000 ) ; should ( retries . shift ( ) ) . be . within ( 900 , 1000 ) ; should ( retries . shift ( ) ) . be . within ( 900 , 1000 ) ; should ( retries . shift ( ) ) . be . within ( 900 , 1000 ) ; should ( retries . shift ( ) ) . be . within ( 900 , 1000 ) ; } ) ; } ) ;