This will be a little bit tricky since JavaScript doesn’t really have a “sleep” API to pause for an exact amount of time when running procedural code. Also, if your test code is generating new requests, those are done in Promises and there’s no guarantee when they would return, so you would have to write out the test in a nested format like this:
// create a folder setup like this:
+ Collection Name
|
+-- Subfolder
|
+--- request
Then, in the “Collection” folder’s pre-request script, set an environment variable like this:
pm.environment.set('delay-time', 2)
Then, in your “request” test script, do something like this:
var delay = pm.environment.get('delay-time');
pm.sendRequest("https://localhost/request1", function (err, response) {
// handle the response here
// figure out some way to sleep for "DELAY" seconds
pm.sendRequest("https://localhost/request2", function (err, response) {
// handle the response here
// figure out some way to sleep for "DELAY" seconds
pm.sendRequest("https://localhost/request3", function (err, response) {
// handle the response here
// figure out some way to sleep for "DELAY" seconds
if (delay * 2 < 64) {
pm.environment.set('delay-time', delay * 2)
pm.setNextRequest('request')
} else {
pm.setNextRequest(null)
}
});
});
});
Because pm.sendRequest()
is asynchronous, you can’t simply do them “in order” as they would all get called around the same time and you wouldn’t know which one would respond first … nesting them in the structure I’ve outlined above should at least ensure that request 1 will be called, and then progress to 2, then to 3, at which point the code will do your exponential back-off and retry the API endpoints again by called pm.setNextRequest
… once you hit a threshold of some sort, in this case I say less than 64, then you call pm.setNextRequest(null)
which will stop the looping.
Another approach would be to make this structure:
// create a folder setup like this:
+ Collection Name
|
+-- Subfolder
|
+--- request1
+--- request2
+--- request3
… and the test code for each of those three requests would be something like this maybe:
// request 1's test code:
var delay = pm.environment.get('delay-time');
// figure out how to "sleep" for that many seconds
pm.setNextRequest('request2')
then
// request 2's test code:
var delay = pm.environment.get('delay-time');
// figure out how to "sleep" for that many seconds
pm.setNextRequest('request3')
then
// request 3's test code:
var delay = pm.environment.get('delay-time');
// figure out how to "sleep" for that many seconds
if (delay * 2 < 64) {
pm.environment.set('delay-time', delay * 2)
pm.setNextRequest('request1') // start over
} else {
pm.setNextRequest(null)
}