Updating a session Variable [Or remove and set]

I have a list stored as a session variable. I want to update the list every time I make a request.
Since I didn’t find a session.update() operation. I thought of calling remove and set methods in sessions

println("Am I there ? "+session.contains(“reportNotifyList”))
val checkList = List(1,2,3,4,5)


This is not working as expected . The print statement after remove() is printing true
Am I there ? true

I can’t find too many examples about updating session variable. Can somebody throw me some light to get past this.

Thanks in advance

It’s all in the documentation: https://gatling.io/docs/gatling/reference/current/session/session_api/

Hello Stephane -

Obviously I read the document and the only relevant section i found about this is

"set(key: String, value: Any): Session: add or replace an attribute"

I tried the set method to replace my session attribute which is “reportNotifyList” in this case.

val firstList = session(“reportNotifyList”).as[String].substring(1,session(“reportNotifyList”).as[String].length-1).split(",").toList
println(“firstList -->”+firstList)
val checkList = List(1,2,3,4,5)
val secondList = session(“reportNotifyList”).as[String].substring(1,session(“reportNotifyList”).as[String].length-1).split(",").toList
println("finalList → "+secondList)

this is what the println statement printed

firstList -->List(613917, 613916, 613896, 613871, 613877, 613881, 613888, 613970, 613977, 613981)
finalList → List(613917, 613916, 613896, 613871, 613877, 613881, 613888, 613970, 613977, 613981)

Let me know what am i missing ?

Thanks in advance

Obviously I read the document

Well, still, you missed the important orange warning block.

Session instances are immutable!

Why is that so? Because Sessions are messages that are dealt with in a multi-threaded concurrent way, so immutability is the best way to deal with state without relying on synchronization and blocking.

A very common pitfall is to forget that set and setAll actually return new instances.

val session: Session = ???

// wrong usage
session.set("foo", "FOO") // wrong: the result of this set call is just discarded
session.set("bar", "BAR")

// proper usage
session.set("foo", "FOO").set("bar", "BAR")