Skip to content

Commit 0f208e7

Browse files
committed
Use task.isCancelled property instead of returning from closure
Check cancellation status directly on the Task instance rather than returning Task.isCancelled from within the closure.
1 parent 7cf84a1 commit 0f208e7

File tree

1 file changed

+45
-54
lines changed

1 file changed

+45
-54
lines changed

Tests/AsyncQueueTests/CancellableQueueTests.swift

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@ struct CancellableQueueTests {
3131
func cancelTasks_fifoQueue_doesNotCancelCompletedTask() async throws {
3232
let systemUnderTest = CancellableQueue(underlyingQueue: FIFOQueue())
3333

34-
// Create a task that completes immediately and returns its cancellation status
34+
// Create a task that completes immediately
3535
let task = Task(on: systemUnderTest) {
3636
try doWork()
37-
return Task.isCancelled
3837
}
3938

4039
// Wait for the task to complete
41-
let wasCancelled = try await task.value
40+
try await task.value
4241

4342
// Now cancel tasks - should have no effect since task already completed
4443
systemUnderTest.cancelTasks()
4544

46-
#expect(!wasCancelled)
45+
#expect(!task.isCancelled)
4746
}
4847

4948
@Test
@@ -56,7 +55,6 @@ struct CancellableQueueTests {
5655
let task = Task(on: systemUnderTest) {
5756
await taskStarted.signal()
5857
await proceedAfterCancel.wait()
59-
return Task.isCancelled
6058
}
6159

6260
// Wait for the task to start executing
@@ -68,7 +66,8 @@ struct CancellableQueueTests {
6866
// Signal the semaphore to let the task continue
6967
await proceedAfterCancel.signal()
7068

71-
#expect(await task.value)
69+
await task.value
70+
#expect(task.isCancelled)
7271
}
7372

7473
@Test
@@ -82,17 +81,12 @@ struct CancellableQueueTests {
8281
let task1 = Task(on: systemUnderTest, isolatedTo: counter) { _ in
8382
await taskStarted.signal()
8483
await proceedAfterCancel.wait()
85-
return Task.isCancelled
8684
}
8785

8886
// Create pending tasks that won't start until the first task completes
89-
let task2 = Task(on: systemUnderTest, isolatedTo: counter) { _ in
90-
Task.isCancelled
91-
}
87+
let task2 = Task(on: systemUnderTest, isolatedTo: counter) { _ in }
9288

93-
let task3 = Task(on: systemUnderTest, isolatedTo: counter) { _ in
94-
Task.isCancelled
95-
}
89+
let task3 = Task(on: systemUnderTest, isolatedTo: counter) { _ in }
9690

9791
// Wait for the first task to start executing
9892
await taskStarted.wait()
@@ -103,9 +97,12 @@ struct CancellableQueueTests {
10397
// Signal the semaphore to let tasks continue
10498
await proceedAfterCancel.signal()
10599

106-
#expect(await task1.value)
107-
#expect(await task2.value)
108-
#expect(await task3.value)
100+
await task1.value
101+
await task2.value
102+
await task3.value
103+
#expect(task1.isCancelled)
104+
#expect(task2.isCancelled)
105+
#expect(task3.isCancelled)
109106
}
110107

111108
@Test
@@ -119,10 +116,10 @@ struct CancellableQueueTests {
119116
// Create a task after cancellation - it should NOT be cancelled
120117
let task = Task(on: systemUnderTest, isolatedTo: counter) { _ in
121118
try doWork()
122-
return Task.isCancelled
123119
}
124120

125-
#expect(try await !task.value)
121+
try await task.value
122+
#expect(!task.isCancelled)
126123
}
127124

128125
// MARK: ActorQueue Tests
@@ -134,19 +131,18 @@ struct CancellableQueueTests {
134131
actorQueue.adoptExecutionContext(of: counter)
135132
let systemUnderTest = CancellableQueue(underlyingQueue: actorQueue)
136133

137-
// Create a task that completes immediately and returns its cancellation status
134+
// Create a task that completes immediately
138135
let task = Task(on: systemUnderTest) { _ in
139136
try doWork()
140-
return Task.isCancelled
141137
}
142138

143139
// Wait for the task to complete
144-
let wasCancelled = try await task.value
140+
try await task.value
145141

146142
// Now cancel tasks - should have no effect since task already completed
147143
systemUnderTest.cancelTasks()
148144

149-
#expect(!wasCancelled)
145+
#expect(!task.isCancelled)
150146
}
151147

152148
@Test
@@ -162,7 +158,6 @@ struct CancellableQueueTests {
162158
let task = Task(on: systemUnderTest) { _ in
163159
await taskStarted.signal()
164160
await proceedAfterCancel.wait()
165-
return Task.isCancelled
166161
}
167162

168163
// Wait for the task to start executing
@@ -174,7 +169,8 @@ struct CancellableQueueTests {
174169
// Signal the semaphore to let the task continue
175170
await proceedAfterCancel.signal()
176171

177-
#expect(await task.value)
172+
await task.value
173+
#expect(task.isCancelled)
178174
}
179175

180176
@Test
@@ -190,17 +186,12 @@ struct CancellableQueueTests {
190186
let task1 = Task(on: systemUnderTest) { _ in
191187
await taskStarted.signal()
192188
await proceedAfterCancel.wait()
193-
return Task.isCancelled
194189
}
195190

196191
// Create pending tasks that won't start until the first task suspends
197-
let task2 = Task(on: systemUnderTest) { _ in
198-
Task.isCancelled
199-
}
192+
let task2 = Task(on: systemUnderTest) { _ in }
200193

201-
let task3 = Task(on: systemUnderTest) { _ in
202-
Task.isCancelled
203-
}
194+
let task3 = Task(on: systemUnderTest) { _ in }
204195

205196
// Wait for the first task to start executing
206197
await taskStarted.wait()
@@ -211,9 +202,12 @@ struct CancellableQueueTests {
211202
// Signal the semaphore to let tasks continue
212203
await proceedAfterCancel.signal()
213204

214-
#expect(await task1.value)
215-
#expect(await task2.value)
216-
#expect(await task3.value)
205+
await task1.value
206+
await task2.value
207+
await task3.value
208+
#expect(task1.isCancelled)
209+
#expect(task2.isCancelled)
210+
#expect(task3.isCancelled)
217211
}
218212

219213
@Test
@@ -229,10 +223,10 @@ struct CancellableQueueTests {
229223
// Create a task after cancellation - it should NOT be cancelled
230224
let task = Task(on: systemUnderTest) { _ in
231225
try doWork()
232-
return Task.isCancelled
233226
}
234227

235-
#expect(try await !task.value)
228+
try await task.value
229+
#expect(!task.isCancelled)
236230
}
237231

238232
// MARK: MainActor Queue Tests
@@ -241,19 +235,18 @@ struct CancellableQueueTests {
241235
func cancelTasks_mainActorQueue_doesNotCancelCompletedTask() async throws {
242236
let systemUnderTest = CancellableQueue(underlyingQueue: MainActor.queue)
243237

244-
// Create a task that completes immediately and returns its cancellation status
238+
// Create a task that completes immediately
245239
let task = Task(on: systemUnderTest) {
246240
try doWork()
247-
return Task.isCancelled
248241
}
249242

250243
// Wait for the task to complete
251-
let wasCancelled = try await task.value
244+
try await task.value
252245

253246
// Now cancel tasks - should have no effect since task already completed
254247
systemUnderTest.cancelTasks()
255248

256-
#expect(!wasCancelled)
249+
#expect(!task.isCancelled)
257250
}
258251

259252
@Test
@@ -266,7 +259,6 @@ struct CancellableQueueTests {
266259
let task = Task(on: systemUnderTest) {
267260
await taskStarted.signal()
268261
await proceedAfterCancel.wait()
269-
return Task.isCancelled
270262
}
271263

272264
// Wait for the task to start executing
@@ -278,7 +270,8 @@ struct CancellableQueueTests {
278270
// Signal the semaphore to let the task continue
279271
await proceedAfterCancel.signal()
280272

281-
#expect(await task.value)
273+
await task.value
274+
#expect(task.isCancelled)
282275
}
283276

284277
@Test
@@ -291,17 +284,12 @@ struct CancellableQueueTests {
291284
let task1 = Task(on: systemUnderTest) {
292285
await taskStarted.signal()
293286
await proceedAfterCancel.wait()
294-
return Task.isCancelled
295287
}
296288

297289
// Create pending tasks that won't start until the first task suspends
298-
let task2 = Task(on: systemUnderTest) {
299-
Task.isCancelled
300-
}
290+
let task2 = Task(on: systemUnderTest) { }
301291

302-
let task3 = Task(on: systemUnderTest) {
303-
Task.isCancelled
304-
}
292+
let task3 = Task(on: systemUnderTest) { }
305293

306294
// Wait for the first task to start executing
307295
await taskStarted.wait()
@@ -312,9 +300,12 @@ struct CancellableQueueTests {
312300
// Signal the semaphore to let tasks continue
313301
await proceedAfterCancel.signal()
314302

315-
#expect(await task1.value)
316-
#expect(await task2.value)
317-
#expect(await task3.value)
303+
await task1.value
304+
await task2.value
305+
await task3.value
306+
#expect(task1.isCancelled)
307+
#expect(task2.isCancelled)
308+
#expect(task3.isCancelled)
318309
}
319310

320311
@Test
@@ -327,10 +318,10 @@ struct CancellableQueueTests {
327318
// Create a task after cancellation - it should NOT be cancelled
328319
let task = Task(on: systemUnderTest) {
329320
try doWork()
330-
return Task.isCancelled
331321
}
332322

333-
#expect(try await !task.value)
323+
try await task.value
324+
#expect(!task.isCancelled)
334325
}
335326

336327
// MARK: Private

0 commit comments

Comments
 (0)