diff --git a/src/adapter/outbound/persistence-jpa/src/postgresqlIntegrationTest/java/dev/caskeleton/adapter/outbound/persistence/liveevent/LiveEventLogContractTest.java b/src/adapter/outbound/persistence-jpa/src/postgresqlIntegrationTest/java/dev/caskeleton/adapter/outbound/persistence/liveevent/LiveEventLogContractTest.java index d847e9f4..bbe5a45b 100644 --- a/src/adapter/outbound/persistence-jpa/src/postgresqlIntegrationTest/java/dev/caskeleton/adapter/outbound/persistence/liveevent/LiveEventLogContractTest.java +++ b/src/adapter/outbound/persistence-jpa/src/postgresqlIntegrationTest/java/dev/caskeleton/adapter/outbound/persistence/liveevent/LiveEventLogContractTest.java @@ -17,6 +17,8 @@ import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.TreeMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -87,23 +89,28 @@ class LiveEventLogContractTest { } @Test - @DisplayName("two concurrent writers receive different positions and both commit") + @DisplayName("두 writer가 동시에 쓰면 서로 다른 위치 1·2를 받고, replay는 받은 위치 순서대로 둘 다 돌려준다") void concurrentWritersAllocateDistinctPositions() throws Exception { + // given CountDownLatch start = new CountDownLatch(1); + // Which writer reaches the allocator first is up to the scheduler, so the expected replay + // order is the order of the positions the writers got back, not the order they were started. + Map payloadByPosition = new TreeMap<>(); + // when try (ExecutorService executor = Executors.newFixedThreadPool(2)) { Future first = executor.submit(() -> allocateAndAppendAfter(start, "orders", "from-first")); Future second = executor.submit(() -> allocateAndAppendAfter(start, "orders", "from-second")); - start.countDown(); - - assertThat(List.of(first.get(10, TimeUnit.SECONDS), second.get(10, TimeUnit.SECONDS))) - .containsExactlyInAnyOrder(1L, 2L); + payloadByPosition.put(first.get(10, TimeUnit.SECONDS), "from-first"); + payloadByPosition.put(second.get(10, TimeUnit.SECONDS), "from-second"); } - assertThat(replayAfter("orders", 0, NOW)).containsExactly("from-first", "from-second"); + // then + assertThat(payloadByPosition).containsOnlyKeys(1L, 2L); + assertThat(replayAfter("orders", 0, NOW)).containsExactlyElementsOf(payloadByPosition.values()); } @Test