Skip to content

Commit c481749

Browse files
committed
runtime regression test for async actor init
1 parent 336ff2d commit c481749

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN: %target-run-simple-swift(%import-libdispatch -parse-as-library) | %FileCheck %s
2+
3+
// REQUIRES: executable_test
4+
// REQUIRES: concurrency
5+
// REQUIRES: libdispatch
6+
7+
@available(SwiftStdlib 5.5, *)
8+
actor Number {
9+
var val: Int
10+
var task: Task<Void, Never>?
11+
12+
func increment() {
13+
print("did increment")
14+
val += 1
15+
}
16+
17+
func fib(n: Int) -> Int {
18+
if n < 2 {
19+
return n
20+
}
21+
return fib(n: n-1) + fib(n: n-2)
22+
}
23+
24+
init() async {
25+
val = 0
26+
27+
task = Task.detached(priority: .high) { await self.increment() }
28+
29+
// do some synchronous work
30+
let ans = fib(n: 37)
31+
guard ans == 24157817 else {
32+
fatalError("got ans = \(ans). miscomputation?")
33+
}
34+
35+
// make sure task didn't modify me!
36+
guard val == 0 else {
37+
fatalError("race!")
38+
}
39+
40+
print("finished init()")
41+
}
42+
43+
init(iterations: Int) async {
44+
var iter = iterations
45+
repeat {
46+
val = iter
47+
iter -= 1
48+
} while iter > 0
49+
}
50+
}
51+
52+
@available(SwiftStdlib 5.5, *)
53+
@main struct Main {
54+
static func main() async {
55+
56+
// CHECK: finished init()
57+
// CHECK-NEXT: did increment
58+
59+
let n1 = await Number()
60+
await n1.task!.value
61+
62+
let n2 = await Number(iterations: 1000)
63+
let val = await n2.val
64+
guard val == 1 else {
65+
fatalError("wrong val setting! got \(val)")
66+
}
67+
}
68+
}
69+

0 commit comments

Comments
 (0)