Skip to content

Commit 9c0deaf

Browse files
committed
[TaskGroup] simplify throw_recover test a little bit
1 parent 5e92d71 commit 9c0deaf

File tree

1 file changed

+30
-49
lines changed

1 file changed

+30
-49
lines changed

test/Concurrency/Runtime/async_taskgroup_throw_recover.swift

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,64 +14,45 @@ func boom() async throws -> Int {
1414
}
1515

1616
func test_taskGroup_throws() async {
17-
do {
18-
print("start \(#function)")
19-
let got = try! await Task.withGroup(resultType: Int.self) {
20-
group async throws -> Int in
21-
await group.add { await one() }
22-
await group.add { try await boom() }
17+
let got: Int = try! await Task.withGroup(resultType: Int.self) { group in
18+
await group.add { try await boom() }
2319

24-
do {
25-
while let r = try await group.next() {
26-
print("next: \(r)")
27-
}
28-
} catch {
29-
print("error caught in group: \(error)")
30-
31-
await group.add { () async -> Int in
32-
let c = await Task.__unsafeCurrentAsync().isCancelled
33-
print("task 3 (cancelled: \(c))")
34-
return 3
35-
}
36-
37-
guard let got = try! await group.next() else {
38-
print("task group failed to get 3 (:\(#line))")
39-
return 0
40-
}
20+
do {
21+
while let r = try await group.next() {
22+
print("next: \(r)")
23+
}
24+
} catch {
25+
print("error caught in group: \(error)")
4126

42-
print("task group next: \(got)")
27+
let gc = group.isCancelled
28+
print("group cancelled: \(gc)")
4329

44-
if got == 1 {
45-
// the previous 1 completed before the 3 we just submitted,
46-
// we still want to see that three so let's await for it
47-
guard let third = try! await group.next() else {
48-
print("task group failed to get 3 (:\(#line))")
49-
return got
50-
}
30+
await group.add { () async -> Int in
31+
let c = await Task.__unsafeCurrentAsync().isCancelled
32+
print("task 3 (cancelled: \(c))")
33+
return 3
34+
}
5135

52-
print("task group returning normally: \(third)")
53-
return third
54-
} else {
55-
print("task group returning normally: \(got)")
56-
return got
57-
}
36+
guard let third = try! await group.next() else {
37+
print("task group failed to get 3")
38+
return 0
5839
}
5940

60-
fatalError("Should have thrown and handled inside the catch block")
41+
print("task group returning normally: \(third)")
42+
return third
6143
}
6244

63-
// Optionally, we may get the first result back before the failure:
64-
// COM: next: 1
65-
// CHECK: error caught in group: Boom()
66-
// CHECK: task 3 (cancelled: false)
67-
// CHECK: task group returning normally: 3
68-
// CHECK: got: 3
69-
70-
print("got: \(got)")
71-
} catch {
72-
print("rethrown: \(error)")
73-
fatalError("Expected recovered result, but got error: \(error)")
45+
fatalError("Should have thrown and handled inside the catch block")
7446
}
47+
48+
// Optionally, we may get the first result back before the failure:
49+
// CHECK: error caught in group: Boom()
50+
// CHECK: group cancelled: false
51+
// CHECK: task 3 (cancelled: false)
52+
// CHECK: task group returning normally: 3
53+
// CHECK: got: 3
54+
55+
print("got: \(got)")
7556
}
7657

7758

0 commit comments

Comments
 (0)