Skip to content

Commit

Permalink
Add a unit test for data syncing feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoy committed Dec 19, 2023
1 parent 92d815d commit fb5df7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
8 changes: 0 additions & 8 deletions Sources/DDGSync/internal/SyncQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,12 @@ final class SyncQueue {
}

func cancelOngoingSyncAndSuspendQueue() {
guard isDataSyncingFeatureFlagEnabled else {
os_log(.debug, log: self.log, "Sync Feature is temporarily disabled, not adjusting sync queue")
return
}
os_log(.debug, log: self.log, "Cancelling sync and suspending sync queue")
operationQueue.cancelAllOperations()
operationQueue.isSuspended = true
}

func resumeQueue() {
guard isDataSyncingFeatureFlagEnabled else {
os_log(.debug, log: self.log, "Sync Feature is temporarily disabled, not adjusting sync queue")
return
}
os_log(.debug, log: self.log, "Resuming sync queue")
operationQueue.isSuspended = false
}
Expand Down
25 changes: 25 additions & 0 deletions Tests/DDGSyncTests/SyncQueueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ class SyncQueueTests: XCTestCase {
requestMaker = SyncRequestMaker(storage: storage, api: apiMock, endpoints: endpoints)
}

func testWhenDataSyncingFeatureFlagIsDisabledThenNewOperationsAreNotEnqueued() async {
let syncQueue = SyncQueue(dataProviders: [], storage: storage, crypter: crypter, api: apiMock, endpoints: endpoints)
XCTAssertFalse(syncQueue.operationQueue.isSuspended)

var syncDidStartEvents = [Bool]()
let cancellable = syncQueue.isSyncInProgressPublisher.removeDuplicates().filter({ $0 }).sink { syncDidStartEvents.append($0) }

syncQueue.isDataSyncingFeatureFlagEnabled = false

await syncQueue.startSync()
await syncQueue.startSync()
await syncQueue.startSync()

XCTAssertTrue(syncDidStartEvents.isEmpty)

syncQueue.isDataSyncingFeatureFlagEnabled = true

await syncQueue.startSync()
await syncQueue.startSync()
await syncQueue.startSync()

cancellable.cancel()
XCTAssertEqual(syncDidStartEvents.count, 3)
}

func testThatInProgressPublisherEmitsValuesWhenSyncStartsAndEndsWithSuccess() async throws {
let feature = Feature(name: "bookmarks")
let dataProvider = DataProvidingMock(feature: feature)
Expand Down

0 comments on commit fb5df7c

Please sign in to comment.