Skip to content

Commit 842cc9c

Browse files
authored
Merge pull request swiftlang#35449 from kavon/mainactor-testing
2 parents 87c6867 + 5a31eea commit 842cc9c

File tree

1 file changed

+25
-43
lines changed

1 file changed

+25
-43
lines changed
Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency) | %FileCheck %s
1+
// RUN: %target-run-simple-swift(-parse-as-library -Xfrontend -enable-experimental-concurrency) | %FileCheck %s
22

33
// REQUIRES: executable_test
44
// REQUIRES: concurrency
55

66
// REQUIRES: OS=macosx || OS=ios
77
// FIXME: should not require Darwin to run this test once we have async main!
88

9-
// for exit(:Int)
10-
#if canImport(Darwin)
11-
import Darwin
12-
#elseif canImport(Glibc)
13-
import Glibc
14-
#endif
15-
169
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1710
import Dispatch
1811
#endif
@@ -34,52 +27,49 @@ func checkIfMainQueue(expectedAnswer expected: Bool) -> Bool {
3427
}
3528

3629
actor class A {
37-
func onCorrectQueue() -> Bool {
30+
func onCorrectQueue(_ count : Int) -> Int {
3831
if checkIfMainQueue(expectedAnswer: false) {
3932
print("on actor instance's queue")
40-
return true
33+
return count + 1
4134
}
4235
print("ERROR: not on actor instance's queue")
43-
return false
36+
return -10
4437
}
4538
}
4639

47-
@MainActor func exitTest(success: Bool) -> Never {
48-
if !success {
49-
exit(EXIT_FAILURE)
50-
}
51-
40+
@MainActor func checkAnotherFn(_ count : Int) -> Int {
5241
if checkIfMainQueue(expectedAnswer: true) {
5342
print("on main queue again!")
43+
return count + 1
5444
} else {
5545
print("ERROR: left the main queue?")
46+
return -10
5647
}
57-
58-
exit(EXIT_SUCCESS)
5948
}
6049

61-
@MainActor func enterMainActor() async -> Never {
62-
var ok = checkIfMainQueue(expectedAnswer: true)
63-
if ok {
50+
@MainActor func enterMainActor(_ initialCount : Int) async -> Int {
51+
if checkIfMainQueue(expectedAnswer: true) {
6452
print("hello from main actor!")
6553
} else {
6654
print("ERROR: not on correct queue!")
6755
}
6856

6957
// try calling a function on another actor.
70-
let someActor = A()
71-
let successfulActorSwitch = await someActor.onCorrectQueue()
72-
ok = ok && successfulActorSwitch
58+
let count = await A().onCorrectQueue(initialCount)
7359

74-
exitTest(success: ok)
60+
guard checkIfMainQueue(expectedAnswer: true) else {
61+
print("ERROR: did not switch back to main actor!")
62+
return -10
63+
}
64+
65+
return checkAnotherFn(count) + 1
7566
}
7667

77-
@concurrent func someFunc() async {
78-
guard checkIfMainQueue(expectedAnswer: false) else {
79-
print("ERROR: did not expect detatched task to run on main queue!")
80-
exit(EXIT_FAILURE)
81-
}
82-
await enterMainActor()
68+
@concurrent func someFunc() async -> Int {
69+
// NOTE: the "return" counter is just to make sure we're properly returning values.
70+
// the expected number should be equal to the number of "plus-one" expressions.
71+
// since there are no loops or duplicate function calls
72+
return await enterMainActor(0) + 1
8373
}
8474

8575

@@ -90,21 +80,13 @@ actor class A {
9080
// CHECK: on actor instance's queue
9181
// CHECK-NOT: ERROR
9282
// CHECK: on main queue again!
83+
// CHECK-NOT: ERROR
84+
// CHECK: finished with return counter = 4
9385

94-
import CoreFoundation
95-
96-
print("starting")
97-
Task.runDetached(operation: someFunc)
98-
CFRunLoopRun()
99-
100-
// FIXME: remove the use of CFRunLoopRun and the CoreFoundation import
101-
// in favor of the below once we have async main support.
102-
// don't forget to add -parse-as-library to the RUN line
103-
/*
10486
@main struct RunIt {
10587
static func main() async {
10688
print("starting")
107-
await someFunc()
89+
let result = await someFunc()
90+
print("finished with return counter = \(result)")
10891
}
10992
}
110-
*/

0 commit comments

Comments
 (0)