From 9d2eaa48507e5b9fa99cf88f6ba452aed3c1bcc7 Mon Sep 17 00:00:00 2001 From: Artem Redkin Date: Sat, 13 Jun 2020 19:25:59 +0100 Subject: [PATCH 1/2] add callin tests --- .../HTTPClientTests+XCTest.swift | 1 + .../HTTPClientTests.swift | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift b/Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift index 7d61f805e..3e95a5879 100644 --- a/Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift +++ b/Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift @@ -109,6 +109,7 @@ extension HTTPClientTests { ("testNothingIsLoggedAtInfoOrHigher", testNothingIsLoggedAtInfoOrHigher), ("testAllMethodsLog", testAllMethodsLog), ("testClosingIdleConnectionsInPoolLogsInTheBackground", testClosingIdleConnectionsInPoolLogsInTheBackground), + ("testDelegateCallinsTolerateRandomEL", testDelegateCallinsTolerateRandomEL), ] } } diff --git a/Tests/AsyncHTTPClientTests/HTTPClientTests.swift b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift index 0e4e25d6e..b4f595765 100644 --- a/Tests/AsyncHTTPClientTests/HTTPClientTests.swift +++ b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift @@ -2005,4 +2005,50 @@ class HTTPClientTests: XCTestCase { self.defaultClient = nil // so it doesn't get shut down again. } + + func testDelegateCallinsTolerateRandomEL() throws { + class TestDelegate: HTTPClientResponseDelegate { + typealias Response = Void + let eventLoop: EventLoop + + init(eventLoop: EventLoop) { + self.eventLoop = eventLoop + } + + func didReceiveHead(task: HTTPClient.Task, _: HTTPResponseHead) -> EventLoopFuture { + self.eventLoop.makeSucceededFuture(()) + } + + func didReceiveBodyPart(task: HTTPClient.Task, _: ByteBuffer) -> EventLoopFuture { + self.eventLoop.makeSucceededFuture(()) + } + + func didFinishRequest(task: HTTPClient.Task) throws {} + } + + let elg = getDefaultEventLoopGroup(numberOfThreads: 3) + let first = elg.next() + let second = elg.next() + XCTAssertFalse(first === second) + + let httpServer = NIOHTTP1TestServer(group: first) + let httpClient = HTTPClient(eventLoopGroupProvider: .shared(first)) + defer { + XCTAssertNoThrow(try httpClient.syncShutdown()) + XCTAssertNoThrow(try httpServer.stop()) + } + + let delegate = TestDelegate(eventLoop: second) + let request = try HTTPClient.Request(url: "http://localhost:\(httpServer.serverPort)/") + let future = httpClient.execute(request: request, delegate: delegate) + + XCTAssertNoThrow(try httpServer.readInbound()) // .head + XCTAssertNoThrow(try httpServer.readInbound()) // .end + + XCTAssertNoThrow(try httpServer.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .ok)))) + XCTAssertNoThrow(try httpServer.writeOutbound(.body(.byteBuffer(ByteBuffer.of(string: "1234"))))) + XCTAssertNoThrow(try httpServer.writeOutbound(.end(nil))) + + XCTAssertNoThrow(try future.wait()) + } } From e759a53827bb877d7c3e29b999170361964f02e5 Mon Sep 17 00:00:00 2001 From: Artem Redkin Date: Sat, 13 Jun 2020 19:30:33 +0100 Subject: [PATCH 2/2] fix swift 5.0 compilation --- Tests/AsyncHTTPClientTests/HTTPClientTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/AsyncHTTPClientTests/HTTPClientTests.swift b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift index b4f595765..4cde4ab9f 100644 --- a/Tests/AsyncHTTPClientTests/HTTPClientTests.swift +++ b/Tests/AsyncHTTPClientTests/HTTPClientTests.swift @@ -2016,11 +2016,11 @@ class HTTPClientTests: XCTestCase { } func didReceiveHead(task: HTTPClient.Task, _: HTTPResponseHead) -> EventLoopFuture { - self.eventLoop.makeSucceededFuture(()) + return self.eventLoop.makeSucceededFuture(()) } func didReceiveBodyPart(task: HTTPClient.Task, _: ByteBuffer) -> EventLoopFuture { - self.eventLoop.makeSucceededFuture(()) + return self.eventLoop.makeSucceededFuture(()) } func didFinishRequest(task: HTTPClient.Task) throws {}