Node.js Nock models request timeout and subsequent success

I am trying to simulate service request timeouts in order to test the node requestretry request module, which allows you to specify a max # retry request and retry delay. To test this, I need to use nock to simulate a timeout for the first number of X requests, and then successfully respond to the same request. I know that there is a socketDelay () method to delay the connection, but how to indicate a successful response after this first delayed response?

I have this that simulates a timeout on first request

//delays the first request response by 1500 nock(urlHost) .post('/' + uriPath) .socketDelay(1500) .reply(200, 'response body'); 

but how can I make it respond faster to simulate service recovery? I want to do something in this direction

 //delays the first two request responses by 1500 nock(urlHost) .post('/' + requestIdentifier.ttoRoutingInfo.uriPath) .socketDelay(1500) .reply(200, 'response body') .times(2) //delays the third request by only 300 .then .socketDelay(300) .reply(200, 'response body'); 
+5
source share
1 answer

I will answer my question, as I understood it. It turns out that the knock allows queues for queues for the same endpoint, although it is not found anywhere in the documentation. This is what I used to simulate different delay times in queries. Note the different values ​​for each response 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 the requestretry module with a timeout = 300, retryDelay = 500 and maxAttempts = 2, the response should be the body of the third request from the first two timeouts. note that I used a different value for each response body to ensure that I am actually in the first two. You can verify that the first two requests failed because it took 1800 ms to complete the test. 300 ms (first timeout) + 500 ms (delay) + 300 ms (second timeout) + 500 ms (delay) + 200 ms (successful request)

+8
source

All Articles