1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library) | %FileCheck %s --dump-input=always
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
// UNSUPPORTED: freestanding
// FIXME: enable discarding taskgroup on windows; rdar://104762037
// UNSUPPORTED: OS=windows-msvc
actor Waiter {
let until: Int
var count: Int
var cc: CheckedContinuation<Int, Never>?
init(until: Int) {
self.until = until
self.count = 0
}
func increment() {
self.count += 1
if self.until <= self.count {
if let cc = self.cc {
cc.resume(returning: self.count)
}
}
}
func wait() async -> Int {
if self.until <= self.count {
return self.count
}
return await withCheckedContinuation { cc in
self.cc = cc
}
}
}
func test_discardingTaskGroup_neverConsume() async {
print(">>> \(#function)")
let until = 100
let waiter = Waiter(until: until)
print("Start tasks: \(until)")
let allTasks = await withDiscardingTaskGroup() { group in
for n in 1...until {
group.addTask {
try? await Task.sleep(until: .now + .milliseconds(100), clock: .continuous)
await waiter.increment()
}
}
return until
}
// CHECK: all tasks: 100
print("all tasks: \(allTasks)")
}
func test_discardingTaskGroup_neverConsume(sleepBeforeGroupWaitAll: Duration) async {
print(">>> \(#function)")
let until = 100
let waiter = Waiter(until: until)
print("Start tasks: \(until)")
let allTasks = await withDiscardingTaskGroup() { group in
for n in 1...until {
group.addTask {
try? await Task.sleep(until: .now + .milliseconds(100), clock: .continuous)
await waiter.increment()
}
}
// wait a little bit, so some tasks complete before we hit the implicit "wait at end of task group scope"
try? await Task.sleep(until: .now + sleepBeforeGroupWaitAll, clock: .continuous)
return until
}
// CHECK: all tasks: 100
print("all tasks: \(allTasks)")
}
@main struct Main {
static func main() async {
await test_discardingTaskGroup_neverConsume()
await test_discardingTaskGroup_neverConsume(sleepBeforeGroupWaitAll: .milliseconds(500))
}
}
|