Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(sync) Add tests to run fetchSaves and fetchArchive at the same t… #710

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions PocketKit/Sources/Sync/PocketSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,20 @@ extension PocketSource {
/// - queue: The operation queue to run the task on
/// - completion: The completion block to execute when the operation is done. If you need to do cleanup work, you should instead do the completion work within the operation itself because they launch BackgroundTasks
private func enqueue(operation: SyncOperation, task: SyncTask, queue: OperationQueue, completion: (() -> Void)? = nil) {
let persistentTask: PersistentSyncTask = PersistentSyncTask(context: space.backgroundContext)
let childBGContext = space.makeChildBackgroundContext()
let persistentTask: PersistentSyncTask = PersistentSyncTask(context: childBGContext)
persistentTask.createdAt = Date()
persistentTask.syncTaskContainer = SyncTaskContainer(task: task)
try? space.save()

// save the child context
try? childBGContext.performAndWait {
guard childBGContext.hasChanges else {
return
}
try childBGContext.save()
// then save the parent context
try space.save()
}

enqueue(operation: operation, persistentTask: persistentTask, queue: queue, completion: completion)
}
Expand Down
30 changes: 30 additions & 0 deletions PocketKit/Tests/SyncTests/PocketSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ class PocketSourceTests: XCTestCase {
)
}

func test_fetching_from_different_threads() async {
sessionProvider.session = MockSession()

operations.stubFetchSaves { _, _, _, _ in
TestSyncOperation { }
}

operations.stubFetchArchive { _, _, _, _ in
TestSyncOperation { }
}

let source = subject()

let expectationToFetchSaves = expectation(description: "Fetch Saves")
let expectationToFetchArchive = expectation(description: "Fetch Archive")

Task {
source.refreshSaves {
expectationToFetchSaves.fulfill()
}
}
Task {
source.refreshArchive {
expectationToFetchArchive.fulfill()
}
}

await fulfillment(of: [expectationToFetchSaves, expectationToFetchArchive], timeout: 10)
}

func test_refresh_addsFetchSavesOperationToQueue() {
let session = MockSession()
sessionProvider.session = session
Expand Down
41 changes: 23 additions & 18 deletions PocketKit/Tests/SyncTests/Support/MockOperationFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SharedPocketKit
class MockOperationFactory: SyncOperationFactory {
private var implementations: [String: Any] = [:]
private var calls: [String: [Any]] = [:]
private var lock: DispatchQueue = DispatchQueue(label: "")
}

// MARK: - fetchSaves
Expand Down Expand Up @@ -44,15 +45,17 @@ extension MockOperationFactory {
fatalError("\(Self.self).\(#function) has not been stubbed")
}

calls["fetchSaves"] = (calls["fetchSaves"] ?? []) + [
FetchSavesCall(
apollo: apollo,
space: space,
events: events,
initialDownloadState: initialDownloadState,
lastRefresh: lastRefresh
)
]
lock.sync {
calls["fetchSaves"] = (calls["fetchSaves"] ?? []) + [
FetchSavesCall(
apollo: apollo,
space: space,
events: events,
initialDownloadState: initialDownloadState,
lastRefresh: lastRefresh
)
]
}

return impl(apollo, space, events, initialDownloadState)
}
Expand Down Expand Up @@ -98,15 +101,17 @@ extension MockOperationFactory {
fatalError("\(Self.self).\(#function) has not been stubbed")
}

calls["fetchArchive"] = (calls["fetchArchive"] ?? []) + [
FetchArchiveCall(
apollo: apollo,
space: space,
events: events,
initialDownloadState: initialDownloadState,
lastRefresh: lastRefresh
)
]
lock.sync {
calls["fetchArchive"] = (calls["fetchArchive"] ?? []) + [
FetchArchiveCall(
apollo: apollo,
space: space,
events: events,
initialDownloadState: initialDownloadState,
lastRefresh: lastRefresh
)
]
}

return impl(apollo, space, events, initialDownloadState)
}
Expand Down