1

I am trying to write some load tests that make an auth call and then use that same token for each call in the scenario.

I am having trouble with getting it to check if the token has been set, if not then call to get the token. Otherwise is the token is set, then execute the calls.

My code that does this check looks like this:

val scn = scenario("Get All Events")

    .doIf(session => session("bearerToken").asOption[String].isEmpty) {
      exec(getAuth.getBearerToken)
    }
    .pause(2)
    .exec (http("Get Events")
      .get("v1/events")
      .header("x-request-id", "perf-test")
      .header("HttpLogRequestId", s"${BaseHelpers.getUUID}")
      .header("x-api-key", s"${BaseHelpers.getXAPIKey}")
      .header("Authorization", "Bearer ${bearerToken}")
      .check(status.is(200))
  )

When it calls to set the auth token it looks like this:

val getBearerToken = exec(http("Get Authorization Token")
    .post(baseAuthURL + "v1/oauth2/token?grant_type=password&client_id=" + client_id + "&username=" + username + "&password=" + password)
    .basicAuth(client_id, client_secret)
    .header("x-request-id", "perf-test")
    .header("HttpLogRequestId", s"${BaseHelpers.getUUID}")
    .check(status.is(200))
    .check(jsonPath("$.id_token").saveAs("bearerToken"))
  )

The result for me is generally one of two things. Either it will make this call each time, somehow ignoring the check or it will error out telling me the key is not found.

I have tried doing this a few different ways and still no luck. Hoping a second set of eyes might point out where I am going wrong.

Trent
  • 77
  • 1
  • 2
  • 8

1 Answers1

1

sessions are private to each user - so I would expect that your scenario as written would always execute your doIf block.

The gatling EL also has support for your use case (not that it would change the result you're seeing). You could write...

doIf("${bearerToken.exists()}") {...}

But the bigger question is, what do you want your simulation to accomplish? As you've described it, you want one user to login and then simultaneously make calls to "v1/events". As you've written it, you want multiple users to each login then make that call.

James Warr
  • 2,552
  • 2
  • 7
  • 20
  • Thanks for that quick answer James. It makes sense that is how its working now that you say that. I do have a couple of ways I need to approach this. One if a single auth "user" to hit the end point over and over and then there is the more proper scenario of many users hitting the endpoint. Maybe you can give me some direction on how I would write these to accomplish that? – Trent Apr 17 '19 at 12:42
  • to have a user login then hit the endpoint over and over, you could just use one of gatling's loop constructs like `repea`' or `doWhile`. This would not have the same user calling the endpoint concurrently though. If you want concurrency for the same user (which is an odd scenario) you could do something like what's described in [link](https://stackoverflow.com/questions/47857825/gatling-io-share-data-between-virtual-users) – James Warr Apr 17 '19 at 22:52
  • for the last scenario (the most likely one) where there are multiple users logging in and hitting the endpoint, you can have a scenario where they login then make the api call (you wouldn't need the doIf here). Then have a feeder with all your users – James Warr Apr 17 '19 at 23:01
  • Perfect. This sounds like it gives me the basics I need to map this out. Greatly appreciated as I know this is something thats not quite standard. – Trent Apr 18 '19 at 18:30