Updating a session Variable [Or remove and set]

86 views
Skip to first unread message

haripr...@gmail.com

unread,
Sep 8, 2021, 5:12:13 PM9/8/21
to Gatling User Group
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

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

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

Stéphane LANDELLE

unread,
Sep 8, 2021, 5:13:30 PM9/8/21
to gat...@googlegroups.com

--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/dbc71d47-d63b-4eb2-9320-5dcf8e55e25an%40googlegroups.com.

haripr...@gmail.com

unread,
Sep 8, 2021, 6:16:30 PM9/8/21
to Gatling User Group
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)
session.set("reportNotifyList",checkList)
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

Stéphane LANDELLE

unread,
Sep 8, 2021, 6:20:34 PM9/8/21
to gat...@googlegroups.com
> 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")

--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 

Reply all
Reply to author
Forward
0 new messages