Skip to content

Commit 5e3b77b

Browse files
authored
add callin tests (#246)
1 parent 238c653 commit 5e3b77b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

Diff for: Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ extension HTTPClientTests {
109109
("testNothingIsLoggedAtInfoOrHigher", testNothingIsLoggedAtInfoOrHigher),
110110
("testAllMethodsLog", testAllMethodsLog),
111111
("testClosingIdleConnectionsInPoolLogsInTheBackground", testClosingIdleConnectionsInPoolLogsInTheBackground),
112+
("testDelegateCallinsTolerateRandomEL", testDelegateCallinsTolerateRandomEL),
112113
]
113114
}
114115
}

Diff for: Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+46
Original file line numberDiff line numberDiff line change
@@ -2005,4 +2005,50 @@ class HTTPClientTests: XCTestCase {
20052005

20062006
self.defaultClient = nil // so it doesn't get shut down again.
20072007
}
2008+
2009+
func testDelegateCallinsTolerateRandomEL() throws {
2010+
class TestDelegate: HTTPClientResponseDelegate {
2011+
typealias Response = Void
2012+
let eventLoop: EventLoop
2013+
2014+
init(eventLoop: EventLoop) {
2015+
self.eventLoop = eventLoop
2016+
}
2017+
2018+
func didReceiveHead(task: HTTPClient.Task<Void>, _: HTTPResponseHead) -> EventLoopFuture<Void> {
2019+
return self.eventLoop.makeSucceededFuture(())
2020+
}
2021+
2022+
func didReceiveBodyPart(task: HTTPClient.Task<Void>, _: ByteBuffer) -> EventLoopFuture<Void> {
2023+
return self.eventLoop.makeSucceededFuture(())
2024+
}
2025+
2026+
func didFinishRequest(task: HTTPClient.Task<Void>) throws {}
2027+
}
2028+
2029+
let elg = getDefaultEventLoopGroup(numberOfThreads: 3)
2030+
let first = elg.next()
2031+
let second = elg.next()
2032+
XCTAssertFalse(first === second)
2033+
2034+
let httpServer = NIOHTTP1TestServer(group: first)
2035+
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(first))
2036+
defer {
2037+
XCTAssertNoThrow(try httpClient.syncShutdown())
2038+
XCTAssertNoThrow(try httpServer.stop())
2039+
}
2040+
2041+
let delegate = TestDelegate(eventLoop: second)
2042+
let request = try HTTPClient.Request(url: "http://localhost:\(httpServer.serverPort)/")
2043+
let future = httpClient.execute(request: request, delegate: delegate)
2044+
2045+
XCTAssertNoThrow(try httpServer.readInbound()) // .head
2046+
XCTAssertNoThrow(try httpServer.readInbound()) // .end
2047+
2048+
XCTAssertNoThrow(try httpServer.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .ok))))
2049+
XCTAssertNoThrow(try httpServer.writeOutbound(.body(.byteBuffer(ByteBuffer.of(string: "1234")))))
2050+
XCTAssertNoThrow(try httpServer.writeOutbound(.end(nil)))
2051+
2052+
XCTAssertNoThrow(try future.wait())
2053+
}
20082054
}

0 commit comments

Comments
 (0)