forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestNSLock.swift
118 lines (99 loc) · 3.53 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
// 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),
]
}
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()
}
}
}