I have an API that gets the status of a job. It parses the response body and looks for the "state:" of the job.
If it's INPROGRESS I'm telling the request to wait 10 seconds and rerun, but this doesn't happen, it just moves on to the next.
This is the test I'm running:
pm.environment.unset("JobStatus");
var jsonData = pm.response.json();
pm.environment.set("JobStatus", jsonData['progress-update'].state);
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds));
};
pm.environment.get("JobStatus");
if ( environment.JobStatus == "DONE")
{
postman.setNextRequest("Export Results");
}
else if ( environment.JobStatus == "INPROGRESS")
{
sleep(10000).then(() => {
postman.setNextRequest("Get Job Status");
})}
else {
postman.setNextRequest(null);
}
The data has a main task that I'm checking for and then many sub-tasks with the same state INPROGRESS string.
{
"progress-update": {
"@type": "parallel-progress",
"job": {
"@href": "/api/space/job-management/jobs/4755716"
},
"taskId": 4755716,
"jobName": "Compare Config-4755716",
"state": "INPROGRESS",
"status": "UNDETERMINED",
"percentage": 10,
"subTask": [
{
"@type": "percentage-complete",
"state": "INPROGRESS",
"status": "UNDETERMINED",
"percentage": 0
},
The variable is being set correctly, but my else if statement is not being followed.
EDIT:
I changed up the logic to set the variable in a test on Request 4 and do the checking on Pre-Script Request 5. This should send collection runner back to Request 4 until the JobStatus == "DONE" but it's not.
I added some additional code to help me see how it's performing from the console:
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds));
};
pm.environment.get("JobStatus");
if ( environment.JobStatus === "DONE")
{
postman.setNextRequest("Export Results");
console.log("DONE")
console.log(environment.JobStatus)
}
else if ( environment.JobStatus === "INPROGRESS")
{
sleep(10000).then(() => {
postman.setNextRequest("Get Job Status");
console.log("INPROGRESS")
console.log(environment.JobStatus)
})}
else {
postman.setNextRequest(null);
console.log("ELSE")
console.log(environment.JobStatus)
}
When this is executed I get
INPROGRESS
INPROGRESS
Printed in the console. and it just finishes the request, without ever sending itself back to request 4 to retry.
Why is the else if logic being followed but the setNextRequest
being ignored?
For those that may run into this I found the issue, or rather two issues.
My environment.JobStatus variable was not being set and i couldn't figure out how to get it to do so. So i scrapped the environment variable and just declared it within JS.
Secondly, my postman.setNextRequest("Get Job Status") was being ignored because it was nested within the sleep function. Not sure why that caused it but i dropped it out of the sleep and it loops perfectly fine now.
var jsonData = pm.response.json();
var jobState = jsonData['progress-update'].state
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds));
};
if ( jobState === "DONE")
{
console.log(jobState);
}
else if ( jobState === "INPROGRESS")
{
sleep(10000).then(() => {
console.log(jobState);
})
postman.setNextRequest("Get Job Status");
}
else {
console.log("ELSE Clause was matched.");
console.log(jobState);
postman.setNextRequest(null);
}