Skip to content

Implementation of URLSessionTask.cancel() #689

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
Feb 15, 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
16 changes: 13 additions & 3 deletions Foundation/NSURLSession/NSURLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,18 @@ open class URLSessionTask : NSObject, NSCopying {
* cases, the task may signal other work before it acknowledges the
* cancelation. -cancel may be sent to a task that has been suspended.
*/
open func cancel() { NSUnimplemented() }

open func cancel() {
workQueue.sync {
guard self.state == .running || self.state == .suspended else { return }
self.state = .canceling
self.workQueue.async {
self.internalState = .transferFailed
let urlError = URLError(_nsError: NSError(domain: NSURLErrorDomain, code: NSURLErrorCancelled, userInfo: nil))
self.completeTask(withError: urlError)
}
}
}

/*
* The current state of the task within the session.
*/
Expand Down Expand Up @@ -866,7 +876,7 @@ extension URLSessionTask {
}
func completeTask(withError error: Error) {
self.error = error

guard case .transferFailed = internalState else {
fatalError("Trying to complete the task, but its transfer isn't complete / failed.")
}
Expand Down
39 changes: 34 additions & 5 deletions TestFoundation/TestNSURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TestURLSession : XCTestCase {
("test_finishTaskAndInvalidate", test_finishTasksAndInvalidate),
("test_taskError", test_taskError),
("test_taskCopy", test_taskCopy),
("test_cancelTask", test_cancelTask),
]
}

Expand Down Expand Up @@ -297,6 +298,26 @@ class TestURLSession : XCTestCase {

XCTAssert(task.isEqual(task.copy()))
}

func test_cancelTask() {
let serverReady = ServerSemaphore()
globalDispatchQueue.async {
do {
try self.runServer(with: serverReady)
} catch {
XCTAssertTrue(true)
return
}
}
serverReady.wait()
let url = URL(string: "http://127.0.0.1:\(serverPort)/Peru")!
let d = DataTask(with: expectation(description: "Task to be canceled"))
d.cancelExpectation = expectation(description: "URLSessionTask wasn't canceled")
d.run(with: url)
d.cancel()
waitForExpectations(timeout: 12)
}

}

class SessionDelegate: NSObject, URLSessionDelegate {
Expand All @@ -314,6 +335,8 @@ class DataTask : NSObject {
var capital = "unknown"
var session: URLSession! = nil
var task: URLSessionDataTask! = nil
var cancelExpectation: XCTestExpectation?

public var error = false

init(with expectation: XCTestExpectation) {
Expand All @@ -335,20 +358,26 @@ class DataTask : NSObject {
task = session.dataTask(with: url)
task.resume()
}

func cancel() {
task.cancel()
}
}

extension DataTask : URLSessionDataDelegate {
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
capital = String(data: data, encoding: String.Encoding.utf8)!
dataTaskExpectation.fulfill()
}
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
capital = String(data: data, encoding: String.Encoding.utf8)!
dataTaskExpectation.fulfill()
}
}

extension DataTask : URLSessionTaskDelegate {
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
guard let e = error as? URLError else { return }
XCTAssertEqual(e.code, .timedOut, "Unexpected error code")
dataTaskExpectation.fulfill()
if let cancellation = cancelExpectation {
cancellation.fulfill()
}
self.error = true
}
}
Expand Down