forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestNSLock.swift
190 lines (155 loc) · 6.25 KB
/
TestNSLock.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
class TestNSLock: XCTestCase {
static var allTests: [(String, (TestNSLock) -> () throws -> Void)] {
return [
("test_lockWait", test_lockWait),
("test_threadsAndLocks", test_threadsAndLocks),
("test_recursiveLock", test_recursiveLock),
]
}
func test_lockWait() {
let condition = NSCondition()
let lock = NSLock()
func test(waitTime: TimeInterval, shouldLock: Bool) -> Bool {
let locked = lock.lock(before: Date.init(timeIntervalSinceNow: waitTime))
if locked {
lock.unlock()
}
return locked == shouldLock
}
let thread = Thread() {
lock.lock()
// Now wake up the main thread so it can try to obtain the lock that
// this thread just obtained.
condition.lock()
condition.signal()
condition.unlock()
Thread.sleep(forTimeInterval: 8)
lock.unlock()
}
condition.lock()
thread.start()
condition.wait()
condition.unlock()
XCTAssertTrue(test(waitTime: 0, shouldLock: false))
XCTAssertTrue(test(waitTime: -1, shouldLock: false))
XCTAssertTrue(test(waitTime: 1, shouldLock: false))
XCTAssertTrue(test(waitTime: 4, shouldLock: false))
XCTAssertTrue(test(waitTime: 8, shouldLock: true))
XCTAssertTrue(test(waitTime: -1, shouldLock: true))
}
func test_threadsAndLocks() {
let condition = NSCondition()
let lock = NSLock()
let threadCount = 10
let endSeconds: Double = 2
let endTime = Date.init(timeIntervalSinceNow: endSeconds)
var threadsStarted = Array<Bool>(repeating: false, count: threadCount)
let arrayLock = NSLock()
for t in 0..<threadCount {
let thread = Thread() {
condition.lock()
arrayLock.lock()
threadsStarted[t] = true
arrayLock.unlock()
condition.wait()
condition.unlock()
for _ in 1...50 {
let r = Double.random(in: 0...0.02)
Thread.sleep(forTimeInterval: r)
if lock.lock(before: endTime) {
lock.unlock()
}
}
arrayLock.lock()
threadsStarted[t] = false
arrayLock.unlock()
}
thread.start()
}
var totalThreads = 0
repeat {
arrayLock.lock()
totalThreads = threadsStarted.filter {$0 == true }.count
arrayLock.unlock()
} while totalThreads < threadCount
XCTAssertEqual(totalThreads, threadCount)
condition.lock()
condition.broadcast()
condition.unlock()
Thread.sleep(until: endTime)
repeat {
arrayLock.lock()
totalThreads = threadsStarted.filter {$0 == false }.count
arrayLock.unlock()
} while totalThreads < threadCount
XCTAssertEqual(totalThreads, threadCount)
let gotLock = lock.try()
XCTAssertTrue(gotLock)
if gotLock {
lock.unlock()
}
}
func test_recursiveLock() {
// We will start 10 threads, and their collective task will be to reduce
// the countdown value to zero in less than 30 seconds.
let threadCount = 10
let countdownPerThread = 1000
let endTime = Date(timeIntervalSinceNow: 30)
var completedThreadCount = 0
var countdownValue = threadCount * countdownPerThread
let threadCompletedCondition = NSCondition()
let countdownValueLock = NSRecursiveLock()
for _ in 0..<threadCount {
let thread = Thread {
// Some dummy work on countdown.
// Add/substract operations are balanced to decrease countdown
// exactly by countdownPerThread
for _ in 0..<countdownPerThread {
countdownValueLock.lock()
countdownValue -= 3
countdownValueLock.lock()
countdownValue += 4
countdownValueLock.unlock()
countdownValueLock.unlock()
countdownValueLock.lock()
countdownValue += 2
countdownValueLock.lock()
countdownValue -= 1
countdownValueLock.unlock()
countdownValue -= 3
countdownValueLock.unlock()
countdownValueLock.lock()
countdownValue += 1
countdownValueLock.unlock()
countdownValueLock.lock()
countdownValue -= 1
countdownValueLock.unlock()
}
threadCompletedCondition.lock()
completedThreadCount += 1
threadCompletedCondition.broadcast()
threadCompletedCondition.unlock()
}
thread.start()
}
threadCompletedCondition.lock()
var isTimedOut = false
while !isTimedOut && completedThreadCount < threadCount {
isTimedOut = !threadCompletedCondition.wait(until: endTime)
}
countdownValueLock.lock()
let resultCountdownValue = countdownValue
countdownValueLock.unlock()
XCTAssertEqual(completedThreadCount, threadCount, "Some threads are still not finished, could be hung")
XCTAssertEqual(resultCountdownValue, 0, "Wrong coundtdown means unresolved race conditions, locks broken")
threadCompletedCondition.unlock()
}
}