-
Notifications
You must be signed in to change notification settings - Fork 125
Add "debug initializer" hook for channels #801
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
Changes from 4 commits
9e26b5e
f51ae1c
a323796
0d22e92
4847b6e
d47e756
fec1b80
77ffedf
134aebf
003f9d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4306,4 +4306,119 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass { | |
request.setBasicAuth(username: "foo", password: "bar") | ||
XCTAssertEqual(request.headers.first(name: "Authorization"), "Basic Zm9vOmJhcg==") | ||
} | ||
|
||
func runBaseTestForHTTP1ConnectionDebugInitializer(ssl: Bool) { | ||
let connectionDebugInitializerUtil = CountingDebugInitializerUtil() | ||
|
||
// Initializing even with just `http1_1ConnectionDebugInitializer` (rather than manually | ||
// modifying `config`) to ensure that the matching `init` actually wires up this argument | ||
// with the respective property. This is necessary as these parameters are defaulted and can | ||
// be easy to miss. | ||
var config = HTTPClient.Configuration( | ||
http1_1ConnectionDebugInitializer: { channel in | ||
return connectionDebugInitializerUtil.initialize(channel: channel) | ||
} | ||
) | ||
config.httpVersion = .http1Only | ||
|
||
if ssl { | ||
config.tlsConfiguration = .clientDefault | ||
config.tlsConfiguration?.certificateVerification = .none | ||
} | ||
|
||
let client = HTTPClient( | ||
eventLoopGroupProvider: .singleton, | ||
configuration: config, | ||
backgroundActivityLogger: Logger( | ||
label: "HTTPClient", | ||
factory: StreamLogHandler.standardOutput(label:) | ||
) | ||
) | ||
defer { XCTAssertNoThrow(client.shutdown()) } | ||
|
||
let bin = HTTPBin(.http1_1(ssl: ssl, compress: false)) | ||
defer { XCTAssertNoThrow(try bin.shutdown()) } | ||
|
||
let scheme = ssl ? "https" : "http" | ||
|
||
for _ in 0..<3 { | ||
XCTAssertNoThrow(try client.get(url: "\(scheme)://localhost:\(bin.port)/get").wait()) | ||
} | ||
|
||
// Even though multiple requests were made, the connection debug initializer must be called | ||
// only once. | ||
XCTAssertEqual(connectionDebugInitializerUtil.executionCount, 1) | ||
} | ||
|
||
func testHTTP1PlainTextConnectionDebugInitializer() { | ||
runBaseTestForHTTP1ConnectionDebugInitializer(ssl: false) | ||
} | ||
|
||
func testHTTP1EncryptedConnectionDebugInitializer() { | ||
runBaseTestForHTTP1ConnectionDebugInitializer(ssl: true) | ||
} | ||
|
||
func testHTTP2ConnectionAndStreamChannelDebugInitializers() { | ||
let connectionDebugInitializerUtil = CountingDebugInitializerUtil() | ||
let streamChannelDebugInitializerUtil = CountingDebugInitializerUtil() | ||
|
||
// Initializing even with just `http2ConnectionDebugInitializer` and | ||
// `http2StreamChannelDebugInitializer` (rather than manually modifying `config`) to ensure | ||
// that the matching `init` actually wires up these arguments with the respective | ||
// properties. This is necessary as these parameters are defaulted and can be easy to miss. | ||
var config = HTTPClient.Configuration( | ||
http2ConnectionDebugInitializer: { channel in | ||
return connectionDebugInitializerUtil.initialize(channel: channel) | ||
}, | ||
http2StreamChannelDebugInitializer: { channel in | ||
return streamChannelDebugInitializerUtil.initialize(channel: channel) | ||
} | ||
) | ||
config.tlsConfiguration = .clientDefault | ||
config.tlsConfiguration?.certificateVerification = .none | ||
config.httpVersion = .automatic | ||
|
||
let client = HTTPClient( | ||
eventLoopGroupProvider: .singleton, | ||
configuration: config, | ||
backgroundActivityLogger: Logger( | ||
label: "HTTPClient", | ||
factory: StreamLogHandler.standardOutput(label:) | ||
) | ||
) | ||
defer { XCTAssertNoThrow(client.shutdown()) } | ||
|
||
let bin = HTTPBin(.http2(compress: false)) | ||
defer { XCTAssertNoThrow(try bin.shutdown()) } | ||
|
||
let numberOfRequests = 3 | ||
|
||
for _ in 0..<numberOfRequests { | ||
XCTAssertNoThrow(try client.get(url: "https://localhost:\(bin.port)/get").wait()) | ||
} | ||
|
||
// Even though multiple requests were made, the connection debug initializer must be called | ||
// only once. | ||
XCTAssertEqual(connectionDebugInitializerUtil.executionCount, 1) | ||
|
||
// The stream channel debug initializer must be called only as much as the number of | ||
// requests made. | ||
XCTAssertEqual(streamChannelDebugInitializerUtil.executionCount, numberOfRequests) | ||
} | ||
} | ||
|
||
final class CountingDebugInitializerUtil: Sendable { | ||
private let _executionCount: NIOLockedValueBox<Int> | ||
var executionCount: Int { self._executionCount.withLockedValue { $0 } } | ||
|
||
/// The acual debug initializer. | ||
func initialize(channel: Channel) -> EventLoopFuture<Void> { | ||
self._executionCount.withLockedValue { $0 += 1 } | ||
|
||
return channel.eventLoop.makeSucceededVoidFuture() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be useful to add some tests that have the debug initializer actually delay the setup. That is, make sure that we actually do wait for this to complete. Easiest thing to do might be to set some low timeouts, and then confirm the timeouts fire, and that completing the promise doesn't then cause any issues. |
||
} | ||
|
||
init() { | ||
self._executionCount = .init(0) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.