Skip to content

Commit d531835

Browse files
committed
[concurrency] add executable test of MainActor
1 parent a2c57d1 commit d531835

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency %import-libdispatch) | %FileCheck %s
2+
3+
// REQUIRES: executable_test
4+
// REQUIRES: concurrency
5+
// REQUIRES: libdispatch
6+
7+
// XFAIL: *
8+
9+
#if canImport(Darwin)
10+
import Darwin
11+
#elseif canImport(Glibc)
12+
import Glibc
13+
#endif
14+
15+
import Foundation
16+
17+
// CHECK: starting
18+
// CHECK-NOT: ERROR
19+
// CHECK: hello from main actor!
20+
// CHECK-NOT: ERROR
21+
// CHECK: ending
22+
23+
@MainActor func helloMainActor() {
24+
if Thread.isMainThread {
25+
print("hello from main actor!")
26+
} else {
27+
print("ERROR: not on correct thread!")
28+
}
29+
}
30+
31+
func someFunc() async {
32+
await helloMainActor()
33+
}
34+
35+
runAsyncAndBlock {
36+
print("starting")
37+
let handle = Task.runDetached(operation: someFunc)
38+
do {
39+
try await handle.get()
40+
} catch {
41+
print("ERROR: exception was thrown while waiting for task to complete")
42+
}
43+
print("ending")
44+
}

0 commit comments

Comments
 (0)