Token is not refreshing for every 5 minutes - exec method is not executed

I would like to refresh the token for every 5 minutes, however as soon as i start running the script it prints I am inside exec method that is inside doIf method below.
not sure how it prints as soon as i run gatling test, since i do have the condition inside doIf method, I expected the statement to print (I am inside exec method) only when doIf condition is met. it only prints console.log and doesnt execute exec method after, when i search for Refresh token", in the log file, i dont find any api requests. Can you please advise how it prints I am inside exec method - as soon as the gatling test started, even before setup block starts executing.

Secondly, My understanding is that the token refresh exec method should execute when it nears 250 seconds fromNow, and when only 5 seconds left -safetyMargin ( 5 seconds less than 300) based on the condition in doIf below. but the exec method to refresh token is never executing, can you please advise what i am missing here.

val authTimeout =  300.seconds
  val safetyMargin = 5.seconds
  val executionTime = 2.hours

  val tokenTimeout: ChainBuilder = exec(session => session.set("timeout", authTimeout.fromNow))
  val printSession: ChainBuilder = exec { session => println(session)
    println(s"*************************************I am inside print session****************************")
    session
  }

 def refreshAccessToken = scenario("Access Token Generation")
    .exec(tokenTimeout)
  doIf(session => {
    println(s"*************************I am inside*****************************")
    session("timeout").as[Deadline].timeLeft <= safetyMargin
  }) {
    println(s"***********************************I am inside exec method************************************")
    exec(
      http("Refresh token")
        .post("https://www.odfo.com")
        .basicAuth("fbkfeb", "249r3849t")
        .queryParam("grant_type", "client_credentials")
        .headers(header)
        .check(status.is(200)).check(jsonPath("$.access_token").exists.saveAs("access_token"))
    )
      .exec(printSession)
  }

  private val submitOrders: ScenarioBuilder =
    scenario("order Submission")
      .exec(refreshAccessToken)
      .exec(Submission()) --> this method submits api request with 300000 records, that has to capture the refresh token from the session.


  setUp(
    Orders.inject(
      nothingFor(1),
      atOnceUsers(1),
    ).andThen(openOrders.inject(nothingFor(1),
      atOnceUsers(1)).
      andThen(submitOrders.inject(nothingFor(1),
        atOnceUsers(1),
        rampUsers(10).during(50))).
      andThen(closeOrders.inject(nothingFor(1),
        atOnceUsers(1)))))
  1. You miss a dot . before doIf (so the step is instanciated, but not part of previous scenario)
  2. Curly braces execute the code, then return their last element. You should not try to log that way.

Cheers

Thank you so much for your time, much appreciated, I added dot in front of doIf and removed log messages, and exec method is still not executed, i don’t see any api request in the log file, Can you please advise?

Meanwhile Can you please clarify if the session variables be shared between users? or users are isolated and session variables can not be shared between virtual users?
I realized. that submitOrders scenario from my previous post above has refreshAccessToken and then submission method to be invoked, I am running submitOrders scenario with 11 users, however i would like to run refreshAccessToken with only one user 1- do not want to invoke refresh token by several virtual users, moreover refresh token is just running sequentially for 11 times, then Submission scenario starts running.

however the challenge is if i separate refreshAccessToken to run by only one user, (virtual user) , can that session be shared with submitOrders user?
if not how i can run refreshAccessToken alone by one user or can you please advise how i can handle this scenario?

or do i have to add .doIf condition directly to submitOrders scenario like below rather than separating refreshToken scenario so the refresh token will execute only when the doIf condition is met? so the session variable stays with in the virtual user session. I tried running the below code, and for some reason exec code to refresh token is never executed even after 10 mins are elapsed. Can you please advise?

Modified Code:

 val authTimeout =  300.seconds
  val safetyMargin = 5.seconds
  val executionTime = 2.hours

  val tokenTimeout: ChainBuilder = exec(session => session.set("timeout", authTimeout.fromNow))
  val printSession: ChainBuilder = exec { session => println(session)
    session
  }

  val header = Map(
    "Accept" -> """application/json""",
    "Content-Type" -> """application/x-www-form-urlencoded""")


   private val submitOrders: ScenarioBuilder =
    scenario("Bids Submission for Round 1")
      .exec(session => session.setAll(Map("access_token" -> Token, "AuctionId" -> AuctionId, "AuctionUri" -> AuctionUri, "CreateAuctionYear" -> CreateAuctionYear)))
      .exec(tokenTimeout)
    .doIf(session => {
       session("timeout").as[Deadline].timeLeft <= safetyMargin
    }) {
    exec(
      http("Refresh token")
        .post("https://www.oo.com")
        .basicAuth("24848", "sbnsdvbfs")
        .queryParam("grant_type", "client_credentials")
        .headers(header)
        .check(status.is(200)).check(jsonPath("$.access_token").exists.saveAs("access_token"))
     )
      .exec(printSession)
    }
      .exec(Submission) --> this method submits api request with 300000 records, that has to capture the refresh token from the session.

setUp(
    Orders.inject(
      nothingFor(1),
      atOnceUsers(1),
    ).andThen(openOrders.inject(nothingFor(1),
      atOnceUsers(1)).
      andThen(submitOrders.inject(nothingFor(1),
        atOnceUsers(1),
        rampUsers(10).during(50))).
      andThen(closeOrders.inject(nothingFor(1),
        atOnceUsers(1)))))

Also , i noticed when i print out the timeout session variable, it shows timeout → Deadline(376887875917982 nanoseconds), when i convert that to 6281.4 minute, not sure which is why the if condition is never satisfied,

is authTimeout.fromNow now calculated wrongly here? i assumed it is just going to capture the current time and add -300 seconds to it, (5 minutes) , but when i print the session timeout value it shows 376887875917982 nanoseconds, can you please advise?

 val authTimeout =  300.seconds
  val tokenTimeout: ChainBuilder = exec(session => session.set("timeout", authTimeout.fromNow))

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