Skip to content

Commit

Permalink
Use a local TCP server that doesn’t accept connections on macOS for `…
Browse files Browse the repository at this point in the history
…testConnectTimeout()` (#592)

* Use a local TCP server that doesn’t accept connections on macOS for `testConnectTimeout()`

* fix linting
  • Loading branch information
dnadoba authored Jun 9, 2022
1 parent 2483e08 commit 0f21b44
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -640,17 +640,44 @@ class HTTPClientTests: XCTestCase {
}
}

func testConnectTimeout() {
func testConnectTimeout() throws {
#if os(Linux)
// 198.51.100.254 is reserved for documentation only and therefore should not accept any TCP connection
let url = "http://198.51.100.254/get"
#else
// on macOS we can use the TCP backlog behaviour when the queue is full to simulate a non reachable server.
// this makes this test a bit more stable if `198.51.100.254` actually responds to connection attempt.
// The backlog behaviour on Linux can not be used to simulate a non-reachable server.
// Linux sends a `SYN/ACK` back even if the `backlog` queue is full as it has two queues.
// The second queue is not limit by `ChannelOptions.backlog` but by `/proc/sys/net/ipv4/tcp_max_syn_backlog`.

let serverChannel = try ServerBootstrap(group: self.serverGroup)
.serverChannelOption(ChannelOptions.backlog, value: 1)
.serverChannelOption(ChannelOptions.autoRead, value: false)
.bind(host: "127.0.0.1", port: 0)
.wait()
defer {
XCTAssertNoThrow(try serverChannel.close().wait())
}
let port = serverChannel.localAddress!.port!
let firstClientChannel = try ClientBootstrap(group: self.serverGroup)
.connect(host: "127.0.0.1", port: port)
.wait()
defer {
XCTAssertNoThrow(try firstClientChannel.close().wait())
}
let url = "http://localhost:\(port)/get"
#endif

let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
configuration: .init(timeout: .init(connect: .milliseconds(100), read: .milliseconds(150))))

defer {
XCTAssertNoThrow(try httpClient.syncShutdown())
}

// This must throw as 198.51.100.254 is reserved for documentation only
XCTAssertThrowsError(try httpClient.get(url: "http://198.51.100.254/get").wait()) {
XCTAssertEqual($0 as? HTTPClientError, .connectTimeout)
XCTAssertThrowsError(try httpClient.get(url: url).wait()) {
XCTAssertEqualTypeAndValue($0, HTTPClientError.connectTimeout)
}
}

Expand Down

0 comments on commit 0f21b44

Please sign in to comment.