Skip to content

testPlatformConnectErrorIsForwardedOnTimeout port reuse #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Tests/AsyncHTTPClientTests/HTTP2ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ class HTTP2ClientTests: XCTestCase {
}

func testPlatformConnectErrorIsForwardedOnTimeout() {
let bin = HTTPBin(.http2(compress: false))
let bin = HTTPBin(.http2(compress: false), reusePort: true)
let clientGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)
let el1 = clientGroup.next()
let el2 = clientGroup.next()
Expand Down Expand Up @@ -404,20 +404,22 @@ class HTTP2ClientTests: XCTestCase {
XCTAssertEqual(.ok, response1?.status)
XCTAssertEqual(response1?.version, .http2)
let serverPort = bin.port
XCTAssertNoThrow(try bin.shutdown())
// client is now in HTTP/2 state and the HTTPBin is closed
// start a new server on the old port which closes all connections immediately

let serverGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try serverGroup.syncShutdownGracefully()) }
var maybeServer: Channel?
XCTAssertNoThrow(maybeServer = try ServerBootstrap(group: serverGroup)
.serverChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEPORT), value: 1)
.childChannelInitializer { channel in
channel.close()
}
.childChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.bind(host: "127.0.0.1", port: serverPort)
.wait())
// shutting down the old server closes all connections immediately
XCTAssertNoThrow(try bin.shutdown())
// client is now in HTTP/2 state and the HTTPBin is closed
guard let server = maybeServer else { return }
defer { XCTAssertNoThrow(try server.close().wait()) }

Expand Down
7 changes: 5 additions & 2 deletions Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ internal final class HTTPBin<RequestHandler: ChannelInboundHandler> where
_ mode: Mode = .http1_1(ssl: false, compress: false),
proxy: Proxy = .none,
bindTarget: BindTarget = .localhostIPv4RandomPort,
reusePort: Bool = false,
handlerFactory: @escaping (Int) -> (RequestHandler)
) {
self.mode = mode
Expand All @@ -460,6 +461,7 @@ internal final class HTTPBin<RequestHandler: ChannelInboundHandler> where

self.serverChannel = try! ServerBootstrap(group: self.group)
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEPORT), value: reusePort ? 1 : 0)
.serverChannelInitializer { channel in
channel.pipeline.addHandler(self.activeConnCounterHandler)
}.childChannelInitializer { channel in
Expand Down Expand Up @@ -642,9 +644,10 @@ extension HTTPBin where RequestHandler == HTTPBinHandler {
convenience init(
_ mode: Mode = .http1_1(ssl: false, compress: false),
proxy: Proxy = .none,
bindTarget: BindTarget = .localhostIPv4RandomPort
bindTarget: BindTarget = .localhostIPv4RandomPort,
reusePort: Bool = false
) {
self.init(mode, proxy: proxy, bindTarget: bindTarget) { HTTPBinHandler(connectionID: $0) }
self.init(mode, proxy: proxy, bindTarget: bindTarget, reusePort: reusePort) { HTTPBinHandler(connectionID: $0) }
}
}

Expand Down