File tree Expand file tree Collapse file tree 1 file changed +32
-2
lines changed Expand file tree Collapse file tree 1 file changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,37 @@ import XCTest
2
2
import AndroidNative
3
3
4
4
class AndroidNativeTests : XCTestCase {
5
- public func testAndroidNative( ) {
6
- XCTAssertEqual ( 1 + 2 , 3 )
5
+ public func testMainActor( ) async {
6
+ let actorDemo = await MainActorDemo ( )
7
+ let result = await actorDemo. add ( n1: 1 , n2: 2 )
8
+ XCTAssertEqual ( result, 3 )
9
+ var tasks : [ Task < Int , Never > ] = [ ]
10
+
11
+ for i in 0 ..< 100 {
12
+ tasks. append ( Task ( priority: [ . low, . medium, . high] . randomElement ( ) !) {
13
+ //print("### Task: \(Thread.current)")
14
+ assert ( !Thread. isMainThread)
15
+ return await actorDemo. add ( n1: i, n2: i)
16
+ } )
17
+ }
18
+
19
+ var totalResult = 0
20
+ for task in tasks {
21
+ let taskResult = await task. value
22
+ totalResult += taskResult
23
+ }
24
+
25
+ XCTAssertEqual ( 9900 , totalResult)
26
+ }
27
+ }
28
+
29
+ @MainActor class MainActorDemo {
30
+ init ( ) {
31
+ }
32
+
33
+ func add( n1: Int , n2: Int ) -> Int {
34
+ //print("### MainActorDemo: \(Thread.current)")
35
+ assert ( Thread . isMainThread)
36
+ return n1 + n2
7
37
}
8
38
}
You can’t perform that action at this time.
0 commit comments