test(persistence-jpa): 동시 writer 시험이 replay 순서를 받은 위치 순서로 본다 #39

Merged
donghyeon.kang merged 2 commits from claude/r9-live-event-order into main 2026-11-13 11:43:53 +00:00
@@ -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<Long, String> payloadByPosition = new TreeMap<>();
// when
try (ExecutorService executor = Executors.newFixedThreadPool(2)) {
Future<Long> first =
executor.submit(() -> allocateAndAppendAfter(start, "orders", "from-first"));
Future<Long> 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