@Charles_Ray:
Spróbowałem wedle tego co zasugerowałeś, test wygląda tak:
@SneakyThrows
@Test
void shouldNotBeAbleToHandleConcurrentConnections() {
//given
final ClientConfiguration clientConfiguration =
new ClientConfiguration(wireMockPort, maxConcurrentConnections);
final Client client =
clientConfiguration.getClient(clientConfiguration .httpClient());
int connections = maxConcurrentConnections + 1;
int workerThreads = connections - 1;
stubPost("/", getWorkerRequest().username(), getResponseBody(), HttpStatus.OK, 1500);
stubPost("/", getMainThreadRequest().username(), getResponseBody(), HttpStatus.OK);
ExecutorService executorService = Executors.newFixedThreadPool(workerThreads);
CountDownLatch latch = new CountDownLatch(connections);
Waiter waiter = new Waiter();
//when
for (int i = 0; i < workerThreads; i++) {
executorService.execute(new Worker(latch, client, waiter));
}
latch.countDown();
//sleep for a while to make sure worker threads acquire connections from pool
Thread.sleep(300);
//then
ClientException exception =
assertThrows(ClientException.class, () -> client.getUser(getMainThreadRequest()));
assertThat(exception.getMessage())
.isEqualTo("Unable to execute HTTP request: Timeout waiting for connection from pool");
executorService.shutdown();
}
Zrobiłem delay 1500ms na wiremocku przy requestach z worker threads, potem żeby mieć pewność że one najpierw wykonają request to w głównym wątku robie Thread.sleep()... co pewnie nie jest najlepszym rozwiązaniem. Tylko na wiremocku chyba nie ma żadnej semafory, albo przynajmniej nie znam czegoś takiego?
Z tym rozwiązaniem mam taki rezultat, że lokalny gradle run przechodzi, wszystkie testy green, ale na jobach w repozytorium fail...