Skip to content

Commit 151cc91

Browse files
committed
Test fixes.
1 parent 5549463 commit 151cc91

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

Foundation/URLSession/URLSessionTask.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ open class URLSessionTask : NSObject, NSCopying {
234234
session = _MissingURLSession()
235235
taskIdentifier = 0
236236
originalRequest = nil
237-
knownBody = .none
237+
knownBody = URLSessionTask._Body.none
238238
workQueue = DispatchQueue(label: "URLSessionTask.notused.0")
239239
super.init()
240240
}

TestFoundation/TestURLSession.swift

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ class TestURLSession : LoopbackServerTest {
147147
func test_downloadTaskWithURL() {
148148
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
149149
let url = URL(string: urlString)!
150-
let d = DownloadTask(with: expectation(description: "Download GET \(urlString): with a delegate"))
150+
let d = DownloadTask(testCase: self, description: "Download GET \(urlString): with a delegate")
151151
d.run(with: url)
152152
waitForExpectations(timeout: 12)
153153
}
154154

155155
func test_downloadTaskWithURLRequest() {
156156
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
157157
let urlRequest = URLRequest(url: URL(string: urlString)!)
158-
let d = DownloadTask(with: expectation(description: "Download GET \(urlString): with a delegate"))
158+
let d = DownloadTask(testCase: self, description: "Download GET \(urlString): with a delegate")
159159
d.run(with: urlRequest)
160160
waitForExpectations(timeout: 12)
161161
}
@@ -203,7 +203,7 @@ class TestURLSession : LoopbackServerTest {
203203
func test_gzippedDownloadTask() {
204204
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/gzipped-response"
205205
let url = URL(string: urlString)!
206-
let d = DownloadTask(with: expectation(description: "GET \(urlString): gzipped response"))
206+
let d = DownloadTask(testCase: self, description: "GET \(urlString): gzipped response")
207207
d.run(with: url)
208208
waitForExpectations(timeout: 12)
209209
if d.totalBytesWritten != "Hello World!".utf8.count {
@@ -986,7 +986,7 @@ class TestURLSession : LoopbackServerTest {
986986
}.resume()
987987
waitForExpectations(timeout: 20)
988988

989-
let d = DownloadTask(with: expectation(description: "Invalid resume data for download task (with delegate)"))
989+
let d = DownloadTask(testCase: self, description: "Invalid resume data for download task")
990990
d.run { (session) -> DownloadTask.Configuration in
991991
return DownloadTask.Configuration(task: session.downloadTask(withResumeData: Data()),
992992
errorExpectation:
@@ -1234,16 +1234,28 @@ extension DataTask : URLSessionTaskDelegate {
12341234

12351235
class DownloadTask : NSObject {
12361236
var totalBytesWritten: Int64 = 0
1237-
let dwdExpectation: XCTestExpectation!
1237+
var didDownloadExpectation: XCTestExpectation?
1238+
let didCompleteExpectation: XCTestExpectation
12381239
var session: URLSession! = nil
12391240
var task: URLSessionDownloadTask! = nil
12401241
var errorExpectation: ((Error) -> Void)?
1242+
weak var testCase: XCTestCase?
1243+
var expectationsDescription: String
12411244

1242-
init(with expectation: XCTestExpectation) {
1243-
dwdExpectation = expectation
1245+
init(testCase: XCTestCase, description: String) {
1246+
self.expectationsDescription = description
1247+
self.testCase = testCase
1248+
self.didCompleteExpectation = testCase.expectation(description: "Did complete \(description)")
1249+
}
1250+
1251+
private func makeDownloadExpectation() {
1252+
guard didDownloadExpectation == nil else { return }
1253+
self.didDownloadExpectation = testCase!.expectation(description: "Did finish download: \(description)")
1254+
self.testCase = nil // No need for it any more here.
12441255
}
12451256

12461257
func run(with url: URL) {
1258+
makeDownloadExpectation()
12471259
let config = URLSessionConfiguration.default
12481260
config.timeoutIntervalForRequest = 8
12491261
session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
@@ -1252,6 +1264,7 @@ class DownloadTask : NSObject {
12521264
}
12531265

12541266
func run(with urlRequest: URLRequest) {
1267+
makeDownloadExpectation()
12551268
let config = URLSessionConfiguration.default
12561269
config.timeoutIntervalForRequest = 8
12571270
session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
@@ -1272,6 +1285,9 @@ class DownloadTask : NSObject {
12721285

12731286
task = taskConfiguration.task
12741287
errorExpectation = taskConfiguration.errorExpectation
1288+
if errorExpectation == nil {
1289+
makeDownloadExpectation()
1290+
}
12751291
task.resume()
12761292
}
12771293
}
@@ -1284,7 +1300,7 @@ extension DownloadTask : URLSessionDownloadDelegate {
12841300
}
12851301

12861302
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
1287-
defer { dwdExpectation.fulfill() }
1303+
defer { didDownloadExpectation?.fulfill() }
12881304

12891305
guard self.errorExpectation == nil else {
12901306
XCTFail("Expected an error, but got …didFinishDownloadingTo… from download task \(downloadTask) (at \(location))")
@@ -1302,7 +1318,7 @@ extension DownloadTask : URLSessionDownloadDelegate {
13021318

13031319
extension DownloadTask : URLSessionTaskDelegate {
13041320
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
1305-
defer { dwdExpectation.fulfill() }
1321+
defer { didCompleteExpectation.fulfill() }
13061322

13071323
if let errorExpectation = self.errorExpectation {
13081324
if let error = error {

0 commit comments

Comments
 (0)