Skip to content

Commit 8271054

Browse files
authored
Merge pull request #1101 from bubski/operation-queue-current
2 parents 0b206d1 + ea12517 commit 8271054

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

Foundation/Operation.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,13 @@ open class OperationQueue: NSObject {
317317
let lock = NSLock()
318318
#if DEPLOYMENT_ENABLE_LIBDISPATCH
319319
var __concurrencyGate: DispatchSemaphore?
320-
var __underlyingQueue: DispatchQueue?
320+
var __underlyingQueue: DispatchQueue? {
321+
didSet {
322+
let key = OperationQueue.OperationQueueKey
323+
oldValue?.setSpecific(key: key, value: nil)
324+
__underlyingQueue?.setSpecific(key: key, value: Unmanaged.passUnretained(self))
325+
}
326+
}
321327
let queueGroup = DispatchGroup()
322328
#endif
323329

TestFoundation/TestNSOperationQueue.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class TestNSOperationQueue : XCTestCase {
2626
("test_AsyncOperation", test_AsyncOperation),
2727
("test_isExecutingWorks", test_isExecutingWorks),
2828
("test_MainQueueGetter", test_MainQueueGetter),
29+
("test_CurrentQueueOnMainQueue", test_CurrentQueueOnMainQueue),
30+
("test_CurrentQueueOnBackgroundQueue", test_CurrentQueueOnBackgroundQueue),
31+
("test_CurrentQueueWithCustomUnderlyingQueue", test_CurrentQueueWithCustomUnderlyingQueue),
32+
("test_CurrentQueueWithUnderlyingQueueResetToNil", test_CurrentQueueWithUnderlyingQueueResetToNil),
2933
]
3034
}
3135

@@ -114,6 +118,51 @@ class TestNSOperationQueue : XCTestCase {
114118
*/
115119
XCTAssertFalse(OperationQueue.main.isSuspended)
116120
}
121+
122+
func test_CurrentQueueOnMainQueue() {
123+
XCTAssertTrue(OperationQueue.main === OperationQueue.current)
124+
}
125+
126+
func test_CurrentQueueOnBackgroundQueue() {
127+
let expectation = self.expectation(description: "Background execution")
128+
129+
let operationQueue = OperationQueue()
130+
operationQueue.addOperation {
131+
XCTAssertEqual(operationQueue, OperationQueue.current)
132+
expectation.fulfill()
133+
}
134+
135+
waitForExpectations(timeout: 1)
136+
}
137+
138+
func test_CurrentQueueWithCustomUnderlyingQueue() {
139+
let expectation = self.expectation(description: "Background execution")
140+
141+
let operationQueue = OperationQueue()
142+
operationQueue.underlyingQueue = DispatchQueue(label: "underlying_queue")
143+
144+
operationQueue.addOperation {
145+
XCTAssertEqual(operationQueue, OperationQueue.current)
146+
expectation.fulfill()
147+
}
148+
149+
waitForExpectations(timeout: 1)
150+
}
151+
152+
func test_CurrentQueueWithUnderlyingQueueResetToNil() {
153+
let expectation = self.expectation(description: "Background execution")
154+
155+
let operationQueue = OperationQueue()
156+
operationQueue.underlyingQueue = DispatchQueue(label: "underlying_queue")
157+
operationQueue.underlyingQueue = nil
158+
159+
operationQueue.addOperation {
160+
XCTAssertEqual(operationQueue, OperationQueue.current)
161+
expectation.fulfill()
162+
}
163+
164+
waitForExpectations(timeout: 1)
165+
}
117166
}
118167

119168
class AsyncOperation: Operation {

0 commit comments

Comments
 (0)