File tree Expand file tree Collapse file tree 1 file changed +4
-13
lines changed Expand file tree Collapse file tree 1 file changed +4
-13
lines changed Original file line number Diff line number Diff line change 9
9
import Dispatch
10
10
11
11
func completeSlowly( n: Int ) async -> Int {
12
- await Task . sleep ( UInt64 ( ( n * 1_000_000_000 ) + 1_000_000_000 ) )
13
- print ( " complete group.spawn { \( n) } " )
12
+ try ? await Task . sleep ( for: . milliseconds( n * 300 ) )
14
13
return n
15
14
}
16
15
17
16
/// Tasks complete AFTER they are next() polled.
18
17
func test_sum_nextOnPending( ) async {
19
18
let numbers = [ 1 , 2 , 3 ]
20
- let expected = 6 // FIXME: numbers.reduce(0, +) this hangs?
19
+ let expected = 6
21
20
22
21
let sum = try ! await withTaskGroup ( of: Int . self) { ( group) async -> Int in
23
22
for n in numbers {
24
- group. spawn {
25
- let res = await completeSlowly ( n: n)
26
- return res
23
+ group. addTask {
24
+ await completeSlowly ( n: n)
27
25
}
28
26
}
29
27
@@ -42,13 +40,6 @@ func test_sum_nextOnPending() async {
42
40
43
41
// The completions are set apart by n seconds, so we expect them to arrive
44
42
// in the order as the numbers (and delays) would suggest:
45
- //
46
- // CHECK: complete group.spawn { 1 }
47
- // CHECK: next: 1
48
- // CHECK: complete group.spawn { 2 }
49
- // CHECK: next: 2
50
- // CHECK: complete group.spawn { 3 }
51
- // CHECK: next: 3
52
43
53
44
// CHECK: task group returning: 6
54
45
You can’t perform that action at this time.
0 commit comments