-
Notifications
You must be signed in to change notification settings - Fork 126
Fix bi directional streaming test #405
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
fabianfett
merged 2 commits into
swift-server:main
from
fabianfett:ff-fix-bi-directional-streaming-test
Aug 12, 2021
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2815,40 +2815,65 @@ class HTTPClientTests: XCTestCase { | |
XCTAssertEqual(result, .success, "we never closed the connection!") | ||
} | ||
|
||
func testBiDirectionalStreaming() throws { | ||
let handler = HTTPEchoHandler() | ||
// In this test, we test that a request can continue to stream its body after the response head, | ||
// was received. The client sends a number to the server and waits for the server to echo the | ||
// number. Once the client receives the echoed number, it will continue with the next number. | ||
// The client and server ping/pong 30 times. | ||
func testBiDirectionalStreaming() { | ||
let httpBin = HTTPBin(.http1_1(ssl: false, compress: false)) { _ in HTTPEchoHandler() } | ||
defer { XCTAssertNoThrow(try httpBin.shutdown()) } | ||
|
||
let server = try ServerBootstrap(group: self.serverGroup) | ||
.serverChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1) | ||
.childChannelInitializer { channel in | ||
channel.pipeline.configureHTTPServerPipeline().flatMap { | ||
channel.pipeline.addHandler(handler) | ||
} | ||
} | ||
.bind(host: "localhost", port: 0) | ||
.wait() | ||
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2) | ||
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } | ||
let writeEL = eventLoopGroup.next() | ||
let delegateEL = eventLoopGroup.next() | ||
|
||
defer { | ||
server.close(promise: nil) | ||
} | ||
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup)) | ||
defer { XCTAssertNoThrow(try httpClient.syncShutdown()) } | ||
|
||
let delegate = StreamDelegate(eventLoop: delegateEL) | ||
|
||
let body: HTTPClient.Body = .stream { writer in | ||
let promise = self.clientGroup.next().makePromise(of: Void.self) | ||
handler.promises.append(promise) | ||
return writer.write(.byteBuffer(ByteBuffer(string: "hello"))).flatMap { | ||
promise.futureResult | ||
}.flatMap { | ||
let promise = self.clientGroup.next().makePromise(of: Void.self) | ||
handler.promises.append(promise) | ||
return writer.write(.byteBuffer(ByteBuffer(string: "hello2"))).flatMap { | ||
promise.futureResult | ||
let finalPromise = writeEL.makePromise(of: Void.self) | ||
|
||
func writeLoop(_ writer: HTTPClient.Body.StreamWriter, index: Int) { | ||
XCTAssert(writeEL.inEventLoop, "Always write from unexpected el") | ||
|
||
if index >= 30 { | ||
return finalPromise.succeed(()) | ||
} | ||
|
||
let sent = ByteBuffer(integer: index) | ||
writer.write(.byteBuffer(sent)).flatMap { () -> EventLoopFuture<ByteBuffer?> in | ||
XCTAssert(delegateEL.inEventLoop, "Always dispatch back to delegate el") | ||
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. Same note here about false positives. |
||
return delegate.next() | ||
}.whenComplete { result in | ||
switch result { | ||
case .success(let returned): | ||
XCTAssertEqual(returned, sent) | ||
|
||
writeEL.execute { | ||
writeLoop(writer, index: index + 1) | ||
} | ||
|
||
case .failure(let error): | ||
finalPromise.fail(error) | ||
} | ||
} | ||
} | ||
|
||
writeEL.execute { | ||
writeLoop(writer, index: 0) | ||
} | ||
|
||
return finalPromise.futureResult | ||
} | ||
|
||
let future = self.defaultClient.execute(url: "http://localhost:\(server.localAddress!.port!)", body: body) | ||
let request = try! HTTPClient.Request(url: "http://localhost:\(httpBin.port)", body: body) | ||
let future = httpClient.execute(request: request, delegate: delegate, eventLoop: .delegate(on: delegateEL)) | ||
|
||
XCTAssertNoThrow(try future.wait()) | ||
XCTAssertNil(try delegate.next().wait()) | ||
} | ||
|
||
func testSynchronousHandshakeErrorReporting() throws { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we remove this assertion?
inEventLoop
is validly allowed to return false negatives, so we shouldn't assert that it won't. We can addpreconditionInEventLoop
instead if you want the check.