2

I am using sessions in gatling to store values, as shown below

      exec(session => {
        val id = Instant.now.toEpochMilli.toString + scala.util.Random.nextInt(1000).toString
        session.set("STARTED_PROCESS_ID",id)
        //Store the id somewhere for processing later
        session
      })
      .exec(
          http("scenario")
            .post(url)
            .header("Content-Type", "application/json")
            .header("id", session => session("STARTED_PROCESS_ID").as[String])
            .body(StringBody(body)
            .check(status.is(200))

According to the documentation, the value should be stored in session & the header "id" should be populated as expected. But when running the simuation I get the following error

java.util.NoSuchElementException: No attribute named 'STARTED_PROCESS_ID' is defined
    at io.gatling.core.session.SessionAttribute.as(Session.scala:46)
    at common.HttpUtil$.$anonfun$sendPostRequestForWasStartDefLoad$1(HttpUtil.scala:557)
    at io.gatling.core.action.SessionHook.execute(SessionHook.scala:32)
    at io.gatling.core.action.Action.$bang(Action.scala:38)
    at io.gatling.core.action.Action.$bang$(Action.scala:38)

Can someone please help explain why is this happening ?

Suraj Menon
  • 1,486
  • 3
  • 28
  • 50

2 Answers2

5

You're not using the Session API correctly. Please properly read the documentation.

Session is immutable and set returns a new instance.

exec { session =>
    val id = Instant.now.toEpochMilli.toString + scala.util.Random.nextInt(1000).toString
    session.set("STARTED_PROCESS_ID",id)
}
Stéphane LANDELLE
  • 6,076
  • 2
  • 10
  • 12
3

gatling sessions are immutable, so where you return session as the final line of your session function, you're actually returning the initial, unedited session.

session.set returns a new, updated session, so you can just leave that as the last line of the session function and it should work.

James Warr
  • 2,552
  • 2
  • 7
  • 20