Skip to content

fix Operation.isExecuting #1080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Foundation/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ open class Operation : NSObject {
}

open func start() {
lock.lock()
_executing = true
lock.unlock()
main()
lock.lock()
_executing = false
lock.unlock()
finish()
}

Expand Down Expand Up @@ -85,7 +91,12 @@ open class Operation : NSObject {
}

open var isExecuting: Bool {
return _executing
let wasExecuting: Bool
lock.lock()
wasExecuting = _executing
lock.unlock()

return wasExecuting
}

open var isFinished: Bool {
Expand Down
23 changes: 22 additions & 1 deletion TestFoundation/TestNSOperationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class TestNSOperationQueue : XCTestCase {
return [
("test_OperationPriorities", test_OperationPriorities),
("test_OperationCount", test_OperationCount),
("test_AsyncOperation", test_AsyncOperation)
("test_AsyncOperation", test_AsyncOperation),
("test_isExecutingWorks", test_isExecutingWorks),
]
}

Expand Down Expand Up @@ -68,6 +69,26 @@ class TestNSOperationQueue : XCTestCase {
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)
Expand Down