|
| 1 | +package life.qbic.projectmanagement.application.sync |
| 2 | + |
| 3 | +import life.qbic.projectmanagement.application.api.AsyncProjectService.RawDataset |
| 4 | +import life.qbic.projectmanagement.application.dataset.LocalRawDatasetCache |
| 5 | +import life.qbic.projectmanagement.application.dataset.RemoteRawDataService |
| 6 | +import life.qbic.projectmanagement.application.sync.WatermarkRepo.Watermark |
| 7 | +import spock.lang.Specification |
| 8 | +import spock.lang.Subject |
| 9 | + |
| 10 | +import java.time.Instant |
| 11 | + |
| 12 | +/** |
| 13 | + * Unit tests for {@link RawDataSyncService}, focusing on watermark update logic. |
| 14 | + * |
| 15 | + * <p>The outer {@code sync()} method runs a time-boxed while-loop calling {@code runSync()}. |
| 16 | + * Tests that need to observe a single iteration set up the mocks so that the first call returns |
| 17 | + * a partial/empty page (causing {@code runSync()} to return {@code false} and the loop to break |
| 18 | + * immediately). Tests that need to observe two iterations chain mock responses.</p> |
| 19 | + */ |
| 20 | +class RawDataSyncServiceSpec extends Specification { |
| 21 | + |
| 22 | + static final int MAX_QUERY_SIZE = 1_000 |
| 23 | + static final String JOB_NAME = "RAW_DATA_SYNC_EXTERNAL" |
| 24 | + |
| 25 | + RemoteRawDataService remoteRawDataServiceMock = Mock() |
| 26 | + WatermarkRepo watermarkRepoMock = Mock() |
| 27 | + LocalRawDatasetCache localRawDatasetCacheMock = Mock() |
| 28 | + |
| 29 | + @Subject |
| 30 | + RawDataSyncService service = new RawDataSyncService( |
| 31 | + remoteRawDataServiceMock, |
| 32 | + watermarkRepoMock, |
| 33 | + localRawDatasetCacheMock |
| 34 | + ) |
| 35 | + |
| 36 | + // ------------------------------------------------------------------ |
| 37 | + // Helpers |
| 38 | + // ------------------------------------------------------------------ |
| 39 | + |
| 40 | + private static RawDataset dataset(Instant registrationDate) { |
| 41 | + new RawDataset("QABCD001", 1024L, 1, Set.of(".fastq"), registrationDate) |
| 42 | + } |
| 43 | + |
| 44 | + private static List<RawDataset> fullPage() { |
| 45 | + (1..MAX_QUERY_SIZE).collect { dataset(Instant.EPOCH.plusSeconds(it)) } |
| 46 | + } |
| 47 | + |
| 48 | + private static List<RawDataset> partialPage(int size) { |
| 49 | + assert size > 0 && size < MAX_QUERY_SIZE |
| 50 | + (1..size).collect { dataset(Instant.EPOCH.plusSeconds(it)) } |
| 51 | + } |
| 52 | + |
| 53 | + // ------------------------------------------------------------------ |
| 54 | + // Race-condition fix: updatedSince must be captured BEFORE the remote query |
| 55 | + // ------------------------------------------------------------------ |
| 56 | + |
| 57 | + def "race condition fix: updatedSince is not later than lastSuccessAt, proving it was captured before the query completed"() { |
| 58 | + given: "no prior watermark" |
| 59 | + watermarkRepoMock.fetch(JOB_NAME) >> Optional.empty() |
| 60 | + |
| 61 | + and: "the remote returns a partial page so the loop terminates after one iteration" |
| 62 | + remoteRawDataServiceMock.registeredSince(_, _, _) >> partialPage(3) |
| 63 | + |
| 64 | + Watermark savedWatermark = null |
| 65 | + watermarkRepoMock.save(_ as Watermark) >> { Watermark w -> savedWatermark = w } |
| 66 | + |
| 67 | + when: "we capture the time just before the sync runs" |
| 68 | + Instant beforeSync = Instant.now() |
| 69 | + service.sync() |
| 70 | + Instant afterSync = Instant.now() |
| 71 | + |
| 72 | + then: "updatedSince falls within the [beforeSync, afterSync] window" |
| 73 | + // updatedSince must be >= beforeSync: it was captured inside runSync(), after entry |
| 74 | + !savedWatermark.updatedSince().isBefore(beforeSync) |
| 75 | + // updatedSince must be <= afterSync: it was captured before sync() returned |
| 76 | + !savedWatermark.updatedSince().isAfter(afterSync) |
| 77 | + |
| 78 | + and: "updatedSince is not after lastSuccessAt, proving it was captured before (or at the same moment as) the save" |
| 79 | + // queryTime is captured before the remote call; lastSuccessAt is captured after. |
| 80 | + // Therefore updatedSince <= lastSuccessAt always holds when the fix is in place. |
| 81 | + !savedWatermark.updatedSince().isAfter(savedWatermark.lastSuccessAt()) |
| 82 | + } |
| 83 | + |
| 84 | + // ------------------------------------------------------------------ |
| 85 | + // Bug regression: partial page must update watermark to now |
| 86 | + // ------------------------------------------------------------------ |
| 87 | + |
| 88 | + def "when the last page is partial, the saved watermark updatedSince is close to now (not a remote entity timestamp)"() { |
| 89 | + given: "no prior watermark" |
| 90 | + watermarkRepoMock.fetch(JOB_NAME) >> Optional.empty() |
| 91 | +
|
| 92 | + and: "the remote returns a partial page (< MAX_QUERY_SIZE) — loop will break after one iteration" |
| 93 | + def items = partialPage(5) |
| 94 | + remoteRawDataServiceMock.registeredSince(_, _, _) >> items |
| 95 | +
|
| 96 | + and: "capture the saved watermark" |
| 97 | + Watermark savedWatermark = null |
| 98 | + watermarkRepoMock.save(_ as Watermark) >> { Watermark w -> savedWatermark = w } |
| 99 | +
|
| 100 | + when: |
| 101 | + service.sync() |
| 102 | +
|
| 103 | + then: "updatedSince is NOT one of the ancient remote registration timestamps (EPOCH + a few seconds)" |
| 104 | + savedWatermark != null |
| 105 | + // All item registrationDates are <= EPOCH + 5s (effectively year 1970). |
| 106 | + // The saved updatedSince must be close to the current time (within 10s of now). |
| 107 | + savedWatermark.updatedSince().isAfter(Instant.now().minusSeconds(10)) |
| 108 | +
|
| 109 | + and: "the offset is reset to 0 so the next run starts fresh" |
| 110 | + savedWatermark.syncOffset() == 0 |
| 111 | + } |
| 112 | +
|
| 113 | + def "bug regression: watermark does NOT stay stuck at the remote entity timestamp after a full sync completes"() { |
| 114 | + given: "an initial watermark stuck at EPOCH (simulating the bug state)" |
| 115 | + def priorWatermark = new Watermark(JOB_NAME, 0, Instant.EPOCH, Instant.EPOCH) |
| 116 | + watermarkRepoMock.fetch(JOB_NAME) >> Optional.of(priorWatermark) |
| 117 | +
|
| 118 | + and: "the remote returns 10 items with very old registrationDates (EPOCH + 1..10 s)" |
| 119 | + def oldItems = (1..10).collect { dataset(Instant.EPOCH.plusSeconds(it)) } |
| 120 | + remoteRawDataServiceMock.registeredSince(_, _, _) >> oldItems |
| 121 | +
|
| 122 | + Watermark savedWatermark = null |
| 123 | + watermarkRepoMock.save(_ as Watermark) >> { Watermark w -> savedWatermark = w } |
| 124 | +
|
| 125 | + when: |
| 126 | + service.sync() |
| 127 | +
|
| 128 | + then: "the saved watermark's updatedSince is NOT stuck at an ancient remote timestamp" |
| 129 | + savedWatermark.updatedSince() != Instant.EPOCH |
| 130 | + savedWatermark.updatedSince().isAfter(Instant.now().minusSeconds(10)) |
| 131 | +
|
| 132 | + and: "offset is 0 so we do not re-paginate the old window" |
| 133 | + savedWatermark.syncOffset() == 0 |
| 134 | + } |
| 135 | +
|
| 136 | + // ------------------------------------------------------------------ |
| 137 | + // Empty page: watermark also advances to now |
| 138 | + // ------------------------------------------------------------------ |
| 139 | +
|
| 140 | + def "when the remote returns an empty page, the saved watermark updatedSince is close to now"() { |
| 141 | + given: "a prior watermark with a non-zero offset (previous page was exactly full)" |
| 142 | + def priorWatermark = new Watermark(JOB_NAME, MAX_QUERY_SIZE, Instant.EPOCH, Instant.EPOCH) |
| 143 | + watermarkRepoMock.fetch(JOB_NAME) >> Optional.of(priorWatermark) |
| 144 | +
|
| 145 | + and: "the remote returns an empty page — loop will break after one iteration" |
| 146 | + remoteRawDataServiceMock.registeredSince(_, _, _) >> [] |
| 147 | +
|
| 148 | + Watermark savedWatermark = null |
| 149 | + watermarkRepoMock.save(_ as Watermark) >> { Watermark w -> savedWatermark = w } |
| 150 | +
|
| 151 | + when: |
| 152 | + service.sync() |
| 153 | +
|
| 154 | + then: |
| 155 | + savedWatermark != null |
| 156 | + savedWatermark.updatedSince().isAfter(Instant.now().minusSeconds(10)) |
| 157 | + savedWatermark.syncOffset() == 0 |
| 158 | + } |
| 159 | +
|
| 160 | + // ------------------------------------------------------------------ |
| 161 | + // Full page: offset advances, updatedSince stays unchanged |
| 162 | + // ------------------------------------------------------------------ |
| 163 | +
|
| 164 | + def "when a full page is returned, offset advances and updatedSince is preserved until the last page"() { |
| 165 | + given: "a prior watermark at offset 0 with a specific updatedSince" |
| 166 | + Instant originalUpdatedSince = Instant.parse("2025-01-01T00:00:00Z") |
| 167 | + def initialWatermark = new Watermark(JOB_NAME, 0, originalUpdatedSince, Instant.EPOCH) |
| 168 | +
|
| 169 | + // First fetch returns the initial watermark; the second fetch (after offset advances) |
| 170 | + // simulates the stored watermark being reloaded by Spock's iterator stub behaviour. |
| 171 | + // We give the second fetch a watermark with the advanced offset so runSync can terminate. |
| 172 | + def advancedWatermark = new Watermark(JOB_NAME, MAX_QUERY_SIZE, originalUpdatedSince, Instant.now()) |
| 173 | + watermarkRepoMock.fetch(JOB_NAME) >>> [ |
| 174 | + Optional.of(initialWatermark), // 1st iteration |
| 175 | + Optional.of(advancedWatermark) // 2nd iteration |
| 176 | + ] |
| 177 | + |
| 178 | + and: "first remote call returns a full page; second call (advanced offset) returns a partial page" |
| 179 | + remoteRawDataServiceMock.registeredSince(_, 0, _) >> fullPage() |
| 180 | + remoteRawDataServiceMock.registeredSince(_, MAX_QUERY_SIZE, _) >> partialPage(3) |
| 181 | + |
| 182 | + List<Watermark> savedWatermarks = [] |
| 183 | + watermarkRepoMock.save(_ as Watermark) >> { Watermark w -> savedWatermarks << w } |
| 184 | + |
| 185 | + when: |
| 186 | + service.sync() |
| 187 | + |
| 188 | + then: "exactly two watermarks were saved" |
| 189 | + savedWatermarks.size() == 2 |
| 190 | + |
| 191 | + and: "the first saved watermark advances the offset and keeps updatedSince unchanged" |
| 192 | + savedWatermarks[0].syncOffset() == MAX_QUERY_SIZE |
| 193 | + savedWatermarks[0].updatedSince() == originalUpdatedSince |
| 194 | + |
| 195 | + and: "the second saved watermark (last page) resets the offset and advances updatedSince to now" |
| 196 | + savedWatermarks[1].syncOffset() == 0 |
| 197 | + savedWatermarks[1].updatedSince().isAfter(Instant.now().minusSeconds(10)) |
| 198 | + } |
| 199 | + |
| 200 | + // ------------------------------------------------------------------ |
| 201 | + // Dataset persistence |
| 202 | + // ------------------------------------------------------------------ |
| 203 | + |
| 204 | + def "datasets from a partial page are persisted"() { |
| 205 | + given: |
| 206 | + watermarkRepoMock.fetch(JOB_NAME) >> Optional.empty() |
| 207 | + def items = partialPage(7) |
| 208 | + remoteRawDataServiceMock.registeredSince(_, _, _) >> items |
| 209 | + watermarkRepoMock.save(_ as Watermark) >> {} |
| 210 | + |
| 211 | + when: |
| 212 | + service.sync() |
| 213 | + |
| 214 | + then: |
| 215 | + 1 * localRawDatasetCacheMock.saveAll(items) |
| 216 | + } |
| 217 | + |
| 218 | + def "datasets from both pages are persisted during multi-page sync"() { |
| 219 | + given: |
| 220 | + def initialWatermark = new Watermark(JOB_NAME, 0, Instant.EPOCH, Instant.EPOCH) |
| 221 | + def advancedWatermark = new Watermark(JOB_NAME, MAX_QUERY_SIZE, Instant.EPOCH, Instant.now()) |
| 222 | + watermarkRepoMock.fetch(JOB_NAME) >>> [ |
| 223 | + Optional.of(initialWatermark), |
| 224 | + Optional.of(advancedWatermark) |
| 225 | + ] |
| 226 | + |
| 227 | + def page1 = fullPage() |
| 228 | + def page2 = partialPage(1) |
| 229 | + remoteRawDataServiceMock.registeredSince(_, 0, _) >> page1 |
| 230 | + remoteRawDataServiceMock.registeredSince(_, MAX_QUERY_SIZE, _) >> page2 |
| 231 | + watermarkRepoMock.save(_ as Watermark) >> {} |
| 232 | + |
| 233 | + when: |
| 234 | + service.sync() |
| 235 | + |
| 236 | + then: |
| 237 | + 1 * localRawDatasetCacheMock.saveAll(page1) |
| 238 | + 1 * localRawDatasetCacheMock.saveAll(page2) |
| 239 | + } |
| 240 | + |
| 241 | + def "when the remote returns an empty page, no datasets are persisted"() { |
| 242 | + given: |
| 243 | + watermarkRepoMock.fetch(JOB_NAME) >> Optional.empty() |
| 244 | + remoteRawDataServiceMock.registeredSince(_, _, _) >> [] |
| 245 | + watermarkRepoMock.save(_ as Watermark) >> {} |
| 246 | + |
| 247 | + when: |
| 248 | + service.sync() |
| 249 | + |
| 250 | + then: |
| 251 | + 0 * localRawDatasetCacheMock.saveAll(_) |
| 252 | + } |
| 253 | +} |
0 commit comments