exec with function argument

I need to do some calculation in a scenario prior to sending an HTTP request, so I am trying to use a function argument. Something like this:

exec(session => {

val cmdIds =

session.getAttribute(“cmdIds”).asInstanceOf[ArrayBuffer[String]]

val locUris =

session.getAttribute(“locUris”).asInstanceOf[ArrayBuffer[String]]

val msgId =

session.getTypedAttributeInt * 2 + 1

val confirmations = replaceConfirmations(msgId, cmdIds, locUris)

http(“replace acknowledgement”)

.post(localURI)

.headers(headers)

.fileBody(“Replace-ack”,

Map(“vid” → “${vid}”,

“target” → (target + “:80” + localURI),

“sessionid” → “${sessionid}”,

“loopctr” → “${loopctr}”,

“cmdIds” → “${cmdIds}”,

“locUris” → “${locUris}”,

“confirmations” → confirmations)).asXML

.check(status.is(200))

session

})

The problem is that the request does not get sent if I do the http call in the function. What can I do to make sure that it does get sent?

You have to store the result of your calculation into the session in the execf, then use this stored value when sending the request.
2 steps

I'm not sure whether I understand you correctly. Do you mean that I
cannot put any local variables into the map I pass to the fileBody
call? I modified my code so that this is the case, but the request
still does not get sent:

        exec(session => {

            val cmdIds =

              session.getAttribute("cmdIds").asInstanceOf[ArrayBuffer[String]]

            val locUris =

              session.getAttribute("locUris").asInstanceOf[ArrayBuffer[String]]

            val msgId =

              session.getTypedAttribute[Int]("loopctr") * 2 + 1

            val confirmations = replaceConfirmations(msgId, cmdIds, locUris)

            session.setAttribute("confirmations", confirmations)

            session.setAttribute("target", target + ":80" + localURI)

            http("replace acknowledgement")

              .post(localURI)

              .headers(headers)

              .fileBody("Replace-ack",

                Map("vid" -> "${vid}",

                  "target" -> "${target}",

                  "sessionid" -> "${sessionid}",

                  "loopctr" -> "${loopctr}",

                  "confirmations" -> "${confirmations}")).asXML

              .check(status.is(200))

            session

          })

exec(session => session) is only for editing the session, or maybe side-effecting, like logging the session content.

The request DSL is only definitions, it doesn’t execute requests by itself.
What you’ve done is basically building a request definition and then discard it.

when I rearrange like this

          exec(session => {

            val cmdIds =

              session.getTypedAttribute[ArrayBuffer[String]]("cmdIds")

            val locUris =

              session.getTypedAttribute[ArrayBuffer[String]]("locUris")

            val msgId =

              session.getTypedAttribute[Int]("loopctr") * 2 + 1

            val confirmations = replaceConfirmations(msgId, cmdIds, locUris)

            session.setAttribute("confirmations", confirmations)

            session.setAttribute("target", target + ":80" + localURI)

          })

          .exec(http("replace acknowledgement")

              .post(localURI)

              .headers(headers)

              .fileBody("Replace-ack",

                Map("vid" -> "${vid}",

                  "target" -> "${target}",

                  "sessionid" -> "${sessionid}",

                  "loopctr" -> "${loopctr}",

                  "confirmations" -> "${confirmations}")).asXML

              .check(status.is(200)))

requests get sent, but there is a problem with the confirmations
session attribute:

18:04:35.410 [WARN ] c.e.e.g.c.s.ELParser$ - Couldn't resolve EL
${confirmations}

https://github.com/excilys/gatling/wiki/Session#immutability

https://github.com/excilys/gatling/wiki/Session#functions

future doc is even more explicit:
https://github.com/excilys/gatling/blob/master/src/sphinx/session/session_api.rst#setting-attributes

OK, I understand, I should have done

           session.setAttribute("confirmations",
replaceConfirmations(msgId, cmdIds, locUris))

            .setAttribute("target", target + ":80" + localURI)

iso

           session.setAttribute("confirmations",
replaceConfirmations(msgId, cmdIds, locUris))

            session.setAttribute("target", target + ":80" + localURI)

That seems to work.

Thx,

Yo

Exactly :slight_smile: