@@ -18,24 +18,49 @@ class StorageBackgroundEventsRegistryTests: XCTestCase {
18
18
let otherIdentifier = UUID ( ) . uuidString
19
19
StorageBackgroundEventsRegistry . register ( identifier: identifier)
20
20
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
+ }
22
40
23
41
Task {
24
42
let handled = await StorageBackgroundEventsRegistry . handleEventsForBackgroundURLSession ( identifier: identifier)
25
43
await done. fulfill ( )
26
44
XCTAssertTrue ( handled)
27
45
}
28
46
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
+
29
55
Task {
30
56
let otherHandled = await StorageBackgroundEventsRegistry . handleEventsForBackgroundURLSession ( identifier: otherIdentifier)
31
- await done . fulfill ( )
57
+ await otherDone . fulfill ( )
32
58
XCTAssertFalse ( otherHandled)
33
59
}
34
60
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] )
39
64
}
40
65
41
66
func testHandlingUnregisteredIdentifier( ) async throws {
@@ -55,10 +80,16 @@ class StorageBackgroundEventsRegistryTests: XCTestCase {
55
80
}
56
81
57
82
// Simulates URLSessionDelegate behavior
58
- func handleEvents( for identifier: String ) {
83
+ func handleEvents( for identifier: String ) async -> Bool {
84
+ await Task . yield ( )
85
+
59
86
if let continuation = StorageBackgroundEventsRegistry . getContinuation ( for: identifier) {
60
87
continuation. resume ( returning: true )
61
88
StorageBackgroundEventsRegistry . removeContinuation ( for: identifier)
89
+ return true
90
+ } else {
91
+ print ( " No continuation for identifier: \( identifier) " )
92
+ return false
62
93
}
63
94
}
64
95
0 commit comments