Skip to content

Commit 8c1fe2c

Browse files
committed
Prevent saving first sync date if it doesn’t happen
1 parent e900ecb commit 8c1fe2c

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public enum POSCatalogSyncError: Error, Equatable {
5757
case syncAlreadyInProgress(siteID: Int64)
5858
case negativeMaxAge
5959
case requestCancelled
60+
case shouldNotSync
6061
}
6162

6263
public actor POSCatalogSyncCoordinator: POSCatalogSyncCoordinatorProtocol {
@@ -90,7 +91,7 @@ public actor POSCatalogSyncCoordinator: POSCatalogSyncCoordinatorProtocol {
9091
}
9192

9293
guard try await shouldPerformFullSync(for: siteID, maxAge: maxAge) else {
93-
return
94+
throw POSCatalogSyncError.shouldNotSync
9495
}
9596

9697
if case .syncStarted = fullSyncStateModel.state[siteID] {

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ struct POSCatalogSyncCoordinatorTests {
111111
let thirtyMinutesAgo = Date().addingTimeInterval(-30 * 60)
112112
try createSiteInDatabase(siteID: sampleSiteID, lastFullSyncDate: thirtyMinutesAgo)
113113

114-
// When - max age is 1 hour
115-
let _ = try await sut.performFullSyncIfApplicable(for: sampleSiteID, maxAge: sampleMaxAge)
114+
// When - max age is 1 hour / Then
115+
await #expect(throws: POSCatalogSyncError.shouldNotSync) {
116+
try await sut.performFullSyncIfApplicable(for: sampleSiteID, maxAge: sampleMaxAge)
117+
}
116118

117-
// Then
118119
#expect(mockSyncService.startFullSyncCallCount == 0)
119120
}
120121

@@ -128,11 +129,12 @@ struct POSCatalogSyncCoordinatorTests {
128129
try createSiteInDatabase(siteID: siteA, lastFullSyncDate: oneHourAgo)
129130
try createSiteInDatabase(siteID: siteB, lastFullSyncDate: nil)
130131

131-
// When
132-
let _ = try await sut.performFullSyncIfApplicable(for: siteA, maxAge: 2 * sampleMaxAge)
132+
// When / Then
133+
await #expect(throws: POSCatalogSyncError.shouldNotSync) {
134+
let _ = try await sut.performFullSyncIfApplicable(for: siteA, maxAge: 2 * sampleMaxAge)
135+
}
133136
let _ = try await sut.performFullSyncIfApplicable(for: siteB, maxAge: 2 * sampleMaxAge)
134137

135-
// Then
136138
#expect(mockSyncService.startFullSyncCallCount == 1)
137139
#expect(mockSyncService.lastSyncSiteID == siteB)
138140
}
@@ -168,9 +170,11 @@ struct POSCatalogSyncCoordinatorTests {
168170
try createSiteInDatabase(siteID: sampleSiteID, lastFullSyncDate: recentSyncDate)
169171

170172
// When - max age is 1 hour
171-
let _ = try await sut.performFullSyncIfApplicable(for: sampleSiteID, maxAge: sampleMaxAge)
172-
173173
// Then - should not sync because site exists and time hasn't passed
174+
await #expect(throws: POSCatalogSyncError.shouldNotSync) {
175+
let _ = try await sut.performFullSyncIfApplicable(for: sampleSiteID, maxAge: sampleMaxAge)
176+
}
177+
174178
#expect(mockSyncService.startFullSyncCallCount == 0)
175179
}
176180

@@ -639,10 +643,11 @@ extension POSCatalogSyncCoordinatorTests {
639643
)
640644
try createSiteInDatabase(siteID: sampleSiteID, lastFullSyncDate: nil)
641645

642-
// When
643-
try await coordinator.performSmartSync(for: sampleSiteID)
646+
// When / Then - sync should be skipped
647+
await #expect(throws: POSCatalogSyncError.shouldNotSync) {
648+
try await coordinator.performSmartSync(for: sampleSiteID)
649+
}
644650

645-
// Then - sync should be skipped
646651
#expect(mockSyncService.startFullSyncCallCount == 0)
647652
#expect(mockIncrementalSyncService.startIncrementalSyncCallCount == 0)
648653
}

WooCommerce/Classes/Tools/BackgroundTasks/ForegroundPOSCatalogSyncDispatcher.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ final actor ForegroundPOSCatalogSyncDispatcher {
161161
DDLogError("⛔️ ForegroundPOSCatalogSyncDispatcher: Invalid max age for site \(siteID)")
162162
case .requestCancelled:
163163
DDLogInfo("ℹ️ ForegroundPOSCatalogSyncDispatcher: Sync request was cancelled for site \(siteID)")
164+
case .shouldNotSync:
165+
DDLogInfo("ℹ️ ForegroundPOSCatalogSyncDispatcher: Should not sync site \(siteID) at this time")
164166
}
165167
} catch {
166168
DDLogError("⛔️ ForegroundPOSCatalogSyncDispatcher: Sync failed for site \(siteID): \(error)")

0 commit comments

Comments
 (0)