Skip to content

Commit 9f65902

Browse files
committed
fix missing connect timeout and make tests safer (swift-server#267)
* fix missing connect timeout and make tests safer * swiftformat and linux tests * fix timeout test * speedup another test * make tests safer
1 parent 02b3ed9 commit 9f65902

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

Sources/AsyncHTTPClient/Utils.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ extension NIOClientTCPBootstrap {
7979
requiresTLS: Bool,
8080
configuration: HTTPClient.Configuration
8181
) throws -> NIOClientTCPBootstrap {
82-
let bootstrap: NIOClientTCPBootstrap
82+
var bootstrap: NIOClientTCPBootstrap
8383
#if canImport(Network)
8484
// if eventLoop is compatible with NIOTransportServices create a NIOTSConnectionBootstrap
8585
if #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *), let tsBootstrap = NIOTSConnectionBootstrap(validatingGroup: eventLoop) {
@@ -106,10 +106,15 @@ extension NIOClientTCPBootstrap {
106106
}
107107
#endif
108108

109+
if let timeout = configuration.timeout.connect {
110+
bootstrap = bootstrap.connectTimeout(timeout)
111+
}
112+
109113
// don't enable TLS if we have a proxy, this will be enabled later on
110114
if requiresTLS, configuration.proxy == nil {
111115
return bootstrap.enableTLS()
112116
}
117+
113118
return bootstrap
114119
}
115120

Tests/AsyncHTTPClientTests/HTTPClientNIOTSTests.swift

+9-14
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ class HTTPClientNIOTSTests: XCTestCase {
7474
func testConnectionFailError() {
7575
guard isTestingNIOTS() else { return }
7676
let httpBin = HTTPBin(ssl: true)
77-
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
77+
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
78+
configuration: .init(timeout: .init(connect: .milliseconds(100),
79+
read: .milliseconds(100))))
80+
7881
defer {
7982
XCTAssertNoThrow(try httpClient.syncShutdown(requiresCleanClose: true))
8083
}
84+
8185
let port = httpBin.port
8286
XCTAssertNoThrow(try httpBin.shutdown())
8387

84-
do {
85-
_ = try httpClient.get(url: "https://localhost:\(port)/get").wait()
86-
XCTFail("This should have failed")
87-
} catch ChannelError.connectTimeout {
88-
} catch {
89-
XCTFail("Error should have been ChannelError.connectTimeout not \(type(of: error))")
88+
XCTAssertThrowsError(try httpClient.get(url: "https://localhost:\(port)/get").wait()) { error in
89+
XCTAssertEqual(.connectTimeout(.milliseconds(100)), error as? ChannelError)
9090
}
9191
}
9292

@@ -103,13 +103,8 @@ class HTTPClientNIOTSTests: XCTestCase {
103103
XCTAssertNoThrow(try httpBin.shutdown())
104104
}
105105

106-
do {
107-
_ = try httpClient.get(url: "https://localhost:\(httpBin.port)/get").wait()
108-
XCTFail("This should have failed")
109-
} catch let error as HTTPClient.NWTLSError {
110-
XCTAssertEqual(error.status, errSSLHandshakeFail)
111-
} catch {
112-
XCTFail("Error should have been NWTLSError not \(type(of: error))")
106+
XCTAssertThrowsError(try httpClient.get(url: "https://localhost:\(httpBin.port)/get").wait()) { error in
107+
XCTAssertEqual((error as? HTTPClient.NWTLSError)?.status, errSSLHandshakeFail)
113108
}
114109
#endif
115110
}

Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extension HTTPClientTests {
4646
("testStreaming", testStreaming),
4747
("testRemoteClose", testRemoteClose),
4848
("testReadTimeout", testReadTimeout),
49+
("testConnectTimeout", testConnectTimeout),
4950
("testDeadline", testDeadline),
5051
("testCancel", testCancel),
5152
("testStressCancel", testStressCancel),

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+15-1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,20 @@ class HTTPClientTests: XCTestCase {
422422
}
423423
}
424424

425+
func testConnectTimeout() throws {
426+
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
427+
configuration: .init(timeout: .init(connect: .milliseconds(100), read: .milliseconds(150))))
428+
429+
defer {
430+
XCTAssertNoThrow(try httpClient.syncShutdown())
431+
}
432+
433+
// This must throw as 198.51.100.254 is reserved for documentation only
434+
XCTAssertThrowsError(try httpClient.get(url: "http://198.51.100.254:65535/get").wait()) { error in
435+
XCTAssertEqual(.connectTimeout(.milliseconds(100)), error as? ChannelError)
436+
}
437+
}
438+
425439
func testDeadline() throws {
426440
XCTAssertThrowsError(try self.defaultClient.get(url: self.defaultHTTPBinURLPrefix + "wait", deadline: .now() + .milliseconds(150)).wait(), "Should fail") { error in
427441
guard case let error = error as? HTTPClientError, error == .readTimeout else {
@@ -1661,7 +1675,7 @@ class HTTPClientTests: XCTestCase {
16611675
}
16621676

16631677
func testAvoidLeakingTLSHandshakeCompletionPromise() {
1664-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
1678+
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), configuration: .init(timeout: .init(connect: .milliseconds(100))))
16651679
let localHTTPBin = HTTPBin()
16661680
let port = localHTTPBin.port
16671681
XCTAssertNoThrow(try localHTTPBin.shutdown())

0 commit comments

Comments
 (0)