Search code examples
scalagatling

How to use doSwitch properly in a scearioBuilder


My scenario builder below does not like the variable ${data} in the doSwitch. Result in error:

Expected type was: (Any, io.gatling.core.structure.ChainBuilder) .doSwitch("${data}")

What is causing the issue?

      val builder: ScenarioBuilder = scenario("Test")
        .repeat(3) {
          pace(10.seconds)
            .feed(new DataFeeder())
            .exec { session =>
              val data = session("data").as[String]
              println(s"data: $data")
              session
            }
            .doSwitch("${data}") {
              // Define cases for different data values
              case "P1" =>
                exec(myFunc(1000000))
              case "P2" =>
                exec(myFunc(2000000))
            }
        }

Solution

  • In the docs of doSwitch you have the following example of code

    doSwitch("#{myKey}")( // beware: use parentheses, not curly braces!
      "foo" -> exec(http("name1").get("/foo")),
      "bar" -> exec(http("name2").get("/bar"))
    )
    

    where the second parameter is between parentheses ( ) and not curly braces { }. The comment in the code says don't use curly braces.

    mean while you are doing this

                .doSwitch("${data}") {
                  // Define cases for different data values
                  case "P1" =>
                    exec(myFunc(1000000))
                  case "P2" =>
                    exec(myFunc(2000000))
                }
    

    you are using curly braces and then adding case followed by a value, then => (instead of ->) and finally exec(...)

    Your should be similar to

                .doSwitch("${data}") (
                  // Define cases for different data values
                  "P1" -> exec(myFunc(1000000))
                  "P2" -> exec(myFunc(2000000))
                )