forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestThread.swift
124 lines (105 loc) · 3.67 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
// 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 DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif
import CoreFoundation
class TestThread : XCTestCase {
static var allTests: [(String, (TestThread) -> () throws -> Void)] {
return [
("test_currentThread", test_currentThread ),
("test_threadStart", test_threadStart),
("test_threadName", test_threadName),
("test_mainThread", test_mainThread),
("test_callStackSymbols", test_callStackSymbols),
("test_callStackReurnAddresses", test_callStackReturnAddresses),
]
}
func test_currentThread() {
let thread1 = Thread.current
let thread2 = Thread.current
XCTAssertNotNil(thread1)
XCTAssertNotNil(thread2)
XCTAssertEqual(thread1, thread2)
XCTAssertEqual(thread1, Thread.mainThread)
}
func test_threadStart() {
let condition = NSCondition()
let thread = Thread() {
condition.lock()
condition.broadcast()
condition.unlock()
}
thread.start()
condition.lock()
let ok = condition.wait(until: Date(timeIntervalSinceNow: 10))
condition.unlock()
XCTAssertTrue(ok, "NSCondition wait timed out")
}
func test_threadName() {
let thread = Thread()
XCTAssertNil(thread.name)
func getPThreadName() -> String? {
var buf = [Int8](repeating: 0, count: 16)
let r = _CFThreadGetName(&buf, Int32(buf.count))
guard r == 0 else {
return nil
}
return String(cString: buf)
}
let thread2 = Thread() {
Thread.current.name = "Thread2"
XCTAssertEqual(Thread.current.name, "Thread2")
XCTAssertEqual(Thread.current.name, getPThreadName())
}
thread2.start()
Thread.current.name = "CurrentThread"
XCTAssertEqual(Thread.current.name, getPThreadName())
let thread3 = Thread()
thread3.name = "Thread3"
XCTAssertEqual(thread3.name, "Thread3")
XCTAssertNotEqual(thread3.name, getPThreadName())
}
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()
let thread = Thread() {
condition.lock()
XCTAssertFalse(Thread.isMainThread)
XCTAssertFalse(Thread.mainThread == Thread.current)
condition.broadcast()
condition.unlock()
}
thread.start()
condition.lock()
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)
}
}