javascript - Node.js Nock simulate request timeout and subsequent success -


i'm trying simulate service request timeouts test node requestretry module, allows specify request max # of attempted retries , retry delay. in order test this, need use nock simulate timeout first x number of requests, respond same request. know there 'socketdelay()' method delay connection, how specify successful response after first delayed response?

i have this, simulates timeout on first request

//delays first request's response 1500 nock(urlhost)      .post('/' + uripath)      .socketdelay(1500)      .reply(200, 'response body'); 

but how can make respond faster after simulate service recovering? i'm looking along these lines

//delays first 2 request's responses 1500 nock(urlhost)     .post('/' + requestidentifier.ttoroutinginfo.uripath)     .socketdelay(1500)     .reply(200, 'response body')     .times(2)   //delays third request 300     .then     .socketdelay(300)     .reply(200, 'response body'); 

i'll answer own question since figured out. turns out nock allows queueing mocks same endpoint, although isn't anywhere in documentation. used simulate varied delay times in requests. notice different values each reply body

 var nockscope = nock(urlhost)         .post('/' + uripath)         .delayconnection(400)         .reply(200, 'response body1')         .post('/' + uripath)         .delayconnection(400)         .reply(200, 'response body2')         .post('/' + uripath)         .delayconnection(200)         .reply(200, 'response body3');    expect(data).to.equal('response body3'); 

after using requestretry module timeout=300, retrydelay=500 , maxattempts=2, response should body of third request since first 2 time out. notice used different value each response body ensure i'm timing out on first two. can verify first 2 requests failed because test took 1800ms complete. 300ms(first timeout) + 500ms(delay) + 300ms(second timeout) + 500ms(delay) + 200ms(successful request)


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -