How to run exec periodically with specific period.

Hellooo !!

I want to invoke specific exec periodically (For every hour or so) in my scripts. How to achieve it in Gatling ??

I’m a newbie to this Framework. Pleaseeeee, help me…

If there is no way to do so, how to schedule a function in Scala. I have tried using Timer and TimerTask Java Libraries, but I’m unable to achieve it.

Suggest me a solution for this…

Thanks,
Charishma

If you are using Linux, I believe Cron can solve it.
https://opensource.com/article/17/11/how-use-cron-linux

That’s it.

I’m using windows…

I don’t want to schedule the entire Gatling script… Just wanna repeat the specific function in the script.

My UseCase is :

I’m running a test for 4hours. But my auth token is getting expired for every 1 hour, because of that all my calls are getting failed with 401 (unauthorized).
I want to refresh my auth token.

So I’m thinking to invoke AUTH call periodically with a period of an hour and overwrite the auth_token session variable.

Need a way to do so ??

I’m novice on Gatling and Scala. And I think the code bellow extracted from AdvancedSimulationStep05.scala can help you.
I

The point is the function tryMax, if you are not receiving 200 try again. You will need the function doIf too, to do the auth.
IF the last request check failed (maybe saving the response code into a variable) then do AUTH
I’ve never done this so it is just a hipotesis,

val edit = tryMax(2) { // let’s try at max 2 times
exec(http(“Form”)
.get("/computers/new"))
.pause(1)
.exec(http(“Post”)
.post("/computerse")
.headers(headers_10)
.formParam(“name”, “Beautiful Computer”)
.formParam(“introduced”, “2012-05-30”)
.formParam(“discontinued”, “”)
.formParam(“company”, “37”).
check(status.is(session => 200 + ThreadLocalRandom.current.nextInt(2)))) // we do a check on a condition that’s been customized with a lambda. It will be evaluated every time a user executes the request
}.exitHereIfFailed // if the chain didn’t finally succeed, have the user exit the whole scenario
}

That’s a good idea. But the thing I’m facing here is :

def getActiveCharacter: ChainBuilder = {
  exec(
    http("getActiveCharacter")
    .get(cfg.getUrl)
    .check([status.in](http://status.in/) (200, 401))
    .check(headerRegex("WWW-Authenticate","^.*The token is expired.*$").exists.saveAs("AuthToken_Expiry_Flag")) // Check for 401
    .check (jsonPath ("$.characterId").exists.saveAs("ACTIVE_CHARACTERID")) // Check for 200 
  )
  .exec(session =>{
    doIf(session => (session.contains("AuthToken_Expiry_Flag"))) { // Invoking Login, if auth token is expired
      exec(Login.login)
      .exec(session => {session.remove("AuthToken_Expiry_Flag")})
    }
    session
  })
}


In the above function,
  If I got 401 response, 200 response related check is getting failed. Because of that total Api Call is failing.
  Similarly, if I got 200 response, 401 related check is failing.

Any suggestions how to implement this..

In Gatlingdocs, I have read about checkIf. But I didnt find any syntax related to this.
Im using Gatling 2.2.0. Anyone has idea, from which version this checkIf is existing ??

Can anyone provide a sample example, how to use checkif ??

I have gone through this link : [https://stackoverflow.com/questions/50867429/gatling-checkif-syntax](https://stackoverflow.com/questions/50867429/gatling-checkif-syntax).. But no luck :|

Hi Charishma,

have you tried using function “optional”:

.check(jsonPath("$.characterId").optional.saveAs("ACTIVE_CHARACTERID"))

In that case you may need a bit different assertions in the relogin part though…

Regards
Aleksei

Hi,

Yes, I have tried… That one also not working for me…

I’m trying to upgrade my Gatling version. So that I can use checkIf DSL.