Hello List, I’m using Gatling 3.0.2 and I’ve tried a few strategies to KO a session when an asLongAs counter reaches a certain limit, but I can’t seem to wrap my head around it.
It might be that I have to write a custom check, but most of the examples I’ve seen are based on checks that are validating a response result. I think the Success and Failure classes may be the answer, but as you can see below I had some trouble implementing:
object Callbacks {
val checkSentCallback: ChainBuilder = exec(
exec()
.exec(
asLongAs(keepPolling, “i”) {
exec(
http(“Look for sent callback”)
.get(“https://mycallbacks.com/id=${id}”)
.check(status.saveAs(“callbackResult”))
).exec(session => {loopCheck(session)
session
}).exitHereIfFailed
.pause(1)
doIf(session => session(“i”).as[Int] >= Constants.callbackCheckLimit)
{
exec(session => {
println(" ------- Do if: Fail Session -------")
session.markAsFailed
session})
}
}
).exitHereIfFailed
)
private def keepPolling(sess: Session): Validation[Boolean] = {
val waiting = 404
val generationStatus = sess(“callbackResult”).asOption[Int].getOrElse(waiting)
println(“callback stat: [” + sess(“callbackTest”).as[String] + “] ct:” + sess(“i”).as[String] + " stat:" + generationStatus)
generationStatus != 200 && sess(“i”).as[Int] < (Constants.callbackCheckLimit + 1)
}
private def loopCheck(sess: Session): Session = {
println(" ------- " + sess(“i”).as[Int] + " > " + Constants.callbackCheckLimit + " -------")
if (sess(“i”).as[Int] >= Constants.callbackCheckLimit) {
println(" ------- Fail Session -------")
sess.markAsFailed
}
sess
}
}
class LoopChecker(wrapped: Check[Response], scope: HttpCheckScope, responseBodyUsageStrategy: Option[ResponseBodyUsageStrategy]) extends Validation[String] {
val okResult: Validation[CheckResult] = Success(“Still checking”)
val koResult: Validation[String] = Failure("Max callback polling iterations reached: " + Constants.callbackCheckLimit)
override def check(response: Response, session: Session): Validation[CheckResult] = {
if (session(“i”).as[Int] >= Constants.callbackCheckLimit) {
return koResult
}
return okResult
}
}
Any tips would be greatly appreciated!
Cheers,
Tip: check the Session API documentation page, in particular the warnings.
I’d read those, but had (mistakenly) thought that an exec statement that returned the session would replace it. I understand that they are immutable, but other than chaining set operations, the documentation doesn’t seem to give clear guidelines on how to update session state other than through checks. I’d be happy to use a custom check and while I’ve heard them referenced here and read the documentation, I’ve been unable to suss out how to apply it in this context.
After reading this documentation, don’t you see what’s wrong in here?
exec(session => {
session.markAsFailed
session})
Hi Stéphan, thanks for your patience…
Do you mean this warning: ``Session instances are immutable!
On this page? https://gatling.io/docs/current/session/session_api/#id2
If so, I’m sorry, I don’t see it. As I mentioned in my previous email, the suggestions below that warning do not have enough context to show how to replace the current session state with a new one. I’m not asking to be spoon fed, and I have reviewed every posting in this list having to do with failing a session. Perhaps it’s my functional programming blindspot?
Nah, it’s not a matter of functional programming.
Let’s write what you’re doing a la Java:
exec(session => {
val newSessionMarkedAsFailed = session.markAsFailed
return session
})
Get it now?
Little bit of extra experimentation and I think I’ve got it, session was scoped to that exec block? So, I got that working but then recalled that marking as failed does not KO the session, KO’ing the session marks it as failed. I now have a hack using a checkif that is behaving ‘properly’:
// terminates session, but does not ko
doIf(session => session(“i”).as[Int] >= Constants.callbackCheckLimit)
{
exec(_.markAsFailed)
}
// whereas this KO’s the session when we’ve reached check limit (and the last http result was not 200
val checkSentCallback: ChainBuilder = exec(
exec()
.exec(
asLongAs(keepPolling, “i”) {
exec(
http(“Look for sent callback”)
.get(“https://mycallbacks.com/id=${id}”)
.check(status.saveAs(“callbackResult”))
.check(checkIf((s: Session) => s(“i”).as[Int] == Constants.callbackCheckLimit)(status.is(200)))
).pause(1)
}.exitHereIfFailed
)
)
Appreciate your help!