|
4 | 4 | import static org.assertj.core.api.Assertions.assertThat;
|
5 | 5 | import static org.awaitility.Awaitility.await;
|
6 | 6 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
| 7 | +import static org.junit.jupiter.api.Assertions.fail; |
7 | 8 |
|
8 | 9 | import java.net.URL;
|
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.CompletableFuture; |
| 12 | +import java.util.concurrent.ExecutorService; |
| 13 | +import java.util.concurrent.Executors; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import java.util.stream.Stream; |
| 16 | + |
9 | 17 | import org.junit.jupiter.api.BeforeEach;
|
10 | 18 | import org.junit.jupiter.api.Test;
|
11 | 19 | import org.springframework.beans.factory.annotation.Autowired;
|
@@ -85,4 +93,37 @@ void testCheckOrdered() {
|
85 | 93 | assertThat(externalQueueService.getSent())
|
86 | 94 | .containsExactly(joe, dave, neil, tupac, jeff));
|
87 | 95 | }
|
| 96 | + |
| 97 | + @Test |
| 98 | + void testCheckOrderedConcurrent() { |
| 99 | + ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); |
| 100 | + |
| 101 | + try { |
| 102 | + var joe = new Customer(1L, "Joe", "Strummer"); |
| 103 | + var dave = new Customer(2L, "Dave", "Grohl"); |
| 104 | + var neil = new Customer(3L, "Neil", "Diamond"); |
| 105 | + var tupac = new Customer(4L, "Tupac", "Shakur"); |
| 106 | + var jeff = new Customer(5L, "Jeff", "Mills"); |
| 107 | + |
| 108 | + List<CompletableFuture<Void>> tasks = Stream.of(joe, dave, neil, tupac, jeff) |
| 109 | + .map(customer -> CompletableFuture.runAsync(() -> { |
| 110 | + var url = base.toString() + "/customer?ordered=true"; |
| 111 | + assertTrue(template.postForEntity(url, customer, Void.class).getStatusCode().is2xxSuccessful()); |
| 112 | + }, executorService).exceptionally((throwable) -> fail("Can't create ordered task for customer " + customer, throwable))) |
| 113 | + .collect(Collectors.toUnmodifiableList()); |
| 114 | + |
| 115 | + CompletableFuture.allOf(tasks.toArray(CompletableFuture[]::new)).join(); |
| 116 | + |
| 117 | + await() |
| 118 | + .atMost(10, SECONDS) |
| 119 | + .pollDelay(1, SECONDS) |
| 120 | + .untilAsserted( |
| 121 | + () -> |
| 122 | + assertThat(externalQueueService.getSent()) |
| 123 | + .containsExactlyInAnyOrder(joe, dave, neil, tupac, jeff)); |
| 124 | + |
| 125 | + } finally { |
| 126 | + executorService.shutdownNow(); |
| 127 | + } |
| 128 | + } |
88 | 129 | }
|
0 commit comments