|
8 | 8 | // REQUIRES: concurrency_runtime
|
9 | 9 | // UNSUPPORTED: back_deployment_runtime
|
10 | 10 |
|
11 |
| -struct Boom: Error {} |
12 |
| -struct IgnoredBoom: Error {} |
| 11 | +struct Boom: Error { |
| 12 | + let id: String |
| 13 | + |
| 14 | + init(file: String = #fileID, line: UInt = #line) { |
| 15 | + self.id = "\(file):\(line)" |
| 16 | + } |
| 17 | + init(id: String) { |
| 18 | + self.id = id |
| 19 | + } |
| 20 | +} |
13 | 21 |
|
| 22 | +struct IgnoredBoom: Error {} |
14 | 23 | func echo(_ i: Int) async -> Int { i }
|
15 |
| -func boom() async throws -> Int { throw Boom() } |
| 24 | + |
| 25 | +func test_taskGroup_throws_rethrows() async { |
| 26 | + print("==== \(#function) ------") // CHECK_LABEL: test_taskGroup_throws_rethrows |
| 27 | + do { |
| 28 | + let got = try await withThrowingTaskGroup(of: Int.self, returning: Int.self) { group in |
| 29 | + group.addTask { await echo(1) } |
| 30 | + group.addTask { await echo(2) } |
| 31 | + group.addTask { throw Boom() } |
| 32 | + |
| 33 | + do { |
| 34 | + while let r = try await group.next() { |
| 35 | + print("next: \(r)") |
| 36 | + } |
| 37 | + } catch { |
| 38 | + // CHECK: error caught and rethrown in group: Boom( |
| 39 | + print("error caught and rethrown in group: \(error)") |
| 40 | + throw error |
| 41 | + } |
| 42 | + |
| 43 | + print("should have thrown") |
| 44 | + return 0 |
| 45 | + } |
| 46 | + |
| 47 | + print("Expected error to be thrown, but got: \(got)") |
| 48 | + } catch { |
| 49 | + // CHECK: rethrown: Boom( |
| 50 | + print("rethrown: \(error)") |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func test_taskGroup_noThrow_ifNotAwaitedThrowingTask() async { |
| 55 | + print("==== \(#function) ------") // CHECK_LABEL: test_taskGroup_noThrow_ifNotAwaitedThrowingTask |
| 56 | + let got = await withThrowingTaskGroup(of: Int.self, returning: Int.self) { group in |
| 57 | + group.addTask { await echo(1) } |
| 58 | + guard let r = try! await group.next() else { |
| 59 | + return 0 |
| 60 | + } |
| 61 | + |
| 62 | + group.addTask { throw Boom() } |
| 63 | + // don't consume this task, so we're not throwing here |
| 64 | + |
| 65 | + return r |
| 66 | + } |
| 67 | + |
| 68 | + print("Expected no error to be thrown, got: \(got)") // CHECK: Expected no error to be thrown, got: 1 |
| 69 | +} |
16 | 70 |
|
17 | 71 | func test_taskGroup_discardResults_automaticallyRethrows() async {
|
18 |
| - print("==== \(#function) ------") |
| 72 | + print("==== \(#function) ------") // CHECK_LABEL: test_taskGroup_discardResults_automaticallyRethrows |
19 | 73 | do {
|
20 | 74 | let got = try await withThrowingTaskGroup(of: Int.self, returning: Int.self,
|
21 | 75 | discardResults: true) { group in
|
22 | 76 | group.addTask { await echo(1) }
|
23 |
| - group.addTask { try await boom() } |
| 77 | + group.addTask { throw Boom() } |
24 | 78 | // add a throwing task, but don't consume it explicitly
|
25 | 79 | // since we're in discard results mode, all will be awaited and the first error it thrown
|
26 |
| - |
27 | 80 | return 13
|
28 | 81 | }
|
29 | 82 |
|
30 | 83 | print("Expected error to be thrown, but got: \(got)")
|
31 | 84 | } catch {
|
32 |
| - // CHECK: rethrown: Boom() |
| 85 | + // CHECK: rethrown: Boom( |
33 | 86 | print("rethrown: \(error)")
|
34 | 87 | }
|
35 | 88 | }
|
36 | 89 |
|
| 90 | +func test_taskGroup_discardResults_automaticallyRethrowsOnlyFirst() async { |
| 91 | + print("==== \(#function) ------") // CHECK_LABEL: test_taskGroup_discardResults_automaticallyRethrowsOnlyFirst |
| 92 | + do { |
| 93 | + let got = try await withThrowingTaskGroup(of: Int.self, returning: Int.self, |
| 94 | + discardResults: true) { group in |
| 95 | + group.addTask { await echo(1) } |
| 96 | + group.addTask { throw Boom(id: "first") } |
| 97 | + // add a throwing task, but don't consume it explicitly |
| 98 | + // since we're in discard results mode, all will be awaited and the first error it thrown |
| 99 | + |
| 100 | + do { |
| 101 | + try await group.waitForAll() |
| 102 | + } catch { |
| 103 | + // CHECK: caught: Boom(id: "first") |
| 104 | + print("caught: \(error)") |
| 105 | + } |
| 106 | + |
| 107 | + group.addTask { throw Boom(id: "second") } |
| 108 | + |
| 109 | + return 4 |
| 110 | + } |
| 111 | + |
| 112 | + print("Expected error to be thrown, but got: \(got)") |
| 113 | + } catch { |
| 114 | + // CHECK: rethrown: Boom(id: "second") |
| 115 | + print("rethrown: \(error)") |
| 116 | + } |
| 117 | +} |
37 | 118 |
|
38 | 119 | @available(SwiftStdlib 5.1, *)
|
39 | 120 | @main struct Main {
|
40 | 121 | static func main() async {
|
41 |
| -// await test_taskGroup_throws_rethrows() |
42 |
| -// await test_taskGroup_noThrow_ifNotAwaitedThrowingTask() |
| 122 | + await test_taskGroup_throws_rethrows() |
| 123 | + await test_taskGroup_noThrow_ifNotAwaitedThrowingTask() |
43 | 124 | await test_taskGroup_discardResults_automaticallyRethrows()
|
| 125 | + await test_taskGroup_discardResults_automaticallyRethrowsOnlyFirst() |
44 | 126 | }
|
45 | 127 | }
|
0 commit comments