Using session attribute in scala function that returns chain to be executed

622 views
Skip to first unread message

Michelle Burrows

unread,
Jun 10, 2014, 1:27:33 PM6/10/14
to gat...@googlegroups.com
I want to use a session attribute to determine what chain to execute (via a Map). None of these is valid:

  exec(MyMaps.chains("${bar}") //MyMaps.chains is a Map(String, chain)
  exec(MyMaps.chains(session("bar").as[String])
  exec(session => {
    MyMaps.chains(session("bar").as[String]) //Doesn't execute
    session
  })

I also don't want to use doIf, as I would either have to use a lot of them, or really long inner blocks with lots of copy/pasting involved, and a lot of hard-coded values that would make it unmaintanable:

  doIf("${bar}", "foo") {
    exec(FooRequest.req) //The map wouldn't help here, anyway
  }
  .doIf("${bar}", "moo") {
    exec(MooRequest.req)
  }

If there are 10 requests and I add another value for bar or change moo to boo, it would take forever to update all my scenarios.

Is there any other way I could accomplish this? I also tried using a var bar before declaring the scenario (variable would do just as well as a session attribute), but that a) is shared between users (not the same for all), and b) can't be changed from within the scenario. I don't think there's any way to get a variable with the right scope. (I'm on 2.0.0-snapshot, if it makes a difference.)

For completeness/clarity, Appendix A: the map

  object MyMaps {
    val chains = Map(
      "foo" -> FooRequest.req,
      "moo" -> MooRequest.req
    )
  }

Stéphane Landelle

unread,
Jun 10, 2014, 1:48:03 PM6/10/14
to gat...@googlegroups.com


--
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.
For more options, visit https://groups.google.com/d/optout.

Michelle Burrows

unread,
Jun 10, 2014, 2:19:44 PM6/10/14
to gat...@googlegroups.com
Still has the maintainability issues of doIf, but at least it's a lot cleaner. Can't seem to get it to work, however (I assume by upcoming version you mean upcoming release, but it's already in the snapshot, right?)

Tried this:

  doSwitch("${bar}") {
    "foo" -> MyMaps.chains("foo"),
    "moo" -> MyMaps.chains("moo")
  }

And am getting "';' expected but ',' found" at the end of the "foo" line.

(It may be worth noting that I can confirm the following works fine):

  exec(MyMaps.chains("foo"))

Michelle Burrows

unread,
Jun 10, 2014, 3:03:48 PM6/10/14
to gat...@googlegroups.com
Figured it out: docs show braces around the cases, but they should be parens. Possibly same issue with other structure elements.

Stéphane Landelle

unread,
Jun 10, 2014, 3:05:41 PM6/10/14
to gat...@googlegroups.com
That was an error in the documentation.
Just like randomSwitch, you can't use curly braces.
In Scala, you may use curly braces instead of parentheses only if the parameter list block is of size 1.

doSwitch("${bar}") ( // parentheses
    "foo" -> MyMaps.chains("foo"),
    "moo" -> MyMaps.chains("moo")
  )

Stéphane Landelle

unread,
Jun 10, 2014, 3:06:15 PM6/10/14
to gat...@googlegroups.com
We shot at the same time.
Documentation is fixed. :)
Reply all
Reply to author
Forward
0 new messages