How to retry http().get() on a specific error code

I’m pretty new to Gatling, but I have a currently running test that I need to modify. I have a RESTful http endpoint that can either return the data I need, or it can return a 503 error with a number of seconds I should wait and then re-try. Any other error should be considered failure.

From the Gatling docs, it looks like looping and retries are implemented at the “scenario” level and not at the HTTP Request. I can live with that limitation, but I can’t figure out how to use check() or checkIf() to “break” from the following HTTP Requests and go back to the beginning of the scenario.

Any ideas?

Regards,
dr

So I re-went through the tutorial again and this is sort of what I’m trying to accomplish. Of course, this code doesn’t compile, probably because I’m trying to use checkIf() in a way it wasn’t meant to be used…

class firstTest extends Simulation {

val httpProtocol = http
.baseURL(“http://httpstat.us”)
.inferHtmlResources()
.acceptHeader(“text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8”)
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“en-US,en;q=0.5”)
.userAgentHeader(“Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0”)

val headers_0 = Map(“Upgrade-Insecure-Requests” → “1”)

val scn = scenario(“firstTest”)
.exec(http(“request_0”)
.get("/503")
.checkIf(status.is(503)) {
tryMax(5) {
pause(5) //TODO get pause duration from the 503
exec(http(“retry_503”)
.get("/503")
}
}
.headers(headers_0))

setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}

Third try is the charm. I’m fairly certain “checkIf” doesn’t do what I thought it would do, but asLongAs() fits my use case. I need to fully test this, but I’m already wondering why the truth test doesn’t work correctly if I put quotes around 200. This is definitely not what I expect, as a bare 200 looks like an Int and not a String!

import scala.concurrent.duration._

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

class firstTest extends Simulation {

object Retry {
val retry = asLongAs(session => session(“ret_code”).as[String] != 200) {
pause(5) //TODO - use the duration returned from the endpoint
.exec(http(“retrying”)
.get("/")
.check(status.in(200,503),status.saveAs(“ret_code”), bodyString.saveAs(“ret_body”))
).exitHereIfFailed
}
}

val httpProtocol = http
.baseURL(“http://httpstat.us”)
.inferHtmlResources()
.acceptHeader(“text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8”)
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“en-US,en;q=0.5”)
.userAgentHeader(“Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0”)

val headers_0 = Map(“Upgrade-Insecure-Requests” → “1”)

val scn = scenario(“firstTest”)
.exec(http(“request_0”)
.get("/503")
.check(status.saveAs(“ret_code”), bodyString.saveAs(“ret_body”)) //TODO - instead of the body, get the duration to wait from the endpoint
.headers(headers_0)
)
.exec(Retry.retry)

setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}