       XCTFail("succeeded incorrectly")
     }
     catch let error as URLError {
+      XCTAssertEqual(error.code, .cancelled)
+    }
+
+    session.finishTasksAndInvalidate()
+  }
+
+  func testDownload_Cancellation() throws
+  {
+    let url = URL(string: "http://127.0.0.1:65521/image.jpg")!
+    let session = URLSession(configuration: .default)
+
+    let deferred = session.deferredDownloadTask(with: url)
+    let canceled = deferred.cancel()
+    XCTAssert(canceled)
+
+    do {
+      let _ = try deferred.get()
+      XCTFail("succeeded incorrectly")
+    }
+    catch let error as URLError {
+      XCTAssertEqual(error.code, .cancelled)
+    }
+    catch URLSessionError.InterruptedDownload(let error, _) {
+      XCTAssertEqual(error.code, .cancelled)
+    }
+
+    session.finishTasksAndInvalidate()
+  }
+
+  func testDownload_DoubleCancellation() throws
+  {
+    let deferred: DeferredURLSessionTask<(URL, FileHandle, HTTPURLResponse)> = {
+      let url = URL(string: "http://127.0.0.1:65521/image.jpg")!
+      let session = URLSession(configuration: .default)
+      defer { session.finishTasksAndInvalidate() }
+
+      return session.deferredDownloadTask(with: url)
+    }()
+
+    deferred.urlSessionTask.cancel()
+
+    do {
+      let _ = try deferred.get()
+      XCTFail("succeeded incorrectly")
+    }
+    catch let error as URLError {
+      XCTAssertEqual(error.code, .cancelled)
+    }
+
+    let canceled = deferred.cancel()
+    XCTAssert(canceled == false)
+  }
+
+  func testDownload_SuspendCancel() throws
+  {
+    let url = URL(string: "http://127.0.0.1:65521/image.jpg")!
+    let session = URLSession(configuration: .default)
+
+    let deferred = session.deferredDownloadTask(with: url)
+    deferred.urlSessionTask.suspend()
+    let canceled = deferred.cancel()
+    XCTAssert(canceled)
+
+    do {
+      let _ = try deferred.get()
+      XCTFail("succeeded incorrectly")
+    }
+    catch let error as URLError {
+      XCTAssertEqual(error.code, .cancelled)
+    }
+    catch URLSessionError.InterruptedDownload(let error, _) {
       XCTAssertEqual(error.code, .cancelled)
     }
 

      return session.deferredDataTask(with: url)
    }()

    usleep(1000)

    do {
      let _ = try deferred.get()
      XCTFail("succeeded incorrectly")
    }
    catch let error as URLError {
      XCTAssertEqual(error.code, .cannotConnectToHost)
    }

    let canceled = deferred.cancel()

  }



  func testDownload_NotFound() throws
  {
