Skip to content

Commit 591a6eb

Browse files
committed
gardening
1 parent 0cf60ac commit 591a6eb

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

Tests/IdentifiableContinuationTests.swift

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ struct IdentifiableContinuationTests {
8686
let waiter = Waiter<String?, Never>()
8787

8888
let task = await waiter.makeTask(delay: 1.0, onCancel: nil)
89-
try? await Task.sleep(seconds: 0.1)
9089
let isEmpty = await waiter.isEmpty
9190
#expect(isEmpty)
9291
task.cancel()
@@ -160,7 +159,6 @@ struct IdentifiableContinuationTests {
160159
let waiter = Waiter<String?, any Error>()
161160

162161
let task = await waiter.makeTask(delay: 1.0, onCancel: .failure(CancellationError()))
163-
try? await Task.sleep(seconds: 0.1)
164162
let isEmpty = await waiter.isEmpty
165163
#expect(isEmpty)
166164
task.cancel()
@@ -181,42 +179,58 @@ private actor Waiter<T: Sendable, E: Error> {
181179
waiting.isEmpty
182180
}
183181

184-
func makeTask(delay: TimeInterval = 0, onCancel: T) -> Task<T, Never> where E == Never {
182+
func makeTask(delay: TimeInterval, onCancel: T) -> Task<T, Never> where E == Never {
185183
Task {
186184
try? await Task.sleep(seconds: delay)
187-
#if compiler(>=6.0)
188185
return await withIdentifiableContinuation {
189186
addContinuation($0)
190187
} onCancel: { id in
191188
Task { await self.resumeID(id, returning: onCancel) }
192189
}
193-
#else
194-
return await withIdentifiableContinuation(isolation: self) {
190+
}
191+
}
192+
193+
func makeTask(onCancel: T) async -> Task<T, Never> where E == Never {
194+
nonisolated(unsafe) var continuation: CheckedContinuation<Void, Never>!
195+
let task = Task {
196+
return await withIdentifiableContinuation {
195197
addContinuation($0)
198+
continuation.resume()
196199
} onCancel: { id in
197200
Task { await self.resumeID(id, returning: onCancel) }
198201
}
199-
#endif
200202
}
203+
await withCheckedContinuation {
204+
continuation = $0
205+
}
206+
return task
201207
}
202208

203-
func makeTask(delay: TimeInterval = 0, onCancel: Result<T, E>) -> Task<T, any Error> where E == any Error {
209+
func makeTask(delay: TimeInterval, onCancel: Result<T, E>) -> Task<T, any Error> where E == any Error {
204210
Task {
205211
try? await Task.sleep(seconds: delay)
206-
#if compiler(>=6.0)
207212
return try await withIdentifiableThrowingContinuation {
208213
addContinuation($0)
209214
} onCancel: { id in
210215
Task { await self.resumeID(id, with: onCancel) }
211216
}
212-
#else
213-
return try await withIdentifiableThrowingContinuation(isolation: self) {
217+
}
218+
}
219+
220+
func makeTask(onCancel: Result<T, E>) async -> Task<T, any Error> where E == any Error {
221+
nonisolated(unsafe) var continuation: CheckedContinuation<Void, Never>!
222+
let task = Task {
223+
try await withIdentifiableThrowingContinuation {
214224
addContinuation($0)
225+
continuation.resume()
215226
} onCancel: { id in
216227
Task { await self.resumeID(id, with: onCancel) }
217228
}
218-
#endif
219229
}
230+
await withCheckedContinuation {
231+
continuation = $0
232+
}
233+
return task
220234
}
221235

222236
private func addContinuation(_ continuation: Continuation) {

0 commit comments

Comments
 (0)