Looping based on condition

Hi Sebastien/Stephane,

I am having an scenario where in I need to execute an particular API call based on a certain condition.

The condition with which I need to execute that call is extracted from that API call itself.

Below is the detailed scenario

  1. Execute API call. Saving C_Completed from response
  2. While C_Completed == null
    execute that API call again
    Extract C_Completed from the last API call and
    then again compare if its == null

My code snippet

.repeat(1) {

.exec(http(“A”)
.get("/API/")
.headers(SA)
.check(jsonPath("$…completed_at").find.optional.saveAs(“C_Completed”))
.check(status.is(200))))

.doWhile(session => session(“C_Completed”)==(null)){exec(http(“A”)

.get("/API/")
.headers(SA)
.check(jsonPath("$…completed_at").find.optional.saveAs(“C_Completed”))
.check(status.is(200)))
.pause(1)

.exec(session => {
println(session(“C_Completed”).validate[String])
session
})}

}
Observations:

1. The code inside while loop executed only a single time alone. Even when the condition got satisficed.

From the https://gatling.io/docs/current/general/scenario/ doc I read that the condition is evaluated after the loop. I guess it will be done automatically with the value stored in it.

  1. If it got evaluated then it should have run till the C_Completed!=null

Could you please help me on this. What was wrong with my code or is there any other issues? Thanks in advance.

Regards,
Aravind

session(“C_Completed”)==null

Wrong usage of the Session API.
Use asOption and check if it’s defined.

Hi All,

I found the root cause of the issue. The code is fine.
The issue is with the null value returned in the response. Failure(Value is null) is the valued which I got for the C_Completed parameter.
Hence the script executed only once , since it didn’t satisfy the condition. Is there any way to get the null response value as such?

Thanks in advance

Hi Stephane,

I covered the same in one of my earlier tries but still faced the same issue.
I tried the below when I give the condition as C_Completed" != null. The loop is getting executed. My problem is I couldn’t extract the exact null value from the response. Instead I am getting Failure(Value is null) in the session value because of which I couldn’t handle it.

.exec(session => session.set(“C_Completed”, “C_Completed”))

.doWhile(“C_Completed” == null)(exec(http(“script-status”)
.get(“A”)
.headers(SA)
.check(jsonPath("$…completed_at").find.optional.saveAs(“C_Completed”))
.check(status.is(200)))
.pause(1)

.exec(session => {
println(session(“C_Completed”).validate[String])
session
})

.exec(session => session.set(“C_Completed”, “C_Completed”)))

doWhile(“C_Completed” == null)

You’re comparing the constant String “C_Completed” with null, which obviously will always be false.

Hi Stephane,

Sure I will follow the terms.
I am using Gatling 3.4.0,

"completed_at": null ( part of API response ) . I should extract only null from this .
Then save that null to C_completed.

But when I extract and print it I am seeing Failure(Value is null) assigned to C_completed.

I check the forum and I could see it belongs to CCE. Hence I used Validate as well but still I couldn’t fetch the exact null value.

Hi All,

I handled this scenario by using regex check instead of using jsonPath. Its working as expected now.

Regards,
Aravind