Skip to content

Commit f58a142

Browse files
Set timeout in NetworkSession.
1 parent f9e47e3 commit f58a142

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

swift-sdk/Internal/NetworkSession.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,29 @@ extension URLSessionDataTask: DataTaskProtocol {}
1414

1515
protocol NetworkSessionProtocol {
1616
typealias CompletionHandler = (Data?, URLResponse?, Error?) -> Void
17+
var timeout: TimeInterval { get set }
1718
func makeRequest(_ request: URLRequest, completionHandler: @escaping CompletionHandler)
1819
func makeDataRequest(with url: URL, completionHandler: @escaping CompletionHandler)
1920
func createDataTask(with url: URL, completionHandler: @escaping CompletionHandler) -> DataTaskProtocol
2021
}
2122

23+
extension NetworkSessionProtocol {
24+
var timeout: TimeInterval {
25+
get { 60.0 }
26+
set {}
27+
}
28+
}
29+
2230
extension URLSession: NetworkSessionProtocol {
31+
var timeout: TimeInterval {
32+
get {
33+
configuration.timeoutIntervalForRequest
34+
}
35+
set {
36+
configuration.timeoutIntervalForRequest = newValue
37+
}
38+
}
39+
2340
func makeRequest(_ request: URLRequest, completionHandler: @escaping CompletionHandler) {
2441
let task = dataTask(with: request) { data, response, error in
2542
completionHandler(data, response, error)
@@ -50,6 +67,15 @@ protocol RedirectNetworkSessionProvider {
5067
}
5168

5269
class RedirectNetworkSession: NSObject, NetworkSessionProtocol {
70+
var timeout: TimeInterval {
71+
get {
72+
networkSession.timeout
73+
}
74+
set {
75+
networkSession.timeout = newValue
76+
}
77+
}
78+
5379
func makeRequest(_ request: URLRequest, completionHandler: @escaping CompletionHandler) {
5480
networkSession.makeRequest(request, completionHandler: completionHandler)
5581
}

tests/common/CommonExtensions.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ class MockDependencyContainer: DependencyContainerProtocol {
143143
}
144144

145145
struct MockRedirectNetworkSession: NetworkSessionProtocol {
146+
var timeout: TimeInterval = 60.0
147+
146148
init(networkSession: NetworkSessionProtocol, redirectDelegate: RedirectNetworkSessionDelegate) {
147149
self.networkSession = Self.createRedirectNetworkSession(fromNetworkSession: networkSession, redirectDelegate: redirectDelegate)
148150
}

tests/common/CommonMocks.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ class MockNetworkSession: NetworkSessionProtocol {
203203

204204
var responseCallback: ((URL) -> MockResponse?)?
205205

206+
var timeout: TimeInterval = 60.0
207+
206208
var requests = [URLRequest]()
207209
var callback: ((Data?, URLResponse?, Error?) -> Void)?
208210
var requestCallback: ((URLRequest) -> Void)?
@@ -263,8 +265,16 @@ class MockNetworkSession: NetworkSessionProtocol {
263265
block()
264266
}
265267
} else {
266-
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
267-
block()
268+
if delay < timeout {
269+
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
270+
block()
271+
}
272+
} else {
273+
DispatchQueue.main.asyncAfter(deadline: .now() + timeout) {
274+
let error = NetworkError(reason: "The request timed out.")
275+
completionHandler(nil, nil, error)
276+
self.callback?(nil, nil, error)
277+
}
268278
}
269279
}
270280
}
@@ -312,7 +322,7 @@ class MockNetworkSession: NetworkSessionProtocol {
312322
static func json(fromData data: Data) -> [AnyHashable: Any] {
313323
try! JSONSerialization.jsonObject(with: data, options: []) as! [AnyHashable: Any]
314324
}
315-
325+
316326
private static let defaultStatus = 200
317327
private static let defaultData = [:].toJsonData()
318328
private static let defaultHttpVersion = "HTTP/1.1"

tests/unit-tests/IterableAPIResponseTests.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Copyright © 2018 Iterable. All rights reserved.
33
//
44

5-
import OHHTTPStubs
65
import XCTest
76

87
@testable import IterableSDK
@@ -159,18 +158,12 @@ class IterableAPIResponseTests: XCTestCase {
159158
let responseTime = 2.0
160159
let timeout = 0.1
161160

162-
HTTPStubs.stubRequests(passingTest: { (_) -> Bool in
163-
true
164-
}) { (_) -> HTTPStubsResponse in
165-
let response = HTTPStubsResponse(data: try! JSONSerialization.data(withJSONObject: [:], options: []),
166-
statusCode: 200,
167-
headers: nil)
168-
response.requestTime = 0.0
169-
response.responseTime = responseTime
170-
return response
161+
let networkSession = MockNetworkSession() { _ in
162+
MockNetworkSession.MockResponse(statusCode: 200,
163+
data: [:].toJsonData(),
164+
delay: responseTime)
171165
}
172-
173-
let networkSession = URLSession(configuration: URLSessionConfiguration.default)
166+
networkSession.timeout = timeout
174167

175168
let iterableRequest = IterableRequest.post(PostRequest(path: "", args: nil, body: [:]))
176169

@@ -185,6 +178,7 @@ class IterableAPIResponseTests: XCTestCase {
185178

186179
wait(for: [xpectation], timeout: testExpectationTimeout)
187180
}
181+
188182

189183
private func verifyIterableHeaders(_ urlRequest: URLRequest) {
190184
XCTAssertEqual(urlRequest.value(forHTTPHeaderField: JsonKey.Header.sdkPlatform), JsonValue.iOS)

0 commit comments

Comments
 (0)