From bb9d10c8c3a2deac4cb654c3bf2fc4e58d20ad33 Mon Sep 17 00:00:00 2001 From: Sergey Minakov Date: Thu, 9 Mar 2017 12:30:51 +0300 Subject: [PATCH] implemented nsurlsession chunked response test revert timeout intervals --- TestFoundation/HTTPServer.swift | 67 ++++++++++++++++++++++----- TestFoundation/TestNSURLSession.swift | 31 +++++++++++-- 2 files changed, 84 insertions(+), 14 deletions(-) diff --git a/TestFoundation/HTTPServer.swift b/TestFoundation/HTTPServer.swift index 5b032a247c..8cc0ebac7d 100644 --- a/TestFoundation/HTTPServer.swift +++ b/TestFoundation/HTTPServer.swift @@ -92,10 +92,33 @@ class _TCPSocket { _ = try attempt("read", valid: isNotNegative, CInt(read(connectionSocket, &buffer, 4096))) return String(cString: &buffer) } + + func split(_ str: String, _ count: Int) -> [String] { + return stride(from: 0, to: str.characters.count, by: count).map { i -> String in + let startIndex = str.index(str.startIndex, offsetBy: i) + let endIndex = str.index(startIndex, offsetBy: count, limitedBy: str.endIndex) ?? str.endIndex + return str[startIndex.. _HTTPResponse { if request.method == .GET { diff --git a/TestFoundation/TestNSURLSession.swift b/TestFoundation/TestNSURLSession.swift index cce15087a5..834b80f6f0 100644 --- a/TestFoundation/TestNSURLSession.swift +++ b/TestFoundation/TestNSURLSession.swift @@ -33,15 +33,16 @@ class TestURLSession : XCTestCase { ("test_taskError", test_taskError), ("test_taskCopy", test_taskCopy), ("test_cancelTask", test_cancelTask), +// ("test_taskTimeout", test_taskTimeout), ] } - private func runServer(with condition: ServerSemaphore) throws { + private func runServer(with condition: ServerSemaphore, startDelay: TimeInterval? = nil, sendDelay: TimeInterval? = nil, bodyChunks: Int? = nil) throws { let start = 21961 for port in start...(start+100) { //we must find at least one port to bind do { serverPort = port - let test = try TestURLSessionServer(port: UInt16(port)) + let test = try TestURLSessionServer(port: UInt16(port), startDelay: startDelay, sendDelay: sendDelay, bodyChunks: bodyChunks) try test.start(started: condition) try test.readAndRespond() test.stop() @@ -317,7 +318,31 @@ class TestURLSession : XCTestCase { d.cancel() waitForExpectations(timeout: 12) } - + + func test_taskTimeout() { + let serverReady = ServerSemaphore() + globalDispatchQueue.async { + do { + try self.runServer(with: serverReady, startDelay: 3, sendDelay: 3, bodyChunks: 3) + } catch { + XCTAssertTrue(true) + return + } + } + serverReady.wait() + let config = URLSessionConfiguration.default + config.timeoutIntervalForRequest = 5 + let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil) + var expect = expectation(description: "download task with handler") + let req = URLRequest(url: URL(string: "http://127.0.0.1:\(serverPort)/Peru")!) + var task = session.dataTask(with: req) { (data, _, error) -> Void in + defer { expect.fulfill() } + XCTAssertNil(error) + } + task.resume() + + waitForExpectations(timeout: 30) + } } class SessionDelegate: NSObject, URLSessionDelegate {