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
2
2
3
3
// REQUIRES: executable_test
4
4
// REQUIRES: concurrency
5
5
6
6
// REQUIRES: OS=macosx || OS=ios
7
7
// FIXME: should not require Darwin to run this test once we have async main!
8
8
9
- // for exit(:Int)
10
- #if canImport(Darwin)
11
- import Darwin
12
- #elseif canImport(Glibc)
13
- import Glibc
14
- #endif
15
-
16
9
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
17
10
import Dispatch
18
11
#endif
@@ -34,52 +27,49 @@ func checkIfMainQueue(expectedAnswer expected: Bool) -> Bool {
34
27
}
35
28
36
29
actor class A {
37
- func onCorrectQueue( ) -> Bool {
30
+ func onCorrectQueue( _ count : Int ) -> Int {
38
31
if checkIfMainQueue ( expectedAnswer: false ) {
39
32
print ( " on actor instance's queue " )
40
- return true
33
+ return count + 1
41
34
}
42
35
print ( " ERROR: not on actor instance's queue " )
43
- return false
36
+ return - 10
44
37
}
45
38
}
46
39
47
- @MainActor func exitTest( success: Bool ) -> Never {
48
- if !success {
49
- exit ( EXIT_FAILURE)
50
- }
51
-
40
+ @MainActor func checkAnotherFn( _ count : Int ) -> Int {
52
41
if checkIfMainQueue ( expectedAnswer: true ) {
53
42
print ( " on main queue again! " )
43
+ return count + 1
54
44
} else {
55
45
print ( " ERROR: left the main queue? " )
46
+ return - 10
56
47
}
57
-
58
- exit ( EXIT_SUCCESS)
59
48
}
60
49
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 ) {
64
52
print ( " hello from main actor! " )
65
53
} else {
66
54
print ( " ERROR: not on correct queue! " )
67
55
}
68
56
69
57
// 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)
73
59
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
75
66
}
76
67
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
83
73
}
84
74
85
75
@@ -90,21 +80,13 @@ actor class A {
90
80
// CHECK: on actor instance's queue
91
81
// CHECK-NOT: ERROR
92
82
// CHECK: on main queue again!
83
+ // CHECK-NOT: ERROR
84
+ // CHECK: finished with return counter = 4
93
85
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
- /*
104
86
@main struct RunIt {
105
87
static func main( ) async {
106
88
print ( " starting " )
107
- await someFunc()
89
+ let result = await someFunc ( )
90
+ print ( " finished with return counter = \( result) " )
108
91
}
109
92
}
110
- */
0 commit comments