I'm performing the following code to get the response field from executing a gatling http request and appending it to a list but in response I see the list is mostly showing the same itemId. I wonder if this is because of concurrency ? I am making this list so I can capture the resources after each create operation call and at the end of test, can iterate over this list to perform a clean up/delete of items as in using "after" hook of gatling.
In simulation class:
var responseIdList = List[String]()
val scenario1 = scenario(" TEST ")
.exec(Create())
.exec(session => {
item = session("itemId").as[String].trim
println("%%%%%%%%%%% item ID =====>>>>>>>>>> " + item)
responseIdList = item :: responseIdList
println("%%%%%%%%%%% List =====>>>>>>>>>> " + responseIdList)
session}
)
setUp(
scenario1.inject(atOnceUsers(5))
)
Gatling action:
def Create():HttpRequestBuilder= {
http("CREATE API")
.post(Host + "/items")
.header("Authorization", "Bearer "+ token)
.header("Content-Type", "application/json")
.body(StringBody(
"""{ "name" : "Item1"
|}""".stripMargin)).asJson
.check(status.is(200))
.check(jsonPath("$.itemId").saveAs("itemId"))
}
Here is the response after executing above code:
%%%%%%%%%%% item ID =====>>>>>>>>>> dc7e4024-c51e-46ce-93be-6ba1dad3679e
%%%%%%%%%%% item ID =====>>>>>>>>>> 0bf84d48-ccea-4f10-93b3-ea464adcc952
%%%%%%%%%%% item ID =====>>>>>>>>>> 072361b2-5c49-4641-a27c-c346c01dde99
%%%%%%%%%%% item ID =====>>>>>>>>>> dc7e4024-c51e-46ce-93be-6ba1dad3679e
%%%%%%%%%%% List =====>>>>>>>>>> List(0bf84d48-ccea-4f10-93b3-ea464adcc952, 072361b2-5c49-4641-a27c-c346c01dde99)
%%%%%%%%%%% List =====>>>>>>>>>> List(0bf84d48-ccea-4f10-93b3-ea464adcc952, 0bf84d48-ccea-4f10-93b3-ea464adcc952, 072361b2-5c49-4641-a27c-c346c01dde99)
%%%%%%%%%%% item ID =====>>>>>>>>>> dc7e4024-c51e-46ce-93be-6ba1dad3679e
%%%%%%%%%%% List =====>>>>>>>>>> List(0bf84d48-ccea-4f10-93b3-ea464adcc952, 0bf84d48-ccea-4f10-93b3-ea464adcc952, 0bf84d48-ccea-4f10-93b3-ea464adcc952, 0bf84d48-ccea-4f10-93b3-ea464adcc952, 072361b2-5c49-4641-a27c-c346c01dde99)
Even though on simulation of the scenario there were 5 different item Ids being created but somehow list is showing the same items. Is there a better data structure or option for me to use? Kindly advice as I'm a beginner in both gatling and scala .
Using a List and var global references is a wrong strategy because it's not threadsafe.
You should use vals and a java.util.concurrent.ConcurrentLinkedQueue
:
import java.util.concurrent.ConcurrentLinkedQueue
def Create():HttpRequestBuilder= {
http("CREATE API")
.post(Host + "/items")
.header("Authorization", "Bearer "+ token)
.header("Content-Type", "application/json")
.body(StringBody(
"""{ "name" : "Item1"
|}""".stripMargin)).asJson
.check(status.is(200))
.check(jsonPath("$.itemId").saveAs("itemId"))
}
// EDIT: global threadsafe structure stored in an immutable reference
val responseIdList = new java.util.concurrent.ConcurrentLinkedQueue[String]()
val scenario1 = scenario(" TEST ")
.exec(Create())
.exec(session => {
// EDIT: local val here instead of global var
val item = session("itemId").as[String].trim
println("%%%%%%%%%%% item ID =====>>>>>>>>>> " + item)
responseIdList.offer(item)
// EDIT: print queue content
println("%%%%%%%%%%% List =====>>>>>>>>>> " + util.Arrays.toString(responseIdList.toArray))
session}
)
setUp(
scenario1.inject(atOnceUsers(5))
)