Skip to content

Aligned Operation cancel behavior with Darwin Foundation #1229

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
Oct 5, 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
22 changes: 14 additions & 8 deletions Foundation/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ open class Operation : NSObject {
}

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

Expand Down Expand Up @@ -79,9 +81,13 @@ open class Operation : NSObject {
}

open func cancel() {
// Note that calling cancel() is advisory. It is up to the main() function to
// call isCancelled at appropriate points in its execution flow and to do the
// actual canceling work. Eventually main() will invoke finish() and this is
// where we then leave the groups and unblock other operations that might
// depend on us.
lock.lock()
_cancelled = true
_leaveGroups()
lock.unlock()
}

Expand Down
15 changes: 15 additions & 0 deletions TestFoundation/TestOperationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TestOperationQueue : XCTestCase {
("test_MainQueueGetter", test_MainQueueGetter),
("test_CurrentQueueOnMainQueue", test_CurrentQueueOnMainQueue),
("test_CurrentQueueOnBackgroundQueue", test_CurrentQueueOnBackgroundQueue),
("test_CurrentQueueOnBackgroundQueueWithSelfCancel", test_CurrentQueueOnBackgroundQueueWithSelfCancel),
("test_CurrentQueueWithCustomUnderlyingQueue", test_CurrentQueueWithCustomUnderlyingQueue),
("test_CurrentQueueWithUnderlyingQueueResetToNil", test_CurrentQueueWithUnderlyingQueueResetToNil),
]
Expand Down Expand Up @@ -135,6 +136,20 @@ class TestOperationQueue : XCTestCase {
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")

Expand Down