Search code examples
load-testinggatlingscala-gatling

Access session value in gatling checks


I use gatling to send data to an ActiveMQ. The payload is generated in a separate method. The response should also be validated. However, how can I access the session data within the checks check(bodyString.is()) or simpleCheck(...)? I have also thought about storing the current payload in a separate global variable, but I don't know if this is the right approach. My code's setup looks like this at the moment:

val scn = scenario("Example ActiveMQ Scenario")
  .exec(jms("Test").requestReply
    .queue(...)
    .textMessage{ session => val message = createPayload(); session.set("payload", payload); message}
    .check(simpleCheck{message => customCheck(message, ?????? )})) //access stored payload value, alternative: check(bodystring.is(?????) 

def customCheck(m: Message, string: String) = {
 // check logic goes here
}

Solution

  • Disclaimer: providing example in Java as you don't seem to be a Scala developper, so Java would be a better fit for you (supported since Gatling 3.7).

    The way you want to do things can't possibly work.

    .textMessage(session -> {
       String message = createPayload();
       session.set("payload", payload);
       return message;
      }
    )
    

    As explained in the documentation, Session is immutable, so in a function that's supposed to return the payload, you can't also return a new Session.

    What you would have to do it first store the payload in the session, then fetch it:

    .exec(session -> session.set("payload", createPayload()))
    ...
    .textMessage("#{payload}")
    

    Regarding writing your check, simpleCheck doesn't have access to the Session. You have to use check(bodyString.is()) and pass a function to is, again as explained in the documentation.