implements new Storage API for handleEventsForBackgroundURLSession#2481
implements new Storage API for handleEventsForBackgroundURLSession#2481brennanMKE wants to merge 4 commits into
Conversation
|
|
||
| import Foundation | ||
|
|
||
| extension Notification.Name { |
There was a problem hiding this comment.
This notification was setup just for unit test? Looks like this will be fired in the production app as well. We should think about a better way to make this class testable?
There was a problem hiding this comment.
What other options do you suggest?
There was a problem hiding this comment.
The test worked without the notification expectation:
func testRegisteringAndUnregister() async throws {
let identifier = UUID().uuidString
let otherIdentifier = UUID().uuidString
await StorageBackgroundEventsRegistry.shared.register(identifier: identifier)
let done = asyncExpectation(description: "done")
Task {
let handled = await StorageBackgroundEventsRegistry.shared.handleEventsForBackgroundURLSession(identifier: identifier)
await done.fulfill()
XCTAssertTrue(handled)
}
let didContinue = await handleEvents(for: identifier)
XCTAssertTrue(didContinue)
await waitForExpectations([done])
let otherDone = asyncExpectation(description: "other done")
Task {
let otherHandled = await StorageBackgroundEventsRegistry.shared.handleEventsForBackgroundURLSession(identifier: otherIdentifier)
await otherDone.fulfill()
XCTAssertFalse(otherHandled)
}
let didNotContinue = await handleEvents(for: otherIdentifier)
XCTAssertFalse(didNotContinue)
await waitForExpectations([otherDone])
}There was a problem hiding this comment.
You have to run it on macOS many times repeatedly to get it to fail. Without the notification it is a flaky test which will fail based on timing.
There was a problem hiding this comment.
This issue can then happen in the real app as well right? What if the URLSession call back reach before we setup the continuation?
There was a problem hiding this comment.
Another option could be to create an overloaded function just for test:
func handleEventsForBackgroundURLSession(identifier: String) async -> Bool {
return await handleEventsForBackgroundURLSession(identifier: identifier, notifyTest: nil)
}
func handleEventsForBackgroundURLSession(identifier: String, notifyTest: ((String) -> Void)? ) async -> Bool {
guard self.identifier == identifier else { return false }
return await withCheckedContinuation { (continuation: CheckedContinuation<Bool, Never>) in
self.continuation = continuation
if let notifyTest = notifyTest {
notifyTest(identifier)
}
}
}…ationCenter is defined
0a1c1ef to
175dc7d
Compare
|
When can we expect this to be released? |
Issue #, if available:
N/A
Description of changes:
Check points: (check or cross out if not relevant)
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.