Node.js Nock模拟请求超时和随后的成功

我试图模拟服务请求超时testing节点requestretry模块,它允许您指定请求最大尝试重试次数和重试延迟。 为了testing这个,我需要使用nock来模拟第一个X个请求的超时,然后成功地响应相同的请求。 我知道有'socketDelay()'方法来延迟连接,但是如何在第一个延迟响应之后指定一个成功的响应呢?

我有这个,它模拟了第一个请求的超时

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

但是如何才能更快地响应模拟服务恢复呢? 我正在寻找这方面的工作

 //delays the first two request's 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'); 

自从我弄清楚以后,我会回答我自己的问题。 原来,nock允许为相同的端点排队模拟,尽pipe它不在文档中的任何地方。 这是我用来模拟请求中不同的延迟时间。 注意每个回复主体的不同值

  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'); 

在使用timeout = 300,retryDelay = 500和maxAttempts = 2的requestretry模块之后,响应应该是第一个两次超时后的第三个请求的主体。 注意我为每个响应主体使用了不同的值,以确保我实际上在前两个时间超时。 您可以validation前两个请求失败,因为testing需要1800毫秒才能完成。 300ms(第一次超时)+ 500ms(延迟)+ 300ms(第二次超时)+ 500ms(延迟)+200ms(成功请求)