|
| 1 | +// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency %import-libdispatch -parse-as-library -sanitize=thread) |
| 2 | + |
| 3 | +// REQUIRES: executable_test |
| 4 | +// REQUIRES: concurrency |
| 5 | +// REQUIRES: libdispatch |
| 6 | +// REQUIRES: tsan_runtime |
| 7 | + |
| 8 | +var scratchBuffer: UnsafeMutableBufferPointer<Int> = .allocate(capacity: 1000) |
| 9 | + |
| 10 | +@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) |
| 11 | +func completeFastOrSlow(n: Int) async -> Int { |
| 12 | + if n % 2 == 0 { |
| 13 | + await Task.sleep(2_000_000_000) |
| 14 | + } |
| 15 | + assert(scratchBuffer[n] == 6) |
| 16 | + scratchBuffer[n] = 7 |
| 17 | + return n |
| 18 | +} |
| 19 | + |
| 20 | +@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) |
| 21 | +func test_sum_nextOnCompletedOrPending() async { |
| 22 | + scratchBuffer.initialize(repeating: 0) |
| 23 | + |
| 24 | + let numbers = 0..<1000 |
| 25 | + let expected = 499_500 |
| 26 | + |
| 27 | + let sum = await withTaskGroup(of: Int.self) { (group) async -> Int in |
| 28 | + for n in numbers { |
| 29 | + scratchBuffer[n] = 6 |
| 30 | + await group.spawn { |
| 31 | + let res = await completeFastOrSlow(n: n) |
| 32 | + return res |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // We want to await on completed and pending child tasks. This gives the |
| 37 | + // fast tasks some time to complete before we call group.next(). |
| 38 | + await Task.sleep(1_000_000_000) |
| 39 | + |
| 40 | + var sum = 0 |
| 41 | + while let r = try! await group.next() { |
| 42 | + assert(scratchBuffer[r] == 7) |
| 43 | + sum += r |
| 44 | + } |
| 45 | + |
| 46 | + assert(group.isEmpty, "Group must be empty after we consumed all tasks") |
| 47 | + |
| 48 | + print("task group returning: \(sum)") |
| 49 | + return sum |
| 50 | + } |
| 51 | + |
| 52 | + print("result: \(sum)") |
| 53 | + assert(sum == expected, "Expected: \(expected), got: \(sum)") |
| 54 | +} |
| 55 | + |
| 56 | +@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) |
| 57 | +@main struct Main { |
| 58 | + static func main() async { |
| 59 | + await test_sum_nextOnCompletedOrPending() |
| 60 | + } |
| 61 | +} |
0 commit comments