forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestThread.swift
154 lines (132 loc) · 5.15 KB
/
TestThread.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
#if !(os(macOS) || os(iOS) || os(watchOS) || os(tvOS))
import CoreFoundation
#endif
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
#if canImport(SwiftFoundation) && !DEPLOYMENT_RUNTIME_OBJC
@testable import SwiftFoundation
#else
@testable import Foundation
#endif
#endif
class TestThread : XCTestCase {
static var allTests: [(String, (TestThread) -> () throws -> Void)] {
var tests: [(String, (TestThread) -> () throws -> Void)] = [
("test_currentThread", test_currentThread),
("test_threadStart", test_threadStart),
("test_mainThread", test_mainThread),
("test_callStackSymbols", testExpectedToFailOnAndroid(test_callStackSymbols, "Android doesn't support backtraces at the moment.")),
("test_callStackReturnAddresses", testExpectedToFailOnAndroid(test_callStackReturnAddresses, "Android doesn't support backtraces at the moment.")),
]
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
tests.append(contentsOf: [
("test_threadName", test_threadName),
])
#endif
return tests
}
func test_currentThread() {
let thread1 = Thread.current
let thread2 = Thread.current
XCTAssertEqual(thread1, thread2)
XCTAssertEqual(thread1, Thread.mainThread)
}
func test_threadStart() {
let condition = NSCondition()
condition.lock()
let thread = Thread() {
condition.lock()
condition.broadcast()
condition.unlock()
}
XCTAssertEqual(thread.qualityOfService, .default)
thread.start()
let ok = condition.wait(until: Date(timeIntervalSinceNow: 2))
condition.unlock()
XCTAssertTrue(ok, "NSCondition wait timed out")
}
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
func test_threadName() {
#if os(Linux) || os(Android) // Linux sets the initial thread name to the process name.
XCTAssertEqual(Thread.current.name, "TestFoundation")
XCTAssertEqual(Thread.current._name, "TestFoundation")
#else
// No name is set initially
XCTAssertEqual(Thread.current.name, "")
XCTAssertEqual(Thread.current._name, "")
#endif
Thread.current.name = "mainThread"
XCTAssertEqual(Thread.mainThread.name, "mainThread")
XCTAssertEqual(Thread.mainThread._name, "mainThread")
let condition = NSCondition()
condition.lock()
let thread2 = Thread() {
XCTAssertEqual(Thread.current.name, "Thread2-1")
Thread.current.name = "Thread2-2"
XCTAssertEqual(Thread.current.name, "Thread2-2")
XCTAssertEqual(Thread.current._name, Thread.current.name)
Thread.current.name = "12345678901234567890"
XCTAssertEqual(Thread.current.name, "12345678901234567890")
#if os(macOS) || os(iOS)
XCTAssertEqual(Thread.current._name, Thread.current.name)
#elseif os(Linux) || os(Android)
// pthread_setname_np() only allows 15 characters on Linux, so setting it fails
// and the previous name will still be there.
XCTAssertEqual(Thread.current._name, "Thread2-2")
#endif
condition.lock()
condition.signal()
condition.unlock()
}
thread2.name = "Thread2-1"
thread2.start()
// Allow 1 second for thread2 to finish
XCTAssertTrue(condition.wait(until: Date(timeIntervalSinceNow: 1)))
condition.unlock()
XCTAssertEqual(Thread.current.name, "mainThread")
XCTAssertEqual(Thread.mainThread.name, "mainThread")
let thread3 = Thread()
thread3.name = "Thread3"
XCTAssertEqual(thread3.name, "Thread3")
}
#endif
func test_mainThread() {
XCTAssertTrue(Thread.isMainThread)
let t = Thread.mainThread
XCTAssertTrue(t.isMainThread)
let c = Thread.current
XCTAssertTrue(c.isMainThread)
XCTAssertTrue(c.isExecuting)
XCTAssertTrue(c.isEqual(t))
let condition = NSCondition()
condition.lock()
let thread = Thread() {
condition.lock()
XCTAssertFalse(Thread.isMainThread)
XCTAssertFalse(Thread.mainThread == Thread.current)
condition.broadcast()
condition.unlock()
}
thread.start()
let ok = condition.wait(until: Date(timeIntervalSinceNow: 10))
condition.unlock()
XCTAssertTrue(ok, "NSCondition wait timed out")
}
func test_callStackSymbols() {
let symbols = Thread.callStackSymbols
XCTAssertTrue(symbols.count > 0)
XCTAssertTrue(symbols.count <= 128)
}
func test_callStackReturnAddresses() {
let addresses = Thread.callStackReturnAddresses
XCTAssertTrue(addresses.count > 0)
XCTAssertTrue(addresses.count <= 128)
}
}