@@ -18,24 +18,49 @@ class StorageBackgroundEventsRegistryTests: XCTestCase {
1818 let otherIdentifier = UUID ( ) . uuidString
1919 StorageBackgroundEventsRegistry . register ( identifier: identifier)
2020
21- let done = asyncExpectation ( description: " done " , expectedFulfillmentCount: 2 )
21+ let notificationCenter = NotificationCenter ( )
22+ StorageBackgroundEventsRegistry . notificationCenter = notificationCenter
23+ defer {
24+ StorageBackgroundEventsRegistry . notificationCenter = nil
25+ }
26+
27+ let done = asyncExpectation ( description: " done " )
28+ let waiting = asyncExpectation ( description: " waiting " )
29+
30+ notificationCenter. addObserver ( forName: Notification . Name. StorageBackgroundEventsRegistryWaiting, object: nil , queue: nil ) { notification in
31+ guard let notificationIdentifier = notification. object as? String else {
32+ XCTFail ( " Identifier not defined " )
33+ return
34+ }
35+ XCTAssertEqual ( notificationIdentifier, identifier)
36+ Task {
37+ await waiting. fulfill ( )
38+ }
39+ }
2240
2341 Task {
2442 let handled = await StorageBackgroundEventsRegistry . handleEventsForBackgroundURLSession ( identifier: identifier)
2543 await done. fulfill ( )
2644 XCTAssertTrue ( handled)
2745 }
2846
47+ await waitForExpectations ( [ waiting] )
48+
49+ let didContinue = await handleEvents ( for: identifier)
50+ XCTAssertTrue ( didContinue)
51+ await waitForExpectations ( [ done] )
52+
53+ let otherDone = asyncExpectation ( description: " other done " )
54+
2955 Task {
3056 let otherHandled = await StorageBackgroundEventsRegistry . handleEventsForBackgroundURLSession ( identifier: otherIdentifier)
31- await done . fulfill ( )
57+ await otherDone . fulfill ( )
3258 XCTAssertFalse ( otherHandled)
3359 }
3460
35- handleEvents ( for: identifier)
36- handleEvents ( for: otherIdentifier)
37-
38- await waitForExpectations ( [ done] )
61+ let didNotContinue = await handleEvents ( for: otherIdentifier)
62+ XCTAssertFalse ( didNotContinue)
63+ await waitForExpectations ( [ otherDone] )
3964 }
4065
4166 func testHandlingUnregisteredIdentifier( ) async throws {
@@ -55,10 +80,16 @@ class StorageBackgroundEventsRegistryTests: XCTestCase {
5580 }
5681
5782 // Simulates URLSessionDelegate behavior
58- func handleEvents( for identifier: String ) {
83+ func handleEvents( for identifier: String ) async -> Bool {
84+ await Task . yield ( )
85+
5986 if let continuation = StorageBackgroundEventsRegistry . getContinuation ( for: identifier) {
6087 continuation. resume ( returning: true )
6188 StorageBackgroundEventsRegistry . removeContinuation ( for: identifier)
89+ return true
90+ } else {
91+ print ( " No continuation for identifier: \( identifier) " )
92+ return false
6293 }
6394 }
6495
0 commit comments