-
Notifications
You must be signed in to change notification settings - Fork 122
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
Add "debug initializer" hook for channels #801
base: main
Are you sure you want to change the base?
Changes from 2 commits
9e26b5e
f51ae1c
a323796
0d22e92
4847b6e
d47e756
fec1b80
77ffedf
134aebf
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,108 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass { | |
request.setBasicAuth(username: "foo", password: "bar") | ||
XCTAssertEqual(request.headers.first(name: "Authorization"), "Basic Zm9vOmJhcg==") | ||
} | ||
|
||
func testHTTP1ConnectionDebugInitializer() { | ||
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: connectionDebugInitializerUtil.initialize(channel:) | ||
) | ||
config.tlsConfiguration = .clientDefault | ||
config.tlsConfiguration?.certificateVerification = .none | ||
config.httpVersion = .http1Only | ||
|
||
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: true, compress: false)) | ||
defer { XCTAssertNoThrow(try bin.shutdown()) } | ||
|
||
for _ in 0..<3 { | ||
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.withLockedValue { $0 }, 1) | ||
} | ||
|
||
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: | ||
connectionDebugInitializerUtil.initialize(channel:), | ||
http2StreamChannelDebugInitializer: | ||
streamChannelDebugInitializerUtil.initialize(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.withLockedValue { $0 }, | ||
1 | ||
) | ||
|
||
// The stream channel debug initializer must be called only as much as the number of | ||
// requests made. | ||
XCTAssertEqual( | ||
streamChannelDebugInitializerUtil.executionCount.withLockedValue { $0 }, | ||
numberOfRequests | ||
) | ||
} | ||
} | ||
|
||
final class CountingDebugInitializerUtil: Sendable { | ||
let executionCount: NIOLockedValueBox<Int> | ||
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. A useful thing to do is to make this private and have a computed property which returns the value from the box; this makes it easier for any test which wants the value to get it without having to go via |
||
|
||
/// The acual debug initializer. | ||
clintonpi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Sendable | ||
glbrntt marked this conversation as resolved.
Show resolved
Hide resolved
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. I don't think we need this annotation as the type is 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. A warning is given for the implicit conversion. 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. I think that's because you do http1_1ConnectionDebugInitializer: { channel in
connectionDebugInitializerUtil.initialize(channel: channel)
} |
||
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably the initialiser also works when we're not using SSL? I'm not super familiar with this codebase so would be good to check that.