Skip to content

Commit 9906199

Browse files
authored
[Concurrency] Harden task group test which is sensitive to timing (#75612)
1 parent 3ea6e70 commit 9906199

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

test/Concurrency/Runtime/async_taskgroup_throw_rethrow.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,15 @@ func test_discardingTaskGroup_automaticallyRethrows_first_withThrowingBodyFirst(
159159
_ = await echo(1)
160160
}
161161
group.addTask {
162-
try? await Task.sleep(until: .now + .seconds(10), clock: .continuous)
163-
let error = Boom(id: "task, second, isCancelled:\(Task.isCancelled)")
162+
let start: ContinuousClock.Instant = .now
163+
do {
164+
// Sleep for a long time because we need to account for very slow test runners;
165+
// This should rarely actually wait so long as throwing from the group will cancel and wake-up this child task.
166+
try await Task.sleep(until: start + .seconds(30), clock: .continuous)
167+
} catch {
168+
print("Child task cancelled! Slept: \(ContinuousClock.Instant.now - start)")
169+
}
170+
let error = Boom(id: "task, second, isCancelled:\(Task.isCancelled), slept: \(ContinuousClock.Instant.now - start)")
164171
print("Throwing: \(error)")
165172
throw error
166173
}
@@ -173,6 +180,7 @@ func test_discardingTaskGroup_automaticallyRethrows_first_withThrowingBodyFirst(
173180
fatalError("Expected error to be thrown")
174181
} catch {
175182
// CHECK: Throwing: Boom(id: "body, first, isCancelled:false
183+
// CHECK: Child task cancelled!
176184
// CHECK: Throwing: Boom(id: "task, second, isCancelled:true
177185
// and only then the re-throw happens:
178186
// CHECK: rethrown: Boom(id: "body, first

0 commit comments

Comments
 (0)