Trouble Updating Session In If block

41 views
Skip to first unread message

Jason Finch

unread,
Jun 21, 2017, 9:19:18 AM6/21/17
to Gatling User Group
So, I want to update a session variable if this scala function returns true.  I set it to always return true to debug this
part of the code.  However, when I run and I print out the session variables at the end of the run the session variable "testNewSeg" is
never updated even though i know that it reaches the code inside the 'if' block. If anyone knows the solution to this problem
please let me know. Thanks! 


.exec{session =>
 
if (newSegmentTest(session,"activity_server_id","isNewSeg"))
   
{exec(session => session
           
.set("testNewSeg","true")
   
)}
  session
}



Barry Perez

unread,
Jun 21, 2017, 9:25:09 AM6/21/17
to Gatling User Group
Hi Jason,

Ref the session API documentation: http://gatling.io/docs/current/session/session_api/

---

Warning


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.


---

So, remove the last "session" and you should be good to go.

Cheers,
Barry

Barry Perez

unread,
Jun 21, 2017, 9:33:31 AM6/21/17
to Gatling User Group
Example:


.exec{session =>
  if (newSegmentTest(session,"activity_server_id","isNewSeg")) {
    session.set("testNewSeg","true")
  } else {
  session

Jason Finch

unread,
Jun 21, 2017, 9:37:13 AM6/21/17
to Gatling User Group
When I remove the last session I get a compile errors:

"Missing parameter type {exec(session => session "

"type mismatch;
 found   : Unit
 required: io.gatling.commons.validation.Validation[io.gatling.core.session.Session]
      .exec{session => if(newSegmentTest(session,"activity_server_id","isNewSeg"))"

.exec{session => if(newSegmentTest(session,"activity_server_id","isNewSeg"))

Barry Perez

unread,
Jun 21, 2017, 9:39:39 AM6/21/17
to Gatling User Group
Hi,

Try following the example I posted a few minutes ago - you have to return a session regardless of whether you modify it or not - but since session.set returns an immutable session object then you return either that OR the original session object (use if/else)

Barry

Jason Finch

unread,
Jun 21, 2017, 9:55:34 AM6/21/17
to Gatling User Group
Hey Barry, 

Thank you so much! New to Gatling and Scala. This should open up a lot for me.  

Barry Perez

unread,
Jun 21, 2017, 11:12:35 AM6/21/17
to Gatling User Group
Hi Jason,

No problems - glad it was helpful... Enjoy!

Barry
Reply all
Reply to author
Forward
0 new messages