@@ -17,14 +17,22 @@ class TestThread : XCTestCase {
17
17
#if os(Android)
18
18
return [ ]
19
19
#endif
20
- return [
20
+
21
+ var tests : [ ( String , ( TestThread ) -> ( ) throws -> Void ) ] = [
21
22
( " test_currentThread " , test_currentThread ) ,
22
23
( " test_threadStart " , test_threadStart) ,
23
- ( " test_threadName " , test_threadName) ,
24
24
( " test_mainThread " , test_mainThread) ,
25
25
( " test_callStackSymbols " , test_callStackSymbols) ,
26
26
( " test_callStackReurnAddresses " , test_callStackReturnAddresses) ,
27
27
]
28
+
29
+ #if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
30
+ tests. append ( contentsOf: [
31
+ ( " test_threadName " , test_threadName) ,
32
+ ] )
33
+ #endif
34
+
35
+ return tests
28
36
}
29
37
30
38
func test_currentThread( ) {
@@ -50,31 +58,15 @@ class TestThread : XCTestCase {
50
58
XCTAssertTrue ( ok, " NSCondition wait timed out " )
51
59
}
52
60
61
+ #if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
53
62
func test_threadName( ) {
54
-
55
- // Compare the name set in pthreads()
56
- func compareThreadName( to name: String ) {
57
- var buf = [ Int8] ( repeating: 0 , count: 128 )
58
- #if os(macOS) || os(iOS)
59
- // Don't use _CF functions on macOS as it will break testing with Darwin's native Foundation.
60
- let r = pthread_getname_np ( pthread_self ( ) , & buf, buf. count)
61
- #else
62
- let r = _CFThreadGetName ( & buf, Int32 ( buf. count) )
63
- #endif
64
- if r == 0 {
65
- XCTAssertEqual ( String ( cString: buf) , name)
66
- } else {
67
- XCTFail ( " Cant get thread name " )
68
- }
69
- }
70
-
71
63
// No name is set initially
72
64
XCTAssertNil ( Thread . current. name)
73
65
74
66
#if os(Linux) // Linux sets the initial thread name to the process name.
75
- compareThreadName ( to : " TestFoundation " )
67
+ XCAssertEqual ( Thread . current . _name , " TestFoundation " )
76
68
#else
77
- compareThreadName ( to : " " )
69
+ XCAssertEqual ( Thread . current . _name , " " )
78
70
#endif
79
71
Thread . current. name = " mainThread "
80
72
XCTAssertEqual ( Thread . mainThread. name, " mainThread " )
@@ -88,16 +80,16 @@ class TestThread : XCTestCase {
88
80
89
81
Thread . current. name = " Thread2-2 "
90
82
XCTAssertEqual ( Thread . current. name, " Thread2-2 " )
91
- compareThreadName ( to : " Thread2-2 " )
83
+ XCAssertEqual ( Thread . current . _name , Thread . current . name )
92
84
93
85
Thread . current. name = " 12345678901234567890 "
94
86
XCTAssertEqual ( Thread . current. name, " 12345678901234567890 " )
95
87
#if os(macOS) || os(iOS)
96
- compareThreadName ( to : " 12345678901234567890 " )
88
+ XCAssertEqual ( Thread . current . _name , Thread . current . name )
97
89
#elseif os(Linux)
98
90
// pthread_setname_np() only allows 15 characters on Linux, so setting it fails
99
91
// and the previous name will still be there.
100
- compareThreadName ( to : " Thread2-2 " )
92
+ XCAssertEqual ( Thread . current . _name , " Thread2-2 " )
101
93
#endif
102
94
condition. lock ( )
103
95
condition. signal ( )
@@ -116,6 +108,7 @@ class TestThread : XCTestCase {
116
108
thread3. name = " Thread3 "
117
109
XCTAssertEqual ( thread3. name, " Thread3 " )
118
110
}
111
+ #endif
119
112
120
113
func test_mainThread( ) {
121
114
XCTAssertTrue ( Thread . isMainThread)
0 commit comments