Skip to content

Commit f4dd740

Browse files
authored
[Local catalog] Skip incremental sync when full sync in progress (#16326)
2 parents b86ecd7 + 87b06f9 commit f4dd740

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

Modules/Sources/Yosemite/Tools/POS/POSCatalogSyncCoordinator.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,9 @@ public actor POSCatalogSyncCoordinator: POSCatalogSyncCoordinatorProtocol {
105105
throw POSCatalogSyncError.shouldNotSync
106106
}
107107

108-
switch fullSyncStateModel.state[siteID] {
109-
case .syncStarted, .initialSyncStarted:
108+
if await fullSyncInProgress(for: siteID) {
110109
DDLogInfo("⚠️ POSCatalogSyncCoordinator: Sync already in progress for site \(siteID)")
111110
throw POSCatalogSyncError.syncAlreadyInProgress(siteID: siteID)
112-
default:
113-
break
114111
}
115112

116113
let isFirstSync = await lastFullSyncDate(for: siteID) == nil
@@ -199,6 +196,19 @@ public actor POSCatalogSyncCoordinator: POSCatalogSyncCoordinatorProtocol {
199196
return shouldSync
200197
}
201198

199+
private func fullSyncInProgress(for siteID: Int64) async -> Bool {
200+
switch fullSyncStateModel.state[siteID] {
201+
case .syncStarted, .initialSyncStarted:
202+
return true
203+
default:
204+
return false
205+
}
206+
}
207+
208+
private func ongoingSyncInProgress(for siteID: Int64) async -> Bool {
209+
ongoingIncrementalSyncs.contains(siteID)
210+
}
211+
202212
/// Performs an incremental sync if applicable based on sync conditions
203213
/// - Parameters:
204214
/// - siteID: The site ID to sync catalog for
@@ -213,11 +223,16 @@ public actor POSCatalogSyncCoordinator: POSCatalogSyncCoordinatorProtocol {
213223
return
214224
}
215225

216-
if ongoingIncrementalSyncs.contains(siteID) {
226+
if await ongoingSyncInProgress(for: siteID) {
217227
DDLogInfo("⚠️ POSCatalogSyncCoordinator: Incremental sync already in progress for site \(siteID)")
218228
throw POSCatalogSyncError.syncAlreadyInProgress(siteID: siteID)
219229
}
220230

231+
if await fullSyncInProgress(for: siteID) {
232+
DDLogInfo("⚠️ POSCatalogSyncCoordinator: Full sync already in progress for site \(siteID)")
233+
throw POSCatalogSyncError.syncAlreadyInProgress(siteID: siteID)
234+
}
235+
221236
guard let lastFullSyncDate = await lastFullSyncDate(for: siteID) else {
222237
return
223238
}

Modules/Tests/YosemiteTests/Tools/POS/POSCatalogSyncCoordinatorTests.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,8 @@ struct POSCatalogSyncCoordinatorTests {
195195
try await Task.sleep(nanoseconds: 10_000_000) // 10ms
196196

197197
// When - try to start second sync while first is blocked
198-
do {
198+
await #expect(throws: POSCatalogSyncError.syncAlreadyInProgress(siteID: sampleSiteID)) {
199199
_ = try await sut.performFullSync(for: sampleSiteID)
200-
#expect(Bool(false), "Should have thrown syncAlreadyInProgress error")
201-
} catch let error as POSCatalogSyncError {
202-
// Then
203-
#expect(error == POSCatalogSyncError.syncAlreadyInProgress(siteID: sampleSiteID))
204200
}
205201

206202
let currentState = await sut.loadLastFullSyncState(for: sampleSiteID)
@@ -370,12 +366,8 @@ struct POSCatalogSyncCoordinatorTests {
370366
try await Task.sleep(nanoseconds: 10_000_000) // 10ms
371367

372368
// When - try to start second incremental sync while first is blocked
373-
do {
374-
_ = try await sut.performIncrementalSyncIfApplicable(for: sampleSiteID, maxAge: sampleMaxAge)
375-
#expect(Bool(false), "Should have thrown syncAlreadyInProgress error")
376-
} catch let error as POSCatalogSyncError {
377-
// Then
378-
#expect(error == POSCatalogSyncError.syncAlreadyInProgress(siteID: sampleSiteID))
369+
await #expect(throws: POSCatalogSyncError.syncAlreadyInProgress(siteID: sampleSiteID)) {
370+
try await sut.performIncrementalSyncIfApplicable(for: sampleSiteID, maxAge: sampleMaxAge)
379371
}
380372

381373
// Cleanup

0 commit comments

Comments
 (0)