Skip to content

Commit 6b6e8e6

Browse files
committed
Enable fast failure mode for testing
1 parent f17a47e commit 6b6e8e6

File tree

5 files changed

+52
-30
lines changed

5 files changed

+52
-30
lines changed

Tests/AsyncHTTPClientTests/AsyncAwaitEndToEndTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,9 @@ final class AsyncAwaitEndToEndTests: XCTestCase {
497497
defer { XCTAssertNoThrow(try serverChannel.close().wait()) }
498498
let port = serverChannel.localAddress!.port!
499499

500-
var config = HTTPClient.Configuration()
501-
config.timeout.connect = .seconds(3)
500+
let config = HTTPClient.Configuration()
501+
.enableFastFailureModeForTesting()
502+
502503
let localClient = HTTPClient(eventLoopGroupProvider: .createNew, configuration: config)
503504
defer { XCTAssertNoThrow(try localClient.syncShutdown()) }
504505
let request = HTTPClientRequest(url: "https://localhost:\(port)")

Tests/AsyncHTTPClientTests/HTTPClient+SOCKSTests.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ class HTTPClientSOCKSTests: XCTestCase {
7676
func testProxySOCKS() throws {
7777
let socksBin = try MockSOCKSServer(expectedURL: "/socks/test", expectedResponse: "it works!")
7878
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
79-
configuration: .init(proxy: .socksServer(host: "localhost", port: socksBin.port)))
79+
configuration: .init(
80+
proxy: .socksServer(host: "localhost", port: socksBin.port)
81+
).enableFastFailureModeForTesting())
8082

8183
defer {
8284
XCTAssertNoThrow(try localClient.syncShutdown())
@@ -90,8 +92,8 @@ class HTTPClientSOCKSTests: XCTestCase {
9092
}
9193

9294
func testProxySOCKSBogusAddress() throws {
93-
var config = HTTPClient.Configuration(proxy: .socksServer(host: "127.0.."))
94-
config.networkFrameworkWaitForConnectivity = false
95+
let config = HTTPClient.Configuration(proxy: .socksServer(host: "127.0.."))
96+
.enableFastFailureModeForTesting()
9597
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
9698
configuration: config)
9799

@@ -104,8 +106,8 @@ class HTTPClientSOCKSTests: XCTestCase {
104106
// there is no socks server, so we should fail
105107
func testProxySOCKSFailureNoServer() throws {
106108
let localHTTPBin = HTTPBin()
107-
var config = HTTPClient.Configuration(proxy: .socksServer(host: "localhost", port: localHTTPBin.port))
108-
config.networkFrameworkWaitForConnectivity = false
109+
let config = HTTPClient.Configuration(proxy: .socksServer(host: "localhost", port: localHTTPBin.port))
110+
.enableFastFailureModeForTesting()
109111

110112
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
111113
configuration: config)
@@ -118,8 +120,8 @@ class HTTPClientSOCKSTests: XCTestCase {
118120

119121
// speak to a server that doesn't speak SOCKS
120122
func testProxySOCKSFailureInvalidServer() throws {
121-
var config = HTTPClient.Configuration(proxy: .socksServer(host: "localhost"))
122-
config.networkFrameworkWaitForConnectivity = false
123+
let config = HTTPClient.Configuration(proxy: .socksServer(host: "localhost"))
124+
.enableFastFailureModeForTesting()
123125

124126
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
125127
configuration: config)
@@ -132,8 +134,8 @@ class HTTPClientSOCKSTests: XCTestCase {
132134
// test a handshake failure with a misbehaving server
133135
func testProxySOCKSMisbehavingServer() throws {
134136
let socksBin = try MockSOCKSServer(expectedURL: "/socks/test", expectedResponse: "it works!", misbehave: true)
135-
var config = HTTPClient.Configuration(proxy: .socksServer(host: "localhost", port: socksBin.port))
136-
config.networkFrameworkWaitForConnectivity = false
137+
let config = HTTPClient.Configuration(proxy: .socksServer(host: "localhost", port: socksBin.port))
138+
.enableFastFailureModeForTesting()
137139

138140
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
139141
configuration: config)

Tests/AsyncHTTPClientTests/HTTPClientInternalTests.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ class HTTPClientInternalTests: XCTestCase {
429429
let el2 = elg.next()
430430

431431
let httpBin = HTTPBin(.refuse)
432-
var config = HTTPClient.Configuration()
433-
config.networkFrameworkWaitForConnectivity = false
432+
let config = HTTPClient.Configuration()
433+
.enableFastFailureModeForTesting()
434434
let client = HTTPClient(eventLoopGroupProvider: .shared(elg), configuration: config)
435435

436436
defer {
@@ -590,3 +590,13 @@ class HTTPClientInternalTests: XCTestCase {
590590
XCTAssert(threadPools.dropFirst().allSatisfy { $0 === firstThreadPool })
591591
}
592592
}
593+
594+
595+
extension HTTPClient.Configuration {
596+
func enableFastFailureModeForTesting() -> Self {
597+
var copy = self
598+
copy.networkFrameworkWaitForConnectivity = false
599+
copy.connectionPool.retryConnectionEstablishment = false
600+
return copy
601+
}
602+
}

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class HTTPClientTests: XCTestCase {
5858
})
5959
backgroundLogger.logLevel = .trace
6060
self.defaultClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
61+
configuration: HTTPClient.Configuration().enableFastFailureModeForTesting(),
6162
backgroundActivityLogger: backgroundLogger)
6263
}
6364

@@ -778,14 +779,21 @@ class HTTPClientTests: XCTestCase {
778779
let res = try localClient.get(url: "http://test/ok").wait()
779780
XCTAssertEqual(res.status, .ok)
780781
}
781-
782+
782783
func testProxyPlaintextWithIncorrectlyAuthorization() throws {
783784
let localHTTPBin = HTTPBin(proxy: .simulate(authorization: "Basic YWxhZGRpbjpvcGVuc2VzYW1l"))
784-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
785-
configuration: .init(proxy: .server(host: "localhost",
786-
port: localHTTPBin.port,
787-
authorization: .basic(username: "aladdin",
788-
password: "opensesamefoo"))))
785+
let localClient = HTTPClient(
786+
eventLoopGroupProvider: .shared(self.clientGroup),
787+
configuration: .init(
788+
proxy: .server(
789+
host: "localhost",
790+
port: localHTTPBin.port,
791+
authorization: .basic(
792+
username: "aladdin",
793+
password: "opensesamefoo")
794+
)
795+
).enableFastFailureModeForTesting()
796+
)
789797
defer {
790798
XCTAssertNoThrow(try localClient.syncShutdown())
791799
XCTAssertNoThrow(try localHTTPBin.shutdown())
@@ -1228,11 +1236,9 @@ class HTTPClientTests: XCTestCase {
12281236
}
12291237

12301238
func testStressGetHttpsSSLError() throws {
1231-
var config = HTTPClient.Configuration()
1232-
config.networkFrameworkWaitForConnectivity = false
1233-
1239+
12341240
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
1235-
configuration: config)
1241+
configuration: HTTPClient.Configuration().enableFastFailureModeForTesting())
12361242
defer {
12371243
XCTAssertNoThrow(try localClient.syncShutdown())
12381244
}
@@ -1292,8 +1298,8 @@ class HTTPClientTests: XCTestCase {
12921298
defer { XCTAssertNoThrow(try serverChannel.close().wait()) }
12931299
let port = serverChannel.localAddress!.port!
12941300

1295-
var config = HTTPClient.Configuration()
1296-
config.timeout.connect = .seconds(2)
1301+
let config = HTTPClient.Configuration().enableFastFailureModeForTesting()
1302+
12971303
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), configuration: config)
12981304
defer { XCTAssertNoThrow(try localClient.syncShutdown()) }
12991305
XCTAssertThrowsError(try localClient.get(url: "https://localhost:\(port)").wait()) { error in
@@ -1332,8 +1338,8 @@ class HTTPClientTests: XCTestCase {
13321338
defer { XCTAssertNoThrow(try serverChannel.close().wait()) }
13331339
let port = serverChannel.localAddress!.port!
13341340

1335-
var config = HTTPClient.Configuration()
1336-
config.timeout.connect = .seconds(3)
1341+
let config = HTTPClient.Configuration().enableFastFailureModeForTesting()
1342+
13371343
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), configuration: config)
13381344
defer { XCTAssertNoThrow(try localClient.syncShutdown()) }
13391345

Tests/AsyncHTTPClientTests/HTTPConnectionPool+FactoryTests.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class HTTPConnectionPool_FactoryTests: XCTestCase {
7777
let factory = HTTPConnectionPool.ConnectionFactory(
7878
key: .init(request),
7979
tlsConfiguration: nil,
80-
clientConfiguration: .init(proxy: .socksServer(host: "127.0.0.1", port: server!.localAddress!.port!)),
80+
clientConfiguration: .init(proxy: .socksServer(host: "127.0.0.1", port: server!.localAddress!.port!))
81+
.enableFastFailureModeForTesting(),
8182
sslContextCache: .init()
8283
)
8384

@@ -113,7 +114,8 @@ class HTTPConnectionPool_FactoryTests: XCTestCase {
113114
let factory = HTTPConnectionPool.ConnectionFactory(
114115
key: .init(request),
115116
tlsConfiguration: nil,
116-
clientConfiguration: .init(proxy: .server(host: "127.0.0.1", port: server!.localAddress!.port!)),
117+
clientConfiguration: .init(proxy: .server(host: "127.0.0.1", port: server!.localAddress!.port!))
118+
.enableFastFailureModeForTesting(),
117119
sslContextCache: .init()
118120
)
119121

@@ -151,7 +153,8 @@ class HTTPConnectionPool_FactoryTests: XCTestCase {
151153
let factory = HTTPConnectionPool.ConnectionFactory(
152154
key: .init(request),
153155
tlsConfiguration: nil,
154-
clientConfiguration: .init(tlsConfiguration: tlsConfig),
156+
clientConfiguration: .init(tlsConfiguration: tlsConfig)
157+
.enableFastFailureModeForTesting(),
155158
sslContextCache: .init()
156159
)
157160

0 commit comments

Comments
 (0)