@@ -28,153 +28,98 @@ struct RetrievedCapabilitiesActorTests {
2828
2929 @Test func setOngoingFetchTrueCausesSuspension( ) async throws {
3030 let actor = RetrievedCapabilitiesActor ( )
31- var awaiterDidProceed = false
3231
3332 // 1. Mark fetch as ongoing
3433 await actor . setOngoingFetch ( forAccount: account1, ongoing: true )
3534
3635 // 2. Attempt to await in a separate task
37- let awaitingTask = Task {
36+ await confirmation ( " awaiter did proceed " ) { didProceed in
3837 await actor . awaitFetchCompletion ( forAccount: account1)
39- awaiterDidProceed = true // This should only become true after resumption
38+ didProceed ( ) // This should only become true after resumption
4039 }
4140
42- // 3. Give the awaitingTask a moment to potentially run and suspend
43- try await Task . sleep ( for: . milliseconds( 100 ) )
44- #expect( !awaiterDidProceed, " `awaitFetchCompletion` should suspend if fetch is ongoing. " )
45-
46- // 4. Clean up: complete the fetch to allow the task to finish
41+ // 3. Clean up: complete the fetch to allow the task to finish
4742 await actor . setOngoingFetch ( forAccount: account1, ongoing: false )
48- await awaitingTask. value // Ensure the task fully completes
49- #expect( awaiterDidProceed, " Awaiter should proceed after fetch is no longer ongoing. " )
5043 }
5144
5245 @Test func setOngoingFetchFalseResumesAwaiter( ) async throws {
5346 let actor = RetrievedCapabilitiesActor ( )
54- var awaiterCompleted = false
5547
5648 // 1. Mark fetch as ongoing and start an awaiter
5749 await actor . setOngoingFetch ( forAccount: account1, ongoing: true )
58- let awaitingTask = Task {
50+
51+ await confirmation ( " awaiter completed " ) { awaiterCompleted in
5952 await actor . awaitFetchCompletion ( forAccount: account1)
60- awaiterCompleted = true
53+ awaiterCompleted ( )
6154 }
6255
63- // 2. Ensure it's waiting
64- try await Task . sleep ( for: . milliseconds( 100 ) )
65- #expect( awaiterCompleted == false , " Awaiter should be suspended initially. " )
66-
67- // 3. Mark fetch as not ongoing, which should resume the awaiter
56+ // 2. Mark fetch as not ongoing, which should resume the awaiter
6857 await actor . setOngoingFetch ( forAccount: account1, ongoing: false )
69-
70- // 4. Await the task's completion and check the flag
71- await awaitingTask. value
72- #expect( awaiterCompleted, " Awaiter should complete after `setOngoingFetch(false)`. " )
7358 }
7459
7560 @Test func awaitFetchCompletionReturnsImmediately( ) async throws {
7661 let actor = RetrievedCapabilitiesActor ( )
77- var didAwaiterCompleteImmediately = false
7862
79- let task = Task {
63+ await confirmation ( " did awaiter complete immediately " ) { didAwaiterCompleteImmediately in
8064 await actor . awaitFetchCompletion ( forAccount: account1)
81- didAwaiterCompleteImmediately = true
65+ didAwaiterCompleteImmediately ( )
8266 }
83- await task. value
84-
85- #expect(
86- didAwaiterCompleteImmediately,
87- " `awaitFetchCompletion` should let the task complete quickly if no fetch is ongoing. "
88- )
8967 }
9068
9169 @Test func awaitFetchCompletion_suspendsAndResumes_behavioral( ) async throws {
9270 let actor = RetrievedCapabilitiesActor ( )
93- var didAwaiterComplete = false
9471
9572 // 1. Mark fetch as ongoing
9673 await actor . setOngoingFetch ( forAccount: account1, ongoing: true )
9774
9875 // 2. Start task that awaits
99- let awaitingTask = Task {
76+ await confirmation ( " did awaiter complete " ) { didAwaiterComplete in
10077 await actor . awaitFetchCompletion ( forAccount: account1)
101- didAwaiterComplete = true
78+ didAwaiterComplete ( )
10279 }
10380
104- // 3. Check for suspension (indirectly)
105- try await Task . sleep ( for: . milliseconds( 100 ) )
106- #expect( !didAwaiterComplete, " Awaiter should be suspended while fetch is ongoing. " )
107-
108- // 4. Mark fetch as completed
81+ // 3. Mark fetch as completed
10982 await actor . setOngoingFetch ( forAccount: account1, ongoing: false )
110-
111- // 5. Awaiter should complete
112- await awaitingTask. value
113- #expect( didAwaiterComplete, " Awaiter should complete after fetch is no longer ongoing. " )
11483 }
11584
11685 @Test func awaitFetchCompletion_multipleAwaiters_behavioral( ) async throws {
11786 let actor = RetrievedCapabilitiesActor ( )
118- var awaiter1Complete = false
119- var awaiter2Complete = false
12087
12188 await actor . setOngoingFetch ( forAccount: account1, ongoing: true )
12289
123- let task1 = Task {
90+ await confirmation ( " firstAwaiterCompleted " ) { firstAwaiterCompleted in
12491 await actor . awaitFetchCompletion ( forAccount: account1)
125- awaiter1Complete = true
92+ firstAwaiterCompleted ( )
12693 }
127- let task2 = Task {
94+
95+ await confirmation ( " secondAwaiterCompleted " ) { secondAwaiterCompleted in
12896 await actor . awaitFetchCompletion ( forAccount: account1)
129- awaiter2Complete = true
97+ secondAwaiterCompleted ( )
13098 }
13199
132- try await Task . sleep ( for: . milliseconds( 100 ) )
133- #expect( !awaiter1Complete && !awaiter2Complete, " Both awaiters should be suspended. " )
134-
135100 await actor . setOngoingFetch ( forAccount: account1, ongoing: false )
136-
137- await task1. value
138- await task2. value
139- #expect(
140- awaiter1Complete && awaiter2Complete,
141- " Both awaiters should complete after fetch is no longer ongoing. "
142- )
143101 }
144102
145103 @Test func setOngoingFetch_false_isolatesAccountResumption_behavioral( ) async throws {
146104 let actor = RetrievedCapabilitiesActor ( )
147- var acc1AwaiterDone = false
148- var acc2AwaiterDone = false
149105
150106 // Start fetches for both accounts
151107 await actor . setOngoingFetch ( forAccount: account1, ongoing: true )
152108 await actor . setOngoingFetch ( forAccount: account2, ongoing: true )
153109
154110 // Setup awaiters
155- let taskAcc1 = Task {
111+ await confirmation ( " firstAccountAwaiterDone " ) { firstAccountAwaiterDone in
156112 await actor . awaitFetchCompletion ( forAccount: account1)
157- acc1AwaiterDone = true
113+ firstAccountAwaiterDone ( )
158114 }
159- let taskAcc2 = Task {
115+
116+ await confirmation ( " secondAccountAwaiterDone " ) { secondAccountAwaiterDone in
160117 await actor . awaitFetchCompletion ( forAccount: account2)
161- acc2AwaiterDone = true
118+ secondAccountAwaiterDone ( )
162119 }
163120
164- try await Task . sleep ( for: . milliseconds( 100 ) ) // Allow tasks to suspend
165- #expect( !acc1AwaiterDone && !acc2AwaiterDone, " Both awaiters initially suspended. " )
166-
167121 // Complete fetch for account1 ONLY
168122 await actor . setOngoingFetch ( forAccount: account1, ongoing: false )
169- await taskAcc1. value
170-
171- #expect( acc1AwaiterDone, " Awaiter for account1 should complete. " )
172- #expect( !acc2AwaiterDone, " Awaiter for account2 should still be suspended. " )
173-
174- // Complete fetch for account2
175123 await actor . setOngoingFetch ( forAccount: account2, ongoing: false )
176- await taskAcc2. value // Wait for acc2's awaiter to complete
177-
178- #expect( acc2AwaiterDone, " Awaiter for account2 should now complete. " )
179124 }
180125}
0 commit comments