Force simulation failure if a cookie is not present at the end

Hi,

At the end of my scenario I need to check if a cookie is present. If this cookie is not present the simulation must be considered as a failure. I’ve tried the following code

exec((session:Session)=>{
val cookieNotPresent = CookieHandling.getStoredCookies(session,REQUESTED_PAGE_URL).find((c:Cookie)=>{c.getName().startsWith(COOKIE_NAME_PREFIX)}).isEmpty
if(cookieNotPresent){
throw new RuntimeException(“Cookie not present at the end of the scenario”)
}
session
})

But even if the exception is raised the simulation is considered as successful only the stack strace is printed in the console. Is there another way to force the simulation to fail ?

In advance thank you.

Sébastien

Hi,

Several things:

.exec(session => {
val cookieNotPresent = !CookieHandling.getStoredCookies(session, REQUESTED_PAGE_URL).contains((c: Cookie) => { c.getName.startsWith(COOKIE_NAME_PREFIX) })
if (cookieNotPresent) session.setFailed else session
})
.exitHereIfFailed

BTW, use !contains instead of find + isEmpty.

Cheers,

Stéphane

Thanks for the quick reply.

I really need to force the simulation failure or to have a check that shows KO because this cookie has to be there. If it isn’t then there is a bug. Since this is the last step of my scenario, I cannot exit on fail because then I cannot differentiate an execution with or without error.

I’ll try to implement a custom check but I’ve seen on the wiki (https://github.com/excilys/gatling/wiki/Checks-API) that this page is obsolete. Is there a more recent version ? After implementation is it possible to write something like the following (regarding type checking and execution)

exec(customCheck(session))

In advance thank you.

Sébastien

I really need to force the simulation failure or to have a check that
shows KO because this cookie has to be there. If it isn't then there is a
bug. Since this is the last step of my scenario, I cannot exit on fail
because then I cannot differentiate an execution with or without error.

I have the feeling that we don't understand each other. What do you want
exactly?

   1. Violently stop all the users because one of them failed because of
   this missing cookie? That's the meaning of "force the simulation failure".
   That's something that's been requested once in a while, and I'm still not
   convinced that it's actually useful (meaning, I'd need a serious use case,
   not "that would be cool if we could").
   2. Count the request as failed in the stats.

As of today, we only have documented the Check DSL and not the API
underneath because it's not considered as stable and will go under a major
refactor in Gatling 2. We will make it a public API then and document it
properly.
Actually, in order to achieve 2, you might not need to go as deep as write
a custom check (I haven't tested), with a combination of
https://github.com/excilys/gatling/wiki/Checks#wiki-header-value on the
Set-Cookie header, findAll, and
https://github.com/excilys/gatling/wiki/Checks#wiki-transforming

Stéphane

You are right. The best would be not to stop the simulation because maybe only one user has got this error. Therefore a solution would be to have some kind of indicator to show that one user has encountered a problem like counting this request as failed as you suggested.

I think this idea is not suitable in my case because I don’t have a request right before this check. I have two paths that lead to this check therefore it would require to put this check on each of these paths. Moreover on one path I have some redirections therefore I cannot check the http header and I would prefer to let gatling follow the redirection for complexity reason. Here is the structure of the scenario.

exec(
//Http request + store end location (could be two)
)
.doIf(one specific location){
//Some stuff
}
.exec(
//Check cookie
)

Sébastien

OK, so what you actually need is a custom Action (what’s being called by exec).
Basically, that’s what you’ve been doing so far, except that you have to notify the DataWriters (the components is charge of logging).
See https://github.com/excilys/gatling/blob/1.4.X/gatling-core/src/main/scala/com/excilys/ebi/gatling/core/result/writer/DataWriter.scala#L54

.exec(session => {

import com.excilys.ebi.gatling.core.result.writer.DataWriter
import com.excilys.ebi.gatling.core.util.TimeHelper.nowMillis
import com.excilys.ebi.gatling.core.result.message.RequestStatus.{ OK, KO }

val cookiePresent = CookieHandling.getStoredCookies(session, REQUESTED_PAGE_URL).contains((c: Cookie) => { c.getName.startsWith(COOKIE_NAME_PREFIX) })
val now = nowMillis
val status = if (cookiePresent) OK else KO
DataWriter.logRequest( session.scenarioName,
session.userId,
“Cookie Check”,
now,
now,
now,
now,
status)

session
})

Note that this will have a impact on your global stats as this will produce 0ms response time requests.

Thanks a lot. This solution works fine.

Sébastien

You’re welcome.

Have fun!

.exec(session => {
    assert(requestStatus.eq("pass"))
  session
})

-- using this line the assertion is failing which is expected but not the test. Can someone advice?