Reputation: 271
I am trying to write some test of my application. I have small parking service and want to do integration test for car entry to the parking and check if any race condition is possible. I checked it by jmeter and app is ok, everything works, race condition is absent but I have to write "classic" test for this. I prepared something like this:
@Test
@Sql(scripts = {"/parking-data.sql", "/cars-data.sql"})
void testForRaceConditionWhen2CarsWantToParkAtTheSameTime() throws Exception {
//given
CarAtParkingGateRequest carAtGateRequestMode = new CarAtParkingGateRequest("000000000000000000000000000000000001",
"000000000000000000000000000000000003");
//when
for (int i = 0; i < 10; i++) {
Thread x = new Thread(() -> {
try {
MvcResult result = mvc.perform(post("/parking/exit")
.contentType("application/json")
.content(objectMapper.writeValueAsString(carAtGateRequestMode)))
.andDo(print())
.andReturn();
} catch (Exception e) {
e.printStackTrace();
}
});
x.start();
}
//then
}
It is of course passes but, how can I for example get response status from every thread.
I was thinking about List<Integer> expectedStatus = List.of(200,400,400...)
and in other list put that statuses from every thread and next comapre them.
Upvotes: 0
Views: 34
Reputation: 8686
You can approach with CompletableFuture
like:
{
CompletableFuture<MvcResult>[] results = new CompletableFuture[10];
for(int i = 0; i < 10; i++){
results[i] = CompletableFuture.supplyAsync(() ->
mvc.perform(post("/parking/exit")
.contentType("application/json")
.content(objectMapper.writeValueAsString(carAtGateRequestMode)))
.andDo(print())
.andReturn()
);
}
CompletableFuture.allOf(results);
for(int i = 0; i < 10; i++){
MvcResult result = results[i].get();
// test
}
}
Upvotes: 1