Gatling Repeat Loop not working for my post request attempt?

see the following scenario. When I run this scenario I get a successful “POST createRuleSet” from the createRulesetUid method.

val createRulesetScenario: ScenarioBuilder = scenario(“Create Ruleset scenario”)
.feed(TestUtil.configValues)
.exec(TestUtil.getAccessToken)
.exec(TestUtil.createRulesetUid(rulesetFilePath,0))
.exec(TestUtil.createRulesetUid(rulesetFilePath,1))

I get the expected response 2 successful requests to POST createRuleSet

This cannot work, please read the documentation: https://gatling.io/docs/gatling/reference/current/general/scenario/#exec

You have to modify your TestUtil.createRulesetUid so it doesn’t take an Int parameter but an Expression[Int].

Thanks Stephane for the quick response.

Sorry I am a bit new to Gatling and I am not sure why this will not work? I tried your suggestion and I added the import io.gatling.core.session.Expression and updated the parameter to ruleSetNumber:Expression[Int] but still received the same result.

This is the only update I made

def createRulesetUid(ruleset:String, ruleSetNumber:Expression[Int])

Is there something else I am missing?

Thanks!Tom

Extract from the documentation I linked:

Gatling DSL components are immutable ActionBuilder(s) that have to be chained altogether and are only built once on startup. The result is a workflow chain of Action(s). These builders don’t do anything by themselves, they don’t trigger any side effect, they are just definitions. As a result, creating such DSL components at runtime in functions is completely meaningless. If you want conditional paths in your execution flow, use the proper DSL components (doIf, randomSwitch, etc)

exec { session =>
  if (someSessionBasedCondition(session)) {
    // just create a builder that is immediately discarded, hence doesn't do anything
    // you should be using a doIf here
    http("Get Homepage").get("[http://github.com/gatling/gatling](http://github.com/gatling/gatling)")
  }
  session
}

Thanks Stephane

Even when I remove the if statement it does not seem to work.

See

def createRulesetUid(ruleset:String, ruleSetNumber:Expression[Int]) =
feed(TestUtil.configValues)
.exec(getAccessToken)
.exec(_.set(“auth_token”, token))
.exec(
http(“POST createRuleSet - " + ruleset.dropRight(5))
.post(session => getCreateRulesetUrl(session,ruleset))
.header(“Authorization”, “Bearer ${auth_token}”)
.header(“GW-Tenant”, “${gw_tenant}”)
.header(“GW-User”, “${gw_user}”)
.header(“Content-Type”, “application/json”)
.header(“GW-request-grn”, “${gw_grn}”)
.headers(TestUtil.serviceCallTracingHeaders)
.body(StringBody(getResourceBody(Integer.parseInt(ruleSetNumber.toString()))))
.check(status.is(200)) // check successful update
.check(jsonPath(”$.resourceUid").exists.saveAs(“resourceUid”))).exitHereIfFailed
.exec {
session => {
rulesetUidArray.add(session(“resourceUid”).as[String])
Console.println("-------###########################-------ruleSetNumber: " + rulesetUidArray.toString);
session
}
}

When I call it from my scenario in the Repeat it does not seem to enter the method

val createRulesetScenario: ScenarioBuilder = scenario(“Create Ruleset scenario”)
.feed(TestUtil.configValues)
.exec(TestUtil.getAccessToken)
// .exec(TestUtil.createRulesetUid(rulesetFilePath,0))
// .exec(TestUtil.createRulesetUid(rulesetFilePath,1))
.repeat(numberOfRulesets,“indexCount”) {
exec(session => {
exec(TestUtil.createRulesetUid(rulesetFilePath, Integer.parseInt(session.attributes(“indexCount”).toString))).pause(5)
session
})
}

there must be something else I am missing about this repeat?

All the documentation I’ve provided you clearly states that calling Gatling DSL methods inside a function is fruitless.

exec(session => {
exec(???)
session
})

does not and will never work.

Ok gotcha Thanks!

So I guess what I am trying to do is something like this…

I have a list “jurisdictionCode”: [

“Hokkaido”,

“Aomori”,

“Iwate”,

“Miyagi”

]

I would like to loop through these values and use them for my post request … is there a way to pass in each jurisdiction as a string or something to have in my post? Something like this?:

.foreach("${jurisdictionCode}", “jurisdiction”) {

exec(TestUtil.createRulesetUid2(rulesetFilePath, “${jurisdiction}”))

}

butt I cant seem to get it to work using this post. Note: I want to save the response in the session see .check(jsonPath("$.resourceUid").exists.saveAs(“resourceUid”))).

def createRulesetUid2(ruleset:String, jurisdictionParam:Expression[String]) = // not sure if this is correct or how i would access this value? it is unclear how Expression[String] works?
feed(TestUtil.configValues)
.exec(getAccessToken)
.exec(_.set(“auth_token”, token))
.exec(
http(“POST createRuleSet - " + ruleset.dropRight(5))
.post(session => getCreateRulesetUrl(session,ruleset))
.header(“Authorization”, “Bearer ${auth_token}”)
.header(“GW-Tenant”, “${gw_tenant}”)
.header(“GW-User”, “${gw_user}”)
.header(“Content-Type”, “application/json”)
.header(“GW-request-grn”, “${gw_grn}”)
.headers(TestUtil.serviceCallTracingHeaders)
.body(StringBody(getResourceBodyJurisdiction(”${jurisdiction}"))) //This doesnt work… how do I get the jurisdiction value in for the body??
.check(status.is(200))
.check(jsonPath("$.resourceUid").exists.saveAs(“resourceUid”))).exitHereIfFailed // note I want to save resourceUid for use later.
.exec {
session => {
rulesetUidArray.add(session(“resourceUid”).as[String]) //here I was adding each resourceUid into a array to save
session
}
}

suggestions would be very much appreciated !

ok looks like I got this to work by removing the exec() in the forEach

.foreach("${jurisdictionCode}", “jurisdiction”) {

TestUtil.createRulesetUid2(rulesetFilePath, “${jurisdiction}”)

}

thanks!