Skip to content

Commit 05b9d9b

Browse files
committed
Make tests faster, by reducing timeout margins
1 parent 93de326 commit 05b9d9b

File tree

3 files changed

+104
-72
lines changed

3 files changed

+104
-72
lines changed

Tests/AsyncHTTPClientTests/HTTPClient+SOCKSTests.swift

+37-10
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@ class HTTPClientSOCKSTests: XCTestCase {
7575

7676
func testProxySOCKS() throws {
7777
let socksBin = try MockSOCKSServer(expectedURL: "/socks/test", expectedResponse: "it works!")
78-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
79-
configuration: .init(proxy: .socksServer(host: "localhost", port: socksBin.port)))
78+
let localClient = HTTPClient(
79+
eventLoopGroupProvider: .shared(self.clientGroup),
80+
configuration: .init(
81+
timeout: .init(connect: .seconds(2), read: nil),
82+
proxy: .socksServer(host: "localhost", port: socksBin.port)
83+
)
84+
)
8085

8186
defer {
8287
XCTAssertNoThrow(try localClient.syncShutdown())
@@ -90,8 +95,13 @@ class HTTPClientSOCKSTests: XCTestCase {
9095
}
9196

9297
func testProxySOCKSBogusAddress() throws {
93-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
94-
configuration: .init(proxy: .socksServer(host: "127.0..")))
98+
let localClient = HTTPClient(
99+
eventLoopGroupProvider: .shared(self.clientGroup),
100+
configuration: .init(
101+
timeout: .init(connect: .seconds(2), read: nil),
102+
proxy: .socksServer(host: "127.0..")
103+
)
104+
)
95105

96106
defer {
97107
XCTAssertNoThrow(try localClient.syncShutdown())
@@ -102,8 +112,14 @@ class HTTPClientSOCKSTests: XCTestCase {
102112
// there is no socks server, so we should fail
103113
func testProxySOCKSFailureNoServer() throws {
104114
let localHTTPBin = HTTPBin()
105-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
106-
configuration: .init(proxy: .socksServer(host: "localhost", port: localHTTPBin.port)))
115+
let localClient = HTTPClient(
116+
eventLoopGroupProvider: .shared(self.clientGroup),
117+
configuration: .init(
118+
timeout: .init(connect: .seconds(2), read: nil),
119+
proxy: .socksServer(host: "localhost", port: localHTTPBin.port)
120+
)
121+
)
122+
107123
defer {
108124
XCTAssertNoThrow(try localClient.syncShutdown())
109125
XCTAssertNoThrow(try localHTTPBin.shutdown())
@@ -113,8 +129,14 @@ class HTTPClientSOCKSTests: XCTestCase {
113129

114130
// speak to a server that doesn't speak SOCKS
115131
func testProxySOCKSFailureInvalidServer() throws {
116-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
117-
configuration: .init(proxy: .socksServer(host: "localhost")))
132+
let localClient = HTTPClient(
133+
eventLoopGroupProvider: .shared(self.clientGroup),
134+
configuration: .init(
135+
timeout: .init(connect: .seconds(2), read: nil),
136+
proxy: .socksServer(host: "localhost")
137+
)
138+
)
139+
118140
defer {
119141
XCTAssertNoThrow(try localClient.syncShutdown())
120142
}
@@ -124,8 +146,13 @@ class HTTPClientSOCKSTests: XCTestCase {
124146
// test a handshake failure with a misbehaving server
125147
func testProxySOCKSMisbehavingServer() throws {
126148
let socksBin = try MockSOCKSServer(expectedURL: "/socks/test", expectedResponse: "it works!", misbehave: true)
127-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
128-
configuration: .init(proxy: .socksServer(host: "localhost", port: socksBin.port)))
149+
let localClient = HTTPClient(
150+
eventLoopGroupProvider: .shared(self.clientGroup),
151+
configuration: .init(
152+
timeout: .init(connect: .seconds(2), read: nil),
153+
proxy: .socksServer(host: "localhost", port: socksBin.port)
154+
)
155+
)
129156

130157
defer {
131158
XCTAssertNoThrow(try localClient.syncShutdown())

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+63-61
Original file line numberDiff line numberDiff line change
@@ -646,10 +646,8 @@ class HTTPClientTests: XCTestCase {
646646
task.cancel()
647647
}
648648

649-
XCTAssertThrowsError(try task.wait(), "Should fail") { error in
650-
guard case let error = error as? HTTPClientError, error == .cancelled else {
651-
return XCTFail("Should fail with cancelled")
652-
}
649+
XCTAssertThrowsError(try task.wait()) {
650+
XCTAssertEqual($0 as? HTTPClientError, .cancelled)
653651
}
654652
}
655653

@@ -730,19 +728,23 @@ class HTTPClientTests: XCTestCase {
730728

731729
func testProxyPlaintextWithIncorrectlyAuthorization() throws {
732730
let localHTTPBin = HTTPBin(proxy: .simulate(authorization: "Basic YWxhZGRpbjpvcGVuc2VzYW1l"))
733-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
734-
configuration: .init(proxy: .server(host: "localhost",
735-
port: localHTTPBin.port,
736-
authorization: .basic(username: "aladdin",
737-
password: "opensesamefoo"))))
731+
let localClient = HTTPClient(
732+
eventLoopGroupProvider: .shared(self.clientGroup),
733+
configuration: .init(
734+
timeout: .init(connect: .seconds(2), read: nil),
735+
proxy: .server(
736+
host: "localhost",
737+
port: localHTTPBin.port,
738+
authorization: .basic(username: "aladdin", password: "opensesamefoo")
739+
)
740+
)
741+
)
738742
defer {
739743
XCTAssertNoThrow(try localClient.syncShutdown())
740744
XCTAssertNoThrow(try localHTTPBin.shutdown())
741745
}
742-
XCTAssertThrowsError(try localClient.get(url: "http://test/ok").wait(), "Should fail") { error in
743-
guard case let error = error as? HTTPClientError, error == .proxyAuthenticationRequired else {
744-
return XCTFail("Should fail with HTTPClientError.proxyAuthenticationRequired")
745-
}
746+
XCTAssertThrowsError(try localClient.get(url: "http://test/ok").wait()) {
747+
XCTAssertEqual($0 as? HTTPClientError, .proxyAuthenticationRequired)
746748
}
747749
}
748750

@@ -859,31 +861,41 @@ class HTTPClientTests: XCTestCase {
859861

860862
func testLoopDetectionRedirectLimit() throws {
861863
let localHTTPBin = HTTPBin(.http1_1(ssl: true))
862-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
863-
configuration: HTTPClient.Configuration(certificateVerification: .none, redirectConfiguration: .follow(max: 5, allowCycles: false)))
864+
let localClient = HTTPClient(
865+
eventLoopGroupProvider: .shared(self.clientGroup),
866+
configuration: .init(
867+
certificateVerification: .none,
868+
redirectConfiguration: .follow(max: 5, allowCycles: false)
869+
)
870+
)
864871

865872
defer {
866873
XCTAssertNoThrow(try localClient.syncShutdown())
867874
XCTAssertNoThrow(try localHTTPBin.shutdown())
868875
}
869876

870-
XCTAssertThrowsError(try localClient.get(url: "https://localhost:\(localHTTPBin.port)/redirect/infinite1").wait(), "Should fail with redirect limit") { error in
871-
XCTAssertEqual(error as? HTTPClientError, HTTPClientError.redirectCycleDetected)
877+
XCTAssertThrowsError(try localClient.get(url: "https://localhost:\(localHTTPBin.port)/redirect/infinite1").wait()) {
878+
XCTAssertEqual($0 as? HTTPClientError, HTTPClientError.redirectCycleDetected)
872879
}
873880
}
874881

875882
func testCountRedirectLimit() throws {
876883
let localHTTPBin = HTTPBin(.http1_1(ssl: true))
877-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
878-
configuration: HTTPClient.Configuration(certificateVerification: .none, redirectConfiguration: .follow(max: 10, allowCycles: true)))
884+
let localClient = HTTPClient(
885+
eventLoopGroupProvider: .shared(self.clientGroup),
886+
configuration: .init(
887+
certificateVerification: .none,
888+
redirectConfiguration: .follow(max: 10, allowCycles: true)
889+
)
890+
)
879891

880892
defer {
881893
XCTAssertNoThrow(try localClient.syncShutdown())
882894
XCTAssertNoThrow(try localHTTPBin.shutdown())
883895
}
884896

885-
XCTAssertThrowsError(try localClient.get(url: "https://localhost:\(localHTTPBin.port)/redirect/infinite1").wait(), "Should fail with redirect limit") { error in
886-
XCTAssertEqual(error as? HTTPClientError, HTTPClientError.redirectLimitReached)
897+
XCTAssertThrowsError(try localClient.get(url: "https://localhost:\(localHTTPBin.port)/redirect/infinite1").wait()) {
898+
XCTAssertEqual($0 as? HTTPClientError, HTTPClientError.redirectLimitReached)
887899
}
888900
}
889901

@@ -1105,9 +1117,15 @@ class HTTPClientTests: XCTestCase {
11051117
}
11061118

11071119
func testStressGetHttpsSSLError() throws {
1120+
let localClient = HTTPClient(
1121+
eventLoopGroupProvider: .createNew,
1122+
configuration: .init(timeout: .init(connect: .seconds(2), read: nil))
1123+
)
1124+
defer { XCTAssertNoThrow(try localClient.syncShutdown()) }
1125+
11081126
let request = try Request(url: "https://localhost:\(self.defaultHTTPBin.port)/wait", method: .GET)
11091127
let tasks = (1...100).map { _ -> HTTPClient.Task<TestHTTPDelegate.Response> in
1110-
self.defaultClient.execute(request: request, delegate: TestHTTPDelegate())
1128+
localClient.execute(request: request, delegate: TestHTTPDelegate())
11111129
}
11121130

11131131
let results = try EventLoopFuture<TestHTTPDelegate.Response>.whenAllComplete(tasks.map { $0.futureResult }, on: self.defaultClient.eventLoopGroup.next()).wait()
@@ -1148,14 +1166,10 @@ class HTTPClientTests: XCTestCase {
11481166
XCTAssertNoThrow(try localClient.syncShutdown())
11491167
XCTAssertNoThrow(try localHTTPBin.shutdown())
11501168
}
1151-
do {
1152-
_ = try localClient.get(url: "http://localhost:\(localHTTPBin.port)/get").timeout(after: .seconds(5)).wait()
1153-
XCTFail("Shouldn't succeed")
1154-
} catch {
1155-
guard !(error is EventLoopFutureTimeoutError) else {
1156-
XCTFail("Timed out but should have failed immediately")
1157-
return
1158-
}
1169+
1170+
let future = localClient.get(url: "http://localhost:\(localHTTPBin.port)/get").timeout(after: .seconds(5))
1171+
XCTAssertThrowsError(try future.wait()) {
1172+
XCTAssertFalse($0 is EventLoopFutureTimeoutError, "Timed out but should have failed immediately")
11591173
}
11601174
}
11611175

@@ -1296,41 +1310,26 @@ class HTTPClientTests: XCTestCase {
12961310
case .success:
12971311
XCTFail("Shouldn't succeed")
12981312
case .failure(let error):
1299-
if let clientError = error as? HTTPClientError, clientError == .cancelled {
1300-
continue
1301-
} else {
1302-
XCTFail("Unexpected error: \(error)")
1303-
}
1313+
XCTAssertEqual(error as? HTTPClientError, .cancelled)
13041314
}
13051315
}
13061316
}
13071317

13081318
func testDoubleShutdown() {
13091319
let client = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
13101320
XCTAssertNoThrow(try client.syncShutdown())
1311-
do {
1312-
try client.syncShutdown()
1313-
XCTFail("Shutdown should fail with \(HTTPClientError.alreadyShutdown)")
1314-
} catch {
1315-
guard let clientError = error as? HTTPClientError, clientError == .alreadyShutdown else {
1316-
XCTFail("Unexpected error: \(error) instead of \(HTTPClientError.alreadyShutdown)")
1317-
return
1318-
}
1321+
1322+
XCTAssertThrowsError(try client.syncShutdown()) {
1323+
XCTAssertEqual($0 as? HTTPClientError, .alreadyShutdown)
13191324
}
13201325
}
13211326

13221327
func testTaskFailsWhenClientIsShutdown() {
13231328
let client = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
13241329
XCTAssertNoThrow(try client.syncShutdown())
1325-
do {
1326-
_ = try client.get(url: "http://localhost/").wait()
1327-
XCTFail("Request shouldn't succeed")
1328-
} catch {
1329-
if let error = error as? HTTPClientError, error == .alreadyShutdown {
1330-
return
1331-
} else {
1332-
XCTFail("Unexpected error: \(error)")
1333-
}
1330+
1331+
XCTAssertThrowsError(try client.get(url: "http://localhost/").wait()) {
1332+
XCTAssertEqual($0 as? HTTPClientError, .alreadyShutdown)
13341333
}
13351334
}
13361335

@@ -1712,25 +1711,28 @@ class HTTPClientTests: XCTestCase {
17121711
}
17131712

17141713
func testRacePoolIdleConnectionsAndGet() {
1715-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
1716-
configuration: .init(connectionPool: .init(idleTimeout: .milliseconds(10))))
1717-
defer {
1718-
XCTAssertNoThrow(try localClient.syncShutdown())
1719-
}
1714+
let localClient = HTTPClient(
1715+
eventLoopGroupProvider: .shared(self.clientGroup),
1716+
configuration: .init(connectionPool: .init(idleTimeout: .milliseconds(10)))
1717+
)
1718+
1719+
defer { XCTAssertNoThrow(try localClient.syncShutdown()) }
17201720
for _ in 1...500 {
17211721
XCTAssertNoThrow(try localClient.get(url: self.defaultHTTPBinURLPrefix + "get").wait())
17221722
Thread.sleep(forTimeInterval: 0.01 + .random(in: -0.05...0.05))
17231723
}
17241724
}
17251725

17261726
func testAvoidLeakingTLSHandshakeCompletionPromise() {
1727-
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup), configuration: .init(timeout: .init(connect: .milliseconds(100))))
17281727
let localHTTPBin = HTTPBin()
17291728
let port = localHTTPBin.port
17301729
XCTAssertNoThrow(try localHTTPBin.shutdown())
1731-
defer {
1732-
XCTAssertNoThrow(try localClient.syncShutdown())
1733-
}
1730+
1731+
let localClient = HTTPClient(
1732+
eventLoopGroupProvider: .shared(self.clientGroup),
1733+
configuration: .init(timeout: .init(connect: .milliseconds(100)))
1734+
)
1735+
defer { XCTAssertNoThrow(try localClient.syncShutdown()) }
17341736

17351737
XCTAssertThrowsError(try localClient.get(url: "http://localhost:\(port)").wait()) { error in
17361738
if isTestingNIOTS() {

Tests/AsyncHTTPClientTests/HTTPConnectionPool+FactoryTests.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ class HTTPConnectionPool_FactoryTests: XCTestCase {
7676
let factory = HTTPConnectionPool.ConnectionFactory(
7777
key: .init(request),
7878
tlsConfiguration: nil,
79-
clientConfiguration: .init(proxy: .socksServer(host: "127.0.0.1", port: server!.localAddress!.port!)),
79+
clientConfiguration: .init(
80+
timeout: .init(connect: .seconds(2), read: nil),
81+
proxy: .socksServer(host: "127.0.0.1", port: server!.localAddress!.port!)
82+
),
8083
sslContextCache: .init()
8184
)
8285

0 commit comments

Comments
 (0)