Skip to content

Commit 079fbf4

Browse files
committed
TestURLSession: Fix test_cancelTask()
- Add a delay into the test server response so that the task will always be cancelled before the server returns the complete response.
1 parent f995719 commit 079fbf4

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

TestFoundation/HTTPServer.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,16 @@ struct _HTTPRequest {
313313
return allHeaders
314314
}
315315

316+
public func getHeader(for key: String) -> String? {
317+
let lookup = key.lowercased()
318+
for header in headers {
319+
let parts = header.components(separatedBy: ":")
320+
if parts[0].lowercased() == lookup {
321+
return parts[1].trimmingCharacters(in: CharacterSet(charactersIn: " "))
322+
}
323+
}
324+
return nil
325+
}
316326
}
317327

318328
struct _HTTPResponse {
@@ -365,6 +375,13 @@ public class TestURLSessionServer {
365375

366376
public func readAndRespond() throws {
367377
let req = try httpServer.request()
378+
379+
if let value = req.getHeader(for: "x-pause") {
380+
if let wait = Double(value), wait > 0 {
381+
Thread.sleep(forTimeInterval: wait)
382+
}
383+
}
384+
368385
if req.uri.hasPrefix("/LandOfTheLostCities/") {
369386
/* these are all misbehaving servers */
370387
try httpServer.respondWithBrokenResponses(uri: req.uri)

TestFoundation/TestURLSession.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class TestURLSession : LoopbackServerTest {
2222
("test_finishTaskAndInvalidate", test_finishTasksAndInvalidate),
2323
("test_taskError", test_taskError),
2424
("test_taskCopy", test_taskCopy),
25-
//("test_cancelTask", test_cancelTask), // Breaks on Ubuntu18.04, doesnt always cancel
25+
("test_cancelTask", test_cancelTask),
2626
("test_taskTimeout", test_taskTimeout),
2727
("test_verifyRequestHeaders", test_verifyRequestHeaders),
2828
("test_verifyHttpAdditionalHeaders", test_verifyHttpAdditionalHeaders),
2929
("test_timeoutInterval", test_timeoutInterval),
3030
("test_httpRedirectionWithCompleteRelativePath", test_httpRedirectionWithCompleteRelativePath),
31-
("test_httpRedirectionWithInCompleteRelativePath", test_httpRedirectionWithInCompleteRelativePath), /* temporarily disabled. Needs HTTPServer rework */
32-
("test_httpRedirectionTimeout", test_httpRedirectionTimeout), /* temporarily disabled (https://bugs.swift.org/browse/SR-5751) */
31+
("test_httpRedirectionWithInCompleteRelativePath", test_httpRedirectionWithInCompleteRelativePath),
32+
("test_httpRedirectionTimeout", test_httpRedirectionTimeout),
3333
("test_http0_9SimpleResponses", test_http0_9SimpleResponses),
3434
("test_outOfRangeButCorrectlyFormattedHTTPCode", test_outOfRangeButCorrectlyFormattedHTTPCode),
3535
("test_missingContentLengthButStillABody", test_missingContentLengthButStillABody),
@@ -231,10 +231,11 @@ class TestURLSession : LoopbackServerTest {
231231
XCTFail("Intermittent failures on Android")
232232
#else
233233
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/Peru"
234-
let url = URL(string: urlString)!
234+
var urlRequest = URLRequest(url: URL(string: urlString)!)
235+
urlRequest.setValue("2.0", forHTTPHeaderField: "X-Pause")
235236
let d = DataTask(with: expectation(description: "GET \(urlString): task cancelation"))
236237
d.cancelExpectation = expectation(description: "GET \(urlString): task canceled")
237-
d.run(with: url)
238+
d.run(with: urlRequest)
238239
d.cancel()
239240
waitForExpectations(timeout: 12)
240241
#endif

0 commit comments

Comments
 (0)