Handle Timeouts with postman - newman

87 views Asked by At

After deploying my new environment, in CI, postman scripts will help to do somme health check to ensure that every thing is OK. Servers and services will take a not predictable amount of time to be up.

In my postman collection, I am retrying requests (for a certain time) until having the server and services up . In CI newman runs this collection and at the end of execution , it receives timeout error and fails.

I don't want these timeouts to be considered as errors. Because I have assertions that will finally check if everything is ok. And timeouts could be normal.

Do you have any solution to just consider assertions and not retry timeout errors.

Best wishes.

I tried with postman to set a global environment variable and set it to a false with theses timeouts and set the newman exitcode to ok when this variable is false. I was not able to read this variable in CI (windows shell)

I tried to set --bail assertion and I thought it could only consider assertions to set the newman exit code, but not worked.

This is my pre-request script:

var maxAttempts = 10;
var delayBetweenAttempts = 20000; // 5 secondes
var addr = pm.variables.get("addr-ip");

function retryRequest(attempt) {
    if (attempt <= maxAttempts) {
    pm.sendRequest("http://" + addr + "/app/home", function (err, response) {
        if (response && response.code === 200) {
            console.log("Reuest successful after " + attempt + " attempt.");
        } else {
            console.log("Attempt #" + attempt + " failed. Retry in " + (delayBetweenAttempts / 1000) + " secondes.");
            setTimeout(function() {
                // retry 
                retryRequest(attempt + 1);
            }, delayBetweenAttempts);
        }
    });
    }
    else {
        console.log("Max attempts reached. Request failed.");
        throw new Error("Service is not running");
    }
}

// First try
retryRequest(1);
0

There are 0 answers