|
| 1 | +package io.cloudshift.gateway.dualwrite; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.time.LocalDate; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +class CutoverRollbackTest { |
| 11 | + |
| 12 | + /** In-memory backend that records every write so we can check for loss or duplication. */ |
| 13 | + private static final class RecordingStore implements ReservationWriter { |
| 14 | + private final List<ReservationRecord> stored = new ArrayList<>(); |
| 15 | + |
| 16 | + @Override |
| 17 | + public ReservationRecord write(ReservationRecord request) { |
| 18 | + stored.add(request); |
| 19 | + return request; |
| 20 | + } |
| 21 | + |
| 22 | + List<ReservationRecord> contents() { |
| 23 | + return List.copyOf(stored); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private static ReservationRecord reservation(long roomId, String guest) { |
| 28 | + return new ReservationRecord( |
| 29 | + roomId, guest, LocalDate.of(2026, 1, 1), LocalDate.of(2026, 1, 2)); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void cutoverThenRollbackLeavesTheMonolithConsistent() { |
| 34 | + RecordingStore monolith = new RecordingStore(); |
| 35 | + RecordingStore service = new RecordingStore(); |
| 36 | + CutoverCoordinator coordinator = new CutoverCoordinator(monolith, service); |
| 37 | + |
| 38 | + // Write before any migration. |
| 39 | + coordinator.write(reservation(1, "before")); |
| 40 | + |
| 41 | + // Open the migration window and write while dual-writing. |
| 42 | + coordinator.beginDualWrite(); |
| 43 | + coordinator.write(reservation(2, "dual")); |
| 44 | + |
| 45 | + // Flip reads to the service and write while cut over. |
| 46 | + coordinator.cutOver(); |
| 47 | + coordinator.write(reservation(3, "cutover")); |
| 48 | + assertThat(coordinator.readBackend()).isSameAs(service); |
| 49 | + |
| 50 | + // Roll back to the monolith. |
| 51 | + coordinator.rollBack(); |
| 52 | + coordinator.write(reservation(4, "after")); |
| 53 | + assertThat(coordinator.readBackend()).isSameAs(monolith); |
| 54 | + |
| 55 | + // The monolith saw every write exactly once: nothing lost, nothing duplicated. |
| 56 | + List<ReservationRecord> monolithView = monolith.contents(); |
| 57 | + assertThat(monolithView) |
| 58 | + .containsExactly( |
| 59 | + reservation(1, "before"), |
| 60 | + reservation(2, "dual"), |
| 61 | + reservation(3, "cutover"), |
| 62 | + reservation(4, "after")); |
| 63 | + assertThat(monolithView).doesNotHaveDuplicates(); |
| 64 | + |
| 65 | + // The service only holds the writes made from the migration window onward. |
| 66 | + assertThat(service.contents()) |
| 67 | + .containsExactly(reservation(2, "dual"), reservation(3, "cutover")); |
| 68 | + } |
| 69 | +} |
0 commit comments