test(migration): 예외 목록 검사를 fixture로 지켜 빈 목록에서도 돌게 한다

문제 (재리뷰 round8 RR8-03-04)
- GrpcOperationLedgerMigrationLockWaitIT의 예외 목록 검사
  (everyMigrationListedAsRunningOutsideATransactionIsOneConcurrentIndex
  BuildThatSaysSo)는 RUN_OUTSIDE_A_TRANSACTION 위에서 돈다. 목록이 비어
  루프가 한 번도 돌지 않고, 단언은 늘 참이다. 거부 조건 셋을 모두 무력하게
  바꿔도 통과한다(리뷰 변이 A11). 가이드는 이 테스트가 목록을 지킨다고
  적는다.

고른 방법
- 리뷰 권장안: 판정을 static refusalsOf(목록, 트리)로 뺀다. 트리는 파일
  이름 → (SQL, executeInTransaction)이다. 실제 목록 검사도 이 메서드를
  부른다. 같은 IT에 문자열 fixture로 위반 넷(트리에 없음, 동시 인덱스 문장
  없음, executeInTransaction=false 없음, 다른 문장과 섞임)과 허용 하나를
  둔다.
- 버린 대안: 판정을 testkit의 ConcurrentIndexMigrationInspector로 옮겨
  persistence-jpa 단위 테스트가 fixture를 갖게 한다. optional 빌드로
  복사하는 JDK 전용 파일이 늘고, ledger 전용 규칙(트리에 있어야 함)이
  testkit에 들어간다.

위험·한계
- fixture 케이스도 클래스의 Testcontainers PostgreSQL이 뜬 뒤 돈다(같은
  클래스라 컨테이너 하나를 같이 쓴다).
- RED 없음 — 테스트 구조 개선이다. 대신 변이: 기준 커밋에서 A11 변이가
  살아남고, 고친 코드에 같은 변이를 얹으면 위반 fixture가 실패한다.

변이 (desktop, 로그 ~/r9-g3-logs/)
- 기준 3eb3b067 + A11 변이: 목록 검사 1건 rc=0 통과(변이 생존)
- 이 커밋 + 같은 변이: GrpcOperationLedgerMigrationLockWaitIT rc=1,
  8건 중 위반 fixture 4건 실패(트리에 없음은 NPE, 나머지 셋은 "Expected
  size: 1 but was: 0")

검증 (desktop, 이 커밋의 트리 65218dff, --no-build-cache, 로그 ~/r9-g3-logs/)
- -p optional-platforms :grpc:grpc-operation-ledger-jpa:check rc=0,
  test 13건
- 같은 leaf integrationTest rc=0, 52건(새 5건 포함) 실패·skip 0
- 같은 leaf testQualityCheck rc=0
- FROM-CACHE 0

Refs: docs/reviews/2026-11-03-rereview/round8/03-migration.md RR8-03-04
Fixes: 5bb7825336 ("fix(migration): 실패한 동시 인덱스 마이그레이션 복구와 검사기 설명을 실제 동작에 맞춘다")
This commit is contained in:
DongHyeonka
2026-11-07 08:07:42 +09:00
parent 829ad58cfd
commit 59a39a6e59
@@ -2,6 +2,7 @@ package dev.caskeleton.grpc.ledger;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import dev.caskeleton.adapter.outbound.persistence.testkit.migration.ConcurrentIndexMigrationInspector;
import dev.caskeleton.adapter.outbound.persistence.testkit.migration.MigrationResource;
@@ -12,6 +13,8 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
@@ -22,6 +25,7 @@ import java.util.stream.Stream;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.postgresql.ds.PGSimpleDataSource;
import org.testcontainers.junit.jupiter.Container;
@@ -87,6 +91,13 @@ class GrpcOperationLedgerMigrationLockWaitIT {
*/
static final Map<String, String> RUN_OUTSIDE_A_TRANSACTION = Map.of();
/** The file name the list check's fixture cases list. */
private static final String LISTED = "V004__ledger_index.sql";
/** A concurrent index build alone, as the guide asks such a file to be. */
private static final String CONCURRENT_BUILD =
"CREATE INDEX CONCURRENTLY ix_ledger_note ON grpc_operation_ledger (note);\n";
/** Every ledger migration after V001, the one that creates the ledger, that runs in one. */
static Stream<String> migrationsAfterTheFirst() throws IOException {
return GrpcOperationLedgerMigrations.inOrder().stream()
@@ -141,18 +152,94 @@ class GrpcOperationLedgerMigrationLockWaitIT {
void everyMigrationListedAsRunningOutsideATransactionIsOneConcurrentIndexBuildThatSaysSo()
throws IOException {
// given
ConcurrentIndexMigrationInspector inspector = new ConcurrentIndexMigrationInspector();
List<String> tree = GrpcOperationLedgerMigrations.inOrder();
Map<String, LedgerMigration> tree = new LinkedHashMap<>();
for (String migration : GrpcOperationLedgerMigrations.inOrder()) {
tree.put(
migration,
new LedgerMigration(
GrpcOperationLedgerMigrations.named(migration),
GrpcOperationLedgerMigrations.executeInTransaction(migration)));
}
// when
List<String> refused = refusalsOf(RUN_OUTSIDE_A_TRANSACTION.keySet(), tree);
// then
assertThat(refused).isEmpty();
}
@ParameterizedTest
@MethodSource("listedFilesTheGuideDoesNotAllow")
@DisplayName(
"예외 목록 검사는 트리에 없는 파일, 동시 인덱스 문장이 없는 파일, executeInTransaction=false가 없는 파일, 다른 문장과 섞인 파일을 이유와 함께 거부한다")
void theListCheckRefusesAFileTheGuideDoesNotAllow(
String listed, Map<String, LedgerMigration> tree, String reason) {
// when
List<String> refused = refusalsOf(List.of(listed), tree);
// then
assertThat(refused).singleElement().asString().contains(listed).contains(reason);
}
@Test
@DisplayName("예외 목록 검사는 executeInTransaction=false를 곁에 둔, 동시 인덱스 문장 하나뿐인 파일을 받는다")
void theListCheckAcceptsAConcurrentIndexBuildAloneThatSaysSo() {
// given
Map<String, LedgerMigration> tree =
Map.of(LISTED, new LedgerMigration(CONCURRENT_BUILD, false));
// when
List<String> refused = refusalsOf(List.of(LISTED), tree);
// then
assertThat(refused).isEmpty();
}
/** One listed file for each way the guide's rule can be broken, and the reason it is refused. */
static Stream<Arguments> listedFilesTheGuideDoesNotAllow() {
return Stream.of(
arguments(LISTED, Map.of(), "is not a migration in the ledger tree"),
arguments(
LISTED,
Map.of(
LISTED,
new LedgerMigration(
"ALTER TABLE grpc_operation_ledger ADD COLUMN note TEXT;\n", false)),
"holds no concurrent index statement"),
arguments(
LISTED,
Map.of(LISTED, new LedgerMigration(CONCURRENT_BUILD, true)),
"must set executeInTransaction=false"),
arguments(
LISTED,
Map.of(
LISTED,
new LedgerMigration("SET LOCAL lock_timeout = '5s';\n" + CONCURRENT_BUILD, false)),
"mixes a concurrent index statement with other statements"));
}
/**
* Why each migration listed as running outside a transaction may not: one reason per file
* refused, none when every listed file is in the tree and is a concurrent index statement alone
* in its file with {@code executeInTransaction=false} beside it ("Concurrent index builds"). The
* list test runs it over {@link #RUN_OUTSIDE_A_TRANSACTION} and the tree, and the fixture cases
* hold it to every refusal while that list is empty (re-review 2026-11-03 round 8, RR8-03-04: its
* loop never ran, so a broken check passed).
*
* @param listed the file names listed
* @param tree every migration in the tree, by file name
* @return the reasons, in the order the files are listed
*/
static List<String> refusalsOf(Collection<String> listed, Map<String, LedgerMigration> tree) {
ConcurrentIndexMigrationInspector inspector = new ConcurrentIndexMigrationInspector();
List<String> refused = new ArrayList<>();
for (String migration : RUN_OUTSIDE_A_TRANSACTION.keySet()) {
if (!tree.contains(migration)) {
for (String migration : listed) {
LedgerMigration file = tree.get(migration);
if (file == null) {
refused.add(migration + " is not a migration in the ledger tree");
continue;
}
MigrationResource resource =
new MigrationResource(migration, GrpcOperationLedgerMigrations.named(migration));
MigrationResource resource = new MigrationResource(migration, file.sql());
if (!inspector.containsConcurrentIndexStatement(resource.sql())) {
refused.add(
migration
@@ -160,20 +247,17 @@ class GrpcOperationLedgerMigrationLockWaitIT {
+ " outside a transaction");
continue;
}
boolean inATransaction = GrpcOperationLedgerMigrations.executeInTransaction(migration);
Throwable thrown =
catchThrowable(
() -> {
inspector.validate(resource, inATransaction);
inspector.validate(resource, file.executeInTransaction());
inspector.requireIsolatedStatement(resource);
});
if (thrown != null) {
refused.add(thrown.getMessage());
}
}
// then
assertThat(refused).isEmpty();
return refused;
}
/** Runs the migration in one transaction while another one holds a write to the ledger. */
@@ -239,6 +323,15 @@ class GrpcOperationLedgerMigrationLockWaitIT {
return dataSource;
}
/**
* A ledger migration as the list check reads it.
*
* @param sql the script
* @param executeInTransaction whether Flyway runs it in a transaction, as {@link
* GrpcOperationLedgerMigrations#executeInTransaction} reads it from the file beside it
*/
record LedgerMigration(String sql, boolean executeInTransaction) {}
/** What the migration did while the writer held the ledger. */
private record Attempt(Throwable thrown, Duration took) {