How does spring singleton beans serve concurrent requests (#11987)

* add service base structure
- controllers & services & sanity check tests

* finalize code

* reformat continues lines with 2 spaces

* reformat continues lines with 2 spaces
This commit is contained in:
Vlad Fernoaga
2022-04-09 06:29:33 +03:00
committed by GitHub
parent 4bcb36ff41
commit bb7d2a5d55
6 changed files with 165 additions and 0 deletions
@@ -0,0 +1,53 @@
package com.baeldung.concurrentrequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;
@SpringBootTest
@AutoConfigureMockMvc
public class ConcurrentRequestUnitTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ProductController controller;
@Test
public void givenContextLoads_thenProductControllerIsAvailable() {
assertThat(controller).isNotNull();
}
@Test
public void givenMultipleCallsRunInParallel_thenAllCallsReturn200() throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(() -> performCall("/product/1", status().isOk()));
executor.submit(() -> performCall("/product/2/stock", status().isOk()));
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
}
private void performCall(String url, ResultMatcher expect) {
try {
this.mockMvc.perform(get(url))
.andExpect(expect);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}