Hi All,
I have a requirement where I need to pass auth code from one of the service to a set of other services which will be getting executed in parallel. I have done execution previously where there was no auth code involved and I was able to execute those set of services in parallel, but now with addition of auth code, I am not sure how to implement it as auth code will be executed once and then rest all the services will get executed as per the set up injection details.
Auth code:
val auth = scenario(“getauth”).exec(http(“GetAuthToken”) .post("/oauth/token”) .body(StringBody(""" { "client_id" : "abc123", "client_secret" : "xyz123" , "grant_type" : “client_credentials”, """)).asJSON.check(status is 200).check(jsonPath(“$.access_token”).saveAs(“authToken”)
Scenarios to execute in parallel
`
object payload_1{
val pl1 = scenario(“pl1test”).feed(data).exec(http(“claimsum”)
.post("/abc/test1.asmx")
.headers(header)
.body(ElFileBody(“req1.txt”)))
val pl2 = scenario(“p21test”).feed(data).exec(http(“eligsum”)
.post("/abc/test2.asmx")
.headers(header)
.body(ElFileBody(“req2.txt”)))
}
val IVR_1 = scenario(“claimsummary”).exec(payload_1.pl1)
val IVR_2 = scenario(“eligsummary”).exec(payload_1.pl2)
val Auth_tkn = scenario (“generateauth”).exec(auth)
#following is the setup i was using to execute two requests in parallel before, not sure how to incorporate auth scenario here
setUp(IVR_1.inject(rampUsers(100) during (100)).protocols(httpProtocol),
IVR_2.inject(rampUsers(100) during (100)).protocols(httpProtocol))
`
Here you can pass auth token in two ways.
- Execute auth tkn request and pass the variable to remaining scenarios.
- use polling strategy for sharing data between two or more scenarios.
e.g.
val authToken = new ConcurrentLinkedQueue[String] //declare variable
authToken.add(session(“authToken”).as[String])//add authtoken
authToken_share = authToken.poll() //get the authtoken
Hi Mohan,
May be I didn’t explain the issue well. I am trying to figure out how to capture a value and pass it on to the other requests which will be executed in parallel. So basically, those second set of requests which will be accepting the captured parameter (auth token) will be getting executed in parallel but the before all that API for auth token need to be executed once. That’s where I am stuck where I can’t figure out a way to combine sequential with concurrent requests. Hope, this explains issue better!
Thanks,
Vikram
Hi Vikram,
here is the scenario you need to setup for achieving the desired results.
setUp(Auth_tkn.inject(rampUsers(1) during (1)),
IVR_1.inject(nothingFor(10 seconds),rampUsers(100) during (100)),
IVR_2.inject(nothingFor(10 seconds),rampUsers(100) during (100))).protocols(httpProtocol)
Steps 1: Once Auth_tkn is executed, save the variable in ConcurrentLinkedQueue.
Step 2: in IVR_1, IVR_2 add custome code to pull the saved variable data in step 1 and pass it to requests in IVR_1 & IVR_2.
Step 3: make sure IVR_1 & IVR_2 scenarios are executed after Auth_tkn scenario ( just add nothingFor based on response timeof Auth_tkn scenario).
Step 4: If you are worked in Loadrunner, VTS (Vitual Table server) where you can write dynamic data during execution of tests for sharing data between virtual users. In the same way in Gatling you need to implemented polling such that use data can be shared across virtual users.
e.g.
val authToken = new ConcurrentLinkedQueue[String] //declare variable
authToken.add(session(“authToken”).as[String])//add authtoken
authToken_share = authToken.poll() //get the authtoken
Hey, not sure if it’s helpful, but what I’m doing is using another Http Library to get my Token and store if before starting my test executing, something like that:
`
def init() : String = {
val response = Http(“AuthTokenURL”)
.postData(“grant_type=client_credentials&client_id=…&client_secret=…”)
.header(“Content-Type”, “application/x-www-form-urlencoded”)
.asString;
decodeToken match {
case Left(error) => println(“Invalid JSON :(”)
case Right(token) => return token.access_token
}
return response.body
}
val authToken = init();
val sessionHeaders = Map(“Authorization” → s"Bearer $authToken",
“Content-Type” → “application/json”)
`
So after that I can use this sessionHeaders in the Gatling scenarios:
`
object GetAccount
{
val getAccount = scenario(“GetAccount”)
.exec(
http(“getaccounts”)
.get("/v1/accounts")
.headers(sessionHeaders)
.check(status.is(200))
)
}
`
Of course the AuthToken code is not being tested, but it’s being executed just once and storing the token for the Gatling Scenarios.