Skip to content

Commit ad8a08b

Browse files
committed
NSOperationQueue.operationCount always return 0
correction to dequeue method
1 parent d2dc9f3 commit ad8a08b

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

Foundation/NSOperation.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ internal struct _OperationList {
204204
var all = [NSOperation]()
205205

206206
mutating func insert(_ operation: NSOperation) {
207+
all.append(operation)
207208
switch operation.queuePriority {
208209
case .VeryLow:
209210
veryLow.append(operation)
@@ -257,22 +258,24 @@ internal struct _OperationList {
257258
}
258259

259260
mutating func dequeue() -> NSOperation? {
261+
var result : NSOperation?
260262
if veryHigh.count > 0 {
261-
return veryHigh.remove(at: 0)
263+
result = veryHigh.remove(at: 0)
264+
} else if high.count > 0 {
265+
result = high.remove(at: 0)
266+
} else if normal.count > 0 {
267+
result = normal.remove(at: 0)
268+
} else if low.count > 0 {
269+
result = low.remove(at: 0)
270+
} else if veryLow.count > 0 {
271+
result = veryLow.remove(at: 0)
262272
}
263-
if high.count > 0 {
264-
return high.remove(at: 0)
265-
}
266-
if normal.count > 0 {
267-
return normal.remove(at: 0)
268-
}
269-
if low.count > 0 {
270-
return low.remove(at: 0)
271-
}
272-
if veryLow.count > 0 {
273-
return veryLow.remove(at: 0)
273+
274+
if let idx = all.index(of: result!) {
275+
all.remove(at: idx)
274276
}
275-
return nil
277+
278+
return result
276279
}
277280

278281
var count: Int {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
12+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
13+
import Foundation
14+
import XCTest
15+
#else
16+
import SwiftFoundation
17+
import SwiftXCTest
18+
#endif
19+
20+
class TestNSOperationQueue : XCTestCase {
21+
static var allTests: [(String, TestNSOperationQueue -> () throws -> Void)] {
22+
return [
23+
("test_OperationCount", test_OperationCount)
24+
]
25+
}
26+
27+
func test_OperationCount() {
28+
let queue = NSOperationQueue()
29+
let op1 = NSBlockOperation(block: { sleep(2) })
30+
queue.addOperation(op1)
31+
XCTAssertTrue(queue.operationCount == 1)
32+
/* uncomment below lines once Dispatch is enabled in Foundation */
33+
//queue.waitUntilAllOperationsAreFinished()
34+
//XCTAssertTrue(queue.operationCount == 0)
35+
}
36+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ XCTMain([
4646
testCase(TestNSNull.allTests),
4747
testCase(TestNSNumber.allTests),
4848
testCase(TestNSNumberFormatter.allTests),
49+
testCase(TestNSOperationQueue.allTests),
4950
testCase(TestNSOrderedSet.allTests),
5051
testCase(TestNSPipe.allTests),
5152
testCase(TestNSPredicate.allTests),

0 commit comments

Comments
 (0)