Gatling Scala - usage of .doIfEquals(session => session.userId, 1) is irresponsive, execution skipped the code block

I am currently working on a script with requirement
For example :
Total number of users 5( should be like using 1 user credentials in 5 different browser sessions )

API 1 call should be executed only by User 1
API 2, 3 & 4 should be executed by all users 1-5
API 5 should be executed only by last user 5

  • No multiple scenarios allowed, all the requests should be handled under only one scenario.

Here is the code sample.

  val testUrl: String = "https://test.com"
  val username: String = "testAdmin"
  val password: String = "testadmin"
  val nbUsers: Integer = 5
  val testCount: Integer = Integer.getInteger("test-count", 3000)
  val testIterationCount: Integer = Integer.getInteger("test-iteration-count", 20)
  val userId = nbUsers.toInt

  val httpProtocol: HttpProtocolBuilder = http
    .baseUrl(testUrl + "/rest/2.0/")
    .disableCaching

  val scn: ScenarioBuilder = scenario("delete_huge_dataset")

   .doIfEquals(session => session.userId, 1) {
        exec(http("Add test main directory")
          .post("/maindirectory")
          .body(StringBody(session => s"""{ "name": "Perfmaindirectory${Random.alphanumeric.take(5).mkString}" }""")).asJson
          .check(jsonPath("$.id").saveAs("maindirectoryId")))
}

.repeat(testIterationCount.toInt){
  exec { session =>
      val testtypeids = Array("000000001", "000001", "000001101", "00000000001")
      val statusID = Array("000008", "0000058", "00000000-00019", "00000000-0009")
      val PerfIDs = session("maindirectoryId").as[String]   **// creating data inside maindirectory**
      val testData = (1 to testCount)
        .map { r =>
          Json.toJson(Map(
            "name" -> Json.toJson(s"Perftest${Random.alphanumeric.take(6).mkString}"),
            "displayName" -> Json.toJson(s"Perftest${Random.alphanumeric.take(6).mkString}"),
            "PerfIDs" -> Json.toJson(s"${PerfIDs}"),
            "testtypeids" -> Json.toJson(s"${Random.shuffle(testtypeids.toList).head}"),
            "statusId" -> Json.toJson(s"${Random.shuffle(statusID.toList).head}"),
            "excludedFromAutoHyperlinking" -> Json.toJson(true)
          ))
        }
      val datatestJson = Json.toJson(testData)
      session.set("datatestJson", datatestJson)
    }

    .exec(http("Add json data holding 3000 data set")
      .post("test/bulk")
      .body(StringBody(session => s"""${session("datatestJson").as[String]}""")).asJson
      .requestTimeout(2.hours)
      .check(jsonPath("$[*].id").findAll.saveAs("testIDs"))
      .check(status.is(201)))
    .pause(2.seconds)

    .foreach("#{testIDs}", "TestID") {
      exec(http("update 3000 data individually in loop * 100 repeat loop")
        .post("updating/testdata")
        .body(StringBody(session =>
          s"""{
             |  "id": "",
             |  "additionalTypeId": "000000001",
             |  "legs": [
             |    {
             |      "additionalTypeId2": "00000000112311",
             |      "testID": "${session("TestID").as[String]}"
             |    }
             |    ]
             |  }""".stripMargin)).asJson
        .check(jsonPath("$.id").saveAs("newtestID"))
        .check(status.is(201)))
}

   .doIfEquals(session => session.userId, 5) {
    exec(http("Delete main directory holding 300000 data set")
      .delete(session => s"domains/${session("maindirectoryId").as[String]}")
      .requestTimeout(2.hours)
      .check(status.is(204)))

}

  setUp(scn.inject(atOnceUsers(nbUsers)))
    .protocols(httpProtocol);
}

when I execute non of the script block is getting executed.
Please help me with solutions.

Note that session.userId is an internal. You should use your own feeder.
And the userId is NOT a int, so it will NEVER be equal to 1

If the requirements are really strict like that, it means you should have 3 scenarios. As you have 3 kinds of users

scenario User # actions
scn1 1 API 1, 2, 3, 4
scn2 2,3,4 API 2, 3, 4
scn3 5 API 2,3,4,5

But from what I understand of your use case, you want more about a initialization scenario andThen your actual test andThen your cleanup scenario.
Is that making sense?

Cheers!

@sbrevet yes, but all these functional should be executed in the same scenario.
do we have any other ways .( but strict to have single scenario)
so if have a feeder file

  1. Admin, admin
  2. Admin, admin
  3. Admin, admin
  4. Admin, admin
  5. Admin, admin
    Then assign the userID, 1 or 5 based on workflow, will this work?
    can you provide an example please?

@Monika,

You can use 2 different feeders. One for ID (from a simple LazyList.from) and one for your credentials. But for your usage, the csv (with proper headers) you describe is fine!

I spend my free time to provide valuable lessons and help others to understand how gatling is working. I’m not here to do your job for free.

Let’s go back from start.

usage of .doIfEquals(session => session.userId, 1) is irresponsive, execution skipped the code block

I’m pretty sure this claim is wrong.

  1. upgrade to Gatling 3.9.2
  2. if you’re still experiencing an issue after upgrading, provide a valid minimal reproducer
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.