-
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 TestBackpressure test #273
Changes from 1 commit
9d8aa24
df778c5
19accd7
8611b61
84622f5
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 |
---|---|---|
|
@@ -2533,4 +2533,59 @@ class HTTPClientTests: XCTestCase { | |
XCTAssertEqual(info.connectionNumber, 1) | ||
XCTAssertEqual(info.requestNumber, 1) | ||
} | ||
|
||
func testBackpressue() { | ||
class BackpressureResponseDelegate: HTTPClientResponseDelegate { | ||
typealias Response = Void | ||
var count = 0 | ||
var processingBodyPart = false | ||
var didntWait = false | ||
var lock = Lock() | ||
|
||
init() { } | ||
|
||
func didReceiveHead(task: HTTPClient.Task<Response>, _ head: HTTPResponseHead) -> EventLoopFuture<Void> { | ||
return task.eventLoop.makeSucceededFuture(()) | ||
} | ||
|
||
func didReceiveBodyPart(task: HTTPClient.Task<Response>, _ part: ByteBuffer) -> EventLoopFuture<Void> { | ||
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. how are we making sure that 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. We aren’t at the moment. I can add a test to see if it did. Is there a way we can force it? 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. @adam-fowler From the NIO level: Yes, you could set The issue here is that through AHC's API, you can't easily mess with the underlying The other (probably easier) option is:
|
||
lock.withLock { | ||
// if processingBodyPart is true then previous body part is still being processed | ||
// XCTAssertEqual doesn't work here so store result to test later | ||
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. out of interest: What about 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. XCTAssertEqual doesn’t return an error while assert does not totally sure why 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. @adam-fowler yes, it continues the execution but it will still fail the test run once it's complete. So I think you can just use 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. In this situation it doesn't work. |
||
if processingBodyPart == true { | ||
didntWait = true | ||
} | ||
processingBodyPart = true | ||
count += 1 | ||
} | ||
// wait one second before returning a successful future | ||
return task.eventLoop.scheduleTask(in: .milliseconds(1000) ) { | ||
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. Waiting a second does force this test to take quite a long time. Is there any reason we think this test wouldn't work equally well with a delay of, say, 200ms instead of the full second? 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. Reduced to 200ms |
||
self.lock.withLock { | ||
self.processingBodyPart = false | ||
self.count -= 1 | ||
} | ||
}.futureResult | ||
Lukasa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func didReceiveError(task: HTTPClient.Task<Response>, _ error: Error) { } | ||
func didFinishRequest(task: HTTPClient.Task<Response>) throws { } | ||
} | ||
|
||
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 5) | ||
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. can't we use the 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. Then we don't need a group or a client 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 found a client that uses an elg with more than one thread is more likely to fail 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 we should get #177 in for that tbh. But your call. 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. Until #177 is merged I leave this using the local client |
||
let client = HTTPClient(eventLoopGroupProvider: .shared(elg)) | ||
defer { | ||
XCTAssertNoThrow(try client.syncShutdown()) | ||
XCTAssertNoThrow(try elg.syncShutdownGracefully()) | ||
} | ||
|
||
let data = Data(count: 65273) | ||
weissi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let backpressureResponseDelegate = BackpressureResponseDelegate() | ||
guard let request = try? HTTPClient.Request(url: self.defaultHTTPBinURLPrefix + "get", body: .data(data)) else { | ||
XCTFail("Failed to init Request") | ||
return | ||
} | ||
XCTAssertNoThrow(try client.execute(request: request, delegate: backpressureResponseDelegate).wait()) | ||
XCTAssertEqual(backpressureResponseDelegate.didntWait, false) | ||
XCTAssertEqual(backpressureResponseDelegate.count, 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.
Nit:
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.
oh dear