Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman / Newman retry in case of failure

Tags:

postman

newman

In Newman I want to tests to make sure that response code is correct, response time is reasonable and that response values are correct.

In some cases, due to network hiccups or some other system conditions, some requests might end up with timeouts or incorrect values that will resolve if the same request was processed a few seconds later.

in such cases I would like to retry the same request x times with a Y timeout between requests.

If an iteration pass after a retry, I would like the Newman exit code to be 0 (successful run).

like image 328
OBender Avatar asked Apr 19 '17 12:04

OBender


2 Answers

After few hours i had Ended with a function Like this:

function retryOnFailure(successCode, numberOfRetrys) {
    var key = request.name + '_counter';
    var execCounter = postman.getEnvironmentVariable(key) || 1;

    var sleepDuration = 1000;
    var waitUntilTime = new Date().getTime() + sleepDuration;
    if (responseCode.code !== successCode && execCounter <= numberOfRetrys) {
        while (new Date().getTime() < waitUntilTime) {
            // Do Nothing -> Wait
        }
        console.log('Retrying: ' + request.name + '\nGot: ' + responseCode.code + ' Expected: ' + successCode + '\nWaited: ' + sleepDuration / 1000 + 'sec  \nRetry Number: ' + execCounter + ' of ' + numberOfRetrys);
        execCounter++;
        postman.setEnvironmentVariable(key, execCounter);
        postman.setNextRequest(request.name);
    }
}

Usage:

retryOnFailure(404, 4);
like image 81
OBender Avatar answered Sep 27 '22 20:09

OBender


You can setup a request workflow like this:

Create a collection with a request, then:

In the pre-request tab you can implement a counter:

// Counter for number of requests
var counter = environment.counter ? _.parseInt(environment.counter) + 1 : 1;
postman.setEnvironmentVariable("counter", counter);

Your tests tab would look like this:

const code = (responseCode.code === 200);

if (code === 200 && environment.counter < X) {
    // Stop execution
    tests["Status code is 200"] = code;
    postman.setNextRequest();
}
else {
    // retry the same request
    postman.setNextRequest("Name of this request");
}

A timeout for the request itself can be configured with the newman CLI:

newman run myCollection.json --timeout-request Y
like image 35
Sergej Lopatkin Avatar answered Sep 27 '22 20:09

Sergej Lopatkin