Skip to content

Make sure that event loop of task is the same as event loop of channel #55

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
merged 1 commit into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/AsyncHTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public class HTTPClient {

let task = Task<T.Response>(eventLoop: eventLoop)

var bootstrap = ClientBootstrap(group: self.eventLoopGroup)
var bootstrap = ClientBootstrap(group: eventLoop)
.channelOption(ChannelOptions.socket(SocketOptionLevel(IPPROTO_TCP), TCP_NODELAY), value: 1)
.channelInitializer { channel in
let encoder = HTTPRequestEncoder()
Expand Down
1 change: 1 addition & 0 deletions Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ extension HTTPClient {
}

func setChannel(_ channel: Channel) -> Channel {
precondition(self.eventLoop === channel.eventLoop, "Channel must use same event loop as this task.")
return self.lock.withLock {
self.channel = channel
return channel
Expand Down
1 change: 1 addition & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extension HTTPClientTests {
return [
("testRequestURI", testRequestURI),
("testGet", testGet),
("testGetWithSharedEventLoopGroup", testGetWithSharedEventLoopGroup),
("testPost", testPost),
("testGetHttps", testGetHttps),
("testPostHttps", testPostHttps),
Expand Down
19 changes: 19 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ class HTTPClientTests: XCTestCase {
let response = try httpClient.get(url: "http://localhost:\(httpBin.port)/get").wait()
XCTAssertEqual(.ok, response.status)
}

func testGetWithSharedEventLoopGroup() throws {
let httpBin = HttpBin()
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 8)
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(elg))
defer {
try! elg.syncShutdownGracefully()
httpBin.shutdown()
}

let delegate = TestHTTPDelegate()
let request = try HTTPClient.Request(url: "http://localhost:\(httpBin.port)/events/10/1")
let task = httpClient.execute(request: request, delegate: delegate)
let expectedEventLoop = task.eventLoop
task.futureResult.whenComplete { (_) in
XCTAssertTrue(expectedEventLoop.inEventLoop)
}
try task.wait()
}

func testPost() throws {
let httpBin = HttpBin()
Expand Down