Skip to content

Commit edd62b4

Browse files
committed
TestFoundation: add SPI for querying the thread name
This keeps the CoreFoundation interfaces confined to the Foundation implementation and outside of the test suite. This is needed to enable tests on Windows.
1 parent abe5c1f commit edd62b4

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

Foundation/Thread.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,22 @@ open class Thread : NSObject {
255255
}
256256
}
257257

258+
internal var _name: String? {
259+
var buf: [Int8] = Array<Int8>(repeating: 0, count: 128)
260+
#if DEPLOYMENT_RUNTIME_OBJC
261+
// Do not use _CF functions on the ObjC runtime as that breaks on the
262+
// Darwin runtime.
263+
if pthread_getname_np(pthread_self(), &buf, buf.count) == 0 {
264+
return ""
265+
}
266+
#else
267+
if _CFThreadGetName(&buf, Int32(buf.count)) == 0 {
268+
return ""
269+
}
270+
#endif
271+
return String(cString: buf)
272+
}
273+
258274
#if os(Windows)
259275
open var stackSize: Int {
260276
get {

TestFoundation/TestThread.swift

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,22 @@ class TestThread : XCTestCase {
1717
#if os(Android)
1818
return []
1919
#endif
20-
return [
20+
21+
var tests: [(String, (TestThread) -> () throws -> Void)] = [
2122
("test_currentThread", test_currentThread ),
2223
("test_threadStart", test_threadStart),
23-
("test_threadName", test_threadName),
2424
("test_mainThread", test_mainThread),
2525
("test_callStackSymbols", test_callStackSymbols),
2626
("test_callStackReurnAddresses", test_callStackReturnAddresses),
2727
]
28+
29+
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
30+
tests.append(contentsOf: [
31+
("test_threadName", test_threadName),
32+
])
33+
#endif
34+
35+
return tests
2836
}
2937

3038
func test_currentThread() {
@@ -50,31 +58,15 @@ class TestThread : XCTestCase {
5058
XCTAssertTrue(ok, "NSCondition wait timed out")
5159
}
5260

61+
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
5362
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-
7163
// No name is set initially
7264
XCTAssertNil(Thread.current.name)
7365

7466
#if os(Linux) // Linux sets the initial thread name to the process name.
75-
compareThreadName(to: "TestFoundation")
67+
XCAssertEqual(Thread.current._name, "TestFoundation")
7668
#else
77-
compareThreadName(to: "")
69+
XCAssertEqual(Thread.current._name, "")
7870
#endif
7971
Thread.current.name = "mainThread"
8072
XCTAssertEqual(Thread.mainThread.name, "mainThread")
@@ -88,16 +80,16 @@ class TestThread : XCTestCase {
8880

8981
Thread.current.name = "Thread2-2"
9082
XCTAssertEqual(Thread.current.name, "Thread2-2")
91-
compareThreadName(to: "Thread2-2")
83+
XCAssertEqual(Thread.current._name, Thread.current.name)
9284

9385
Thread.current.name = "12345678901234567890"
9486
XCTAssertEqual(Thread.current.name, "12345678901234567890")
9587
#if os(macOS) || os(iOS)
96-
compareThreadName(to: "12345678901234567890")
88+
XCAssertEqual(Thread.current._name, Thread.current.name)
9789
#elseif os(Linux)
9890
// pthread_setname_np() only allows 15 characters on Linux, so setting it fails
9991
// and the previous name will still be there.
100-
compareThreadName(to: "Thread2-2")
92+
XCAssertEqual(Thread.current._name, "Thread2-2")
10193
#endif
10294
condition.lock()
10395
condition.signal()
@@ -116,6 +108,7 @@ class TestThread : XCTestCase {
116108
thread3.name = "Thread3"
117109
XCTAssertEqual(thread3.name, "Thread3")
118110
}
111+
#endif
119112

120113
func test_mainThread() {
121114
XCTAssertTrue(Thread.isMainThread)

0 commit comments

Comments
 (0)