Skip to content

Commit 6cbc4bb

Browse files
authored
Merge pull request #2181 from drodriguez/android-enable-thread-tests
[android] Enable Thread tests were possible.
2 parents 2d0b229 + 138e586 commit 6cbc4bb

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
#endif
3838

39+
#if TARGET_OS_ANDROID
40+
#include <sys/prctl.h>
41+
#endif
42+
3943
#if TARGET_OS_MAC || TARGET_OS_WIN32
4044
#define kCFPlatformInterfaceStringEncoding kCFStringEncodingUTF8
4145
#else
@@ -1495,6 +1499,17 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
14951499
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
14961500
#if TARGET_OS_MAC
14971501
return pthread_getname_np(pthread_self(), buf, length);
1502+
#elif TARGET_OS_ANDROID
1503+
// Android did not get pthread_getname_np until API 26, but prctl seems to
1504+
// return at most 15 chars of the name + null terminator.
1505+
char *buffer[16] = {0};
1506+
if (prctl(PR_GET_NAME, buffer, 0, 0, 0) != 0) {
1507+
return -1;
1508+
}
1509+
size_t sz = MIN(strnlen(buffer, 15), length - 1);
1510+
memcpy(buf, buffer, sz);
1511+
buf[sz] = 0;
1512+
return 0;
14981513
#elif TARGET_OS_LINUX
14991514
return pthread_getname_np(pthread_self(), buf, length);
15001515
#endif
@@ -1532,4 +1547,3 @@ CF_CROSS_PLATFORM_EXPORT void *_CFReallocf(void *ptr, size_t size) {
15321547
}
15331548

15341549
#endif
1535-

TestFoundation/TestThread.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@
2121

2222
class TestThread : XCTestCase {
2323
static var allTests: [(String, (TestThread) -> () throws -> Void)] {
24-
#if os(Android)
25-
return []
26-
#endif
27-
2824
var tests: [(String, (TestThread) -> () throws -> Void)] = [
29-
("test_currentThread", test_currentThread ),
25+
("test_currentThread", test_currentThread),
3026
("test_threadStart", test_threadStart),
3127
("test_mainThread", test_mainThread),
32-
("test_callStackSymbols", test_callStackSymbols),
33-
("test_callStackReurnAddresses", test_callStackReturnAddresses),
3428
]
3529

30+
#if !os(Android)
31+
// Android doesn't support backtraces at the moment.
32+
tests.append(contentsOf: [
33+
("test_callStackSymbols", test_callStackSymbols),
34+
("test_callStackReturnAddresses", test_callStackReturnAddresses),
35+
])
36+
#endif
37+
3638
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
3739
tests.append(contentsOf: [
3840
("test_threadName", test_threadName),
@@ -68,7 +70,7 @@ class TestThread : XCTestCase {
6870

6971
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
7072
func test_threadName() {
71-
#if os(Linux) // Linux sets the initial thread name to the process name.
73+
#if os(Linux) || os(Android) // Linux sets the initial thread name to the process name.
7274
XCTAssertEqual(Thread.current.name, "TestFoundation")
7375
XCTAssertEqual(Thread.current._name, "TestFoundation")
7476
#else
@@ -94,7 +96,7 @@ class TestThread : XCTestCase {
9496
XCTAssertEqual(Thread.current.name, "12345678901234567890")
9597
#if os(macOS) || os(iOS)
9698
XCTAssertEqual(Thread.current._name, Thread.current.name)
97-
#elseif os(Linux)
99+
#elseif os(Linux) || os(Android)
98100
// pthread_setname_np() only allows 15 characters on Linux, so setting it fails
99101
// and the previous name will still be there.
100102
XCTAssertEqual(Thread.current._name, "Thread2-2")

0 commit comments

Comments
 (0)