Skip to content

Commit f2bb283

Browse files
authored
Fix flaky tests in HTTPClientSOCKSTests (#498)
* bind to a port defined by the operating system
1 parent 3b18e76 commit f2bb283

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Tests/AsyncHTTPClientTests/HTTPClient+SOCKSTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ 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")))
79+
configuration: .init(proxy: .socksServer(host: "localhost", port: socksBin.port)))
8080

8181
defer {
8282
XCTAssertNoThrow(try localClient.syncShutdown())
@@ -125,7 +125,7 @@ class HTTPClientSOCKSTests: XCTestCase {
125125
func testProxySOCKSMisbehavingServer() throws {
126126
let socksBin = try MockSOCKSServer(expectedURL: "/socks/test", expectedResponse: "it works!", misbehave: true)
127127
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
128-
configuration: .init(proxy: .socksServer(host: "localhost")))
128+
configuration: .init(proxy: .socksServer(host: "localhost", port: socksBin.port)))
129129

130130
defer {
131131
XCTAssertNoThrow(try localClient.syncShutdown())

Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class TestSOCKSBadServerHandler: ChannelInboundHandler {
3636
class MockSOCKSServer {
3737
let channel: Channel
3838

39+
var port: Int {
40+
self.channel.localAddress!.port!
41+
}
42+
3943
init(expectedURL: String, expectedResponse: String, misbehave: Bool = false, file: String = #file, line: UInt = #line) throws {
4044
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
4145
let bootstrap: ServerBootstrap
@@ -57,7 +61,7 @@ class MockSOCKSServer {
5761
])
5862
}
5963
}
60-
self.channel = try bootstrap.bind(host: "localhost", port: 1080).wait()
64+
self.channel = try bootstrap.bind(host: "localhost", port: 0).wait()
6165
}
6266

6367
func shutdown() throws {

0 commit comments

Comments
 (0)