forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestOperationQueue.swift
332 lines (281 loc) · 11.2 KB
/
TestOperationQueue.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// 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
//
import Dispatch
class TestOperationQueue : XCTestCase {
static var allTests: [(String, (TestOperationQueue) -> () throws -> Void)] {
return [
("test_OperationPriorities", test_OperationPriorities),
("test_OperationCount", test_OperationCount),
("test_AsyncOperation", test_AsyncOperation),
("test_isExecutingWorks", test_isExecutingWorks),
("test_MainQueueGetter", test_MainQueueGetter),
("test_CancelOneOperation", test_CancelOneOperation),
("test_CancelOperationsOfSpecificQueuePriority", test_CancelOperationsOfSpecificQueuePriority),
("test_CurrentQueueOnMainQueue", test_CurrentQueueOnMainQueue),
("test_CurrentQueueOnBackgroundQueue", test_CurrentQueueOnBackgroundQueue),
("test_CurrentQueueOnBackgroundQueueWithSelfCancel", test_CurrentQueueOnBackgroundQueueWithSelfCancel),
("test_CurrentQueueWithCustomUnderlyingQueue", test_CurrentQueueWithCustomUnderlyingQueue),
("test_CurrentQueueWithUnderlyingQueueResetToNil", test_CurrentQueueWithUnderlyingQueueResetToNil),
("test_isSuspended", test_isSuspended),
]
}
func test_OperationCount() {
let queue = OperationQueue()
let op1 = BlockOperation(block: { Thread.sleep(forTimeInterval: 2) })
queue.addOperation(op1)
XCTAssertTrue(queue.operationCount == 1)
queue.waitUntilAllOperationsAreFinished()
XCTAssertTrue(queue.operationCount == 0)
}
func test_OperationPriorities() {
var msgOperations = [String]()
let operation1 : BlockOperation = BlockOperation(block: {
msgOperations.append("Operation1 executed")
})
let operation2 : BlockOperation = BlockOperation(block: {
msgOperations.append("Operation2 executed")
})
let operation3 : BlockOperation = BlockOperation(block: {
msgOperations.append("Operation3 executed")
})
let operation4: BlockOperation = BlockOperation(block: {
msgOperations.append("Operation4 executed")
})
operation4.queuePriority = .veryLow
operation3.queuePriority = .veryHigh
operation2.queuePriority = .low
operation1.queuePriority = .normal
var operations = [Operation]()
operations.append(operation1)
operations.append(operation2)
operations.append(operation3)
operations.append(operation4)
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
queue.addOperations(operations, waitUntilFinished: true)
XCTAssertEqual(msgOperations[0], "Operation3 executed")
XCTAssertEqual(msgOperations[1], "Operation1 executed")
XCTAssertEqual(msgOperations[2], "Operation2 executed")
XCTAssertEqual(msgOperations[3], "Operation4 executed")
}
func test_isExecutingWorks() {
class _OperationBox {
var operation: Operation?
init() {
self.operation = nil
}
}
let queue = OperationQueue()
let opBox = _OperationBox()
let op = BlockOperation(block: { XCTAssertEqual(true, opBox.operation?.isExecuting) })
opBox.operation = op
XCTAssertFalse(op.isExecuting)
queue.addOperation(op)
queue.waitUntilAllOperationsAreFinished()
XCTAssertFalse(op.isExecuting)
opBox.operation = nil /* break the reference cycle op -> <closure> -> opBox -> op */
}
func test_AsyncOperation() {
let operation = AsyncOperation()
XCTAssertFalse(operation.isExecuting)
XCTAssertFalse(operation.isFinished)
operation.start()
while !operation.isFinished {
// do nothing
}
XCTAssertFalse(operation.isExecuting)
XCTAssertTrue(operation.isFinished)
}
func test_MainQueueGetter() {
XCTAssertTrue(OperationQueue.main === OperationQueue.main)
/*
This call is only to check if OperationQueue.main returns a living instance.
There used to be a bug where subsequent OperationQueue.main call would return a "dangling pointer".
*/
XCTAssertFalse(OperationQueue.main.isSuspended)
}
func test_CancelOneOperation() {
var operations = [Operation]()
var valueOperations = [Int]()
for i in 0..<5 {
let operation = BlockOperation {
valueOperations.append(i)
Thread.sleep(forTimeInterval: 2)
}
operations.append(operation)
}
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
queue.addOperations(operations, waitUntilFinished: false)
operations.remove(at: 2).cancel()
queue.waitUntilAllOperationsAreFinished()
XCTAssertTrue(!valueOperations.contains(2))
}
func test_CancelOperationsOfSpecificQueuePriority() {
var operations = [Operation]()
var valueOperations = [Int]()
let operation1 = BlockOperation {
valueOperations.append(0)
Thread.sleep(forTimeInterval: 2)
}
operation1.queuePriority = .high
operations.append(operation1)
let operation2 = BlockOperation {
valueOperations.append(1)
Thread.sleep(forTimeInterval: 2)
}
operation2.queuePriority = .high
operations.append(operation2)
let operation3 = BlockOperation {
valueOperations.append(2)
}
operation3.queuePriority = .normal
operations.append(operation3)
let operation4 = BlockOperation {
valueOperations.append(3)
}
operation4.queuePriority = .normal
operations.append(operation4)
let operation5 = BlockOperation {
valueOperations.append(4)
}
operation5.queuePriority = .normal
operations.append(operation5)
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
queue.addOperations(operations, waitUntilFinished: false)
for operation in operations {
if operation.queuePriority == .normal {
operation.cancel()
}
}
queue.waitUntilAllOperationsAreFinished()
XCTAssertTrue(valueOperations.count == 2)
XCTAssertTrue(valueOperations[0] == 0)
XCTAssertTrue(valueOperations[1] == 1)
}
func test_CurrentQueueOnMainQueue() {
XCTAssertTrue(OperationQueue.main === OperationQueue.current)
}
func test_CurrentQueueOnBackgroundQueue() {
let expectation = self.expectation(description: "Background execution")
let operationQueue = OperationQueue()
operationQueue.addOperation {
XCTAssertEqual(operationQueue, OperationQueue.current)
expectation.fulfill()
}
waitForExpectations(timeout: 1)
}
func test_CurrentQueueOnBackgroundQueueWithSelfCancel() {
let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1
let expectation = self.expectation(description: "Background execution")
operationQueue.addOperation {
XCTAssertEqual(operationQueue, OperationQueue.current)
expectation.fulfill()
// Canceling operation X from inside operation X should not cause the app to a crash
operationQueue.cancelAllOperations()
}
waitForExpectations(timeout: 1)
}
func test_CurrentQueueWithCustomUnderlyingQueue() {
let expectation = self.expectation(description: "Background execution")
let operationQueue = OperationQueue()
operationQueue.underlyingQueue = DispatchQueue(label: "underlying_queue")
operationQueue.addOperation {
XCTAssertEqual(operationQueue, OperationQueue.current)
expectation.fulfill()
}
waitForExpectations(timeout: 1)
}
func test_isSuspended() {
let expectation1 = self.expectation(description: "DispatchQueue execution")
let expectation2 = self.expectation(description: "OperationQueue execution")
let dispatchQueue = DispatchQueue(label: "underlying_queue")
let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1
operationQueue.underlyingQueue = dispatchQueue
operationQueue.isSuspended = true
operationQueue.addOperation {
XCTAssert(OperationQueue.current?.underlyingQueue === dispatchQueue)
expectation2.fulfill()
}
dispatchQueue.async {
operationQueue.isSuspended = false
expectation1.fulfill()
}
waitForExpectations(timeout: 1)
}
func test_CurrentQueueWithUnderlyingQueueResetToNil() {
let expectation = self.expectation(description: "Background execution")
let operationQueue = OperationQueue()
operationQueue.underlyingQueue = DispatchQueue(label: "underlying_queue")
operationQueue.underlyingQueue = nil
operationQueue.addOperation {
XCTAssertEqual(operationQueue, OperationQueue.current)
expectation.fulfill()
}
waitForExpectations(timeout: 1)
}
}
class AsyncOperation: Operation {
private let queue = DispatchQueue(label: "async.operation.queue")
private let lock = NSLock()
private var _executing = false
private var _finished = false
override var isExecuting: Bool {
get {
lock.lock()
let wasExecuting = _executing
lock.unlock()
return wasExecuting
}
set {
if isExecuting != newValue {
willChangeValue(forKey: "isExecuting")
lock.lock()
_executing = newValue
lock.unlock()
didChangeValue(forKey: "isExecuting")
}
}
}
override var isFinished: Bool {
get {
lock.lock()
let wasFinished = _finished
lock.unlock()
return wasFinished
}
set {
if isFinished != newValue {
willChangeValue(forKey: "isFinished")
lock.lock()
_finished = newValue
lock.unlock()
didChangeValue(forKey: "isFinished")
}
}
}
override var isAsynchronous: Bool {
return true
}
override func start() {
if isCancelled {
isFinished = true
return
}
isExecuting = true
queue.async {
Thread.sleep(forTimeInterval: 1)
self.isExecuting = false
self.isFinished = true
}
}
}