Skip to content

Commit daf66bd

Browse files
t089weissi
authored andcommitted
Make sure that event loop of task is the same as event loop of channel (#55)
1 parent 27fe926 commit daf66bd

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

Sources/AsyncHTTPClient/HTTPClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public class HTTPClient {
123123

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

126-
var bootstrap = ClientBootstrap(group: self.eventLoopGroup)
126+
var bootstrap = ClientBootstrap(group: eventLoop)
127127
.channelOption(ChannelOptions.socket(SocketOptionLevel(IPPROTO_TCP), TCP_NODELAY), value: 1)
128128
.channelInitializer { channel in
129129
let encoder = HTTPRequestEncoder()

Sources/AsyncHTTPClient/HTTPHandler.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ extension HTTPClient {
271271
}
272272

273273
func setChannel(_ channel: Channel) -> Channel {
274+
precondition(self.eventLoop === channel.eventLoop, "Channel must use same event loop as this task.")
274275
return self.lock.withLock {
275276
self.channel = channel
276277
return channel

Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extension HTTPClientTests {
2727
return [
2828
("testRequestURI", testRequestURI),
2929
("testGet", testGet),
30+
("testGetWithSharedEventLoopGroup", testGetWithSharedEventLoopGroup),
3031
("testPost", testPost),
3132
("testGetHttps", testGetHttps),
3233
("testPostHttps", testPostHttps),

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ class HTTPClientTests: XCTestCase {
4444
let response = try httpClient.get(url: "http://localhost:\(httpBin.port)/get").wait()
4545
XCTAssertEqual(.ok, response.status)
4646
}
47+
48+
func testGetWithSharedEventLoopGroup() throws {
49+
let httpBin = HttpBin()
50+
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 8)
51+
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(elg))
52+
defer {
53+
try! elg.syncShutdownGracefully()
54+
httpBin.shutdown()
55+
}
56+
57+
let delegate = TestHTTPDelegate()
58+
let request = try HTTPClient.Request(url: "http://localhost:\(httpBin.port)/events/10/1")
59+
let task = httpClient.execute(request: request, delegate: delegate)
60+
let expectedEventLoop = task.eventLoop
61+
task.futureResult.whenComplete { (_) in
62+
XCTAssertTrue(expectedEventLoop.inEventLoop)
63+
}
64+
try task.wait()
65+
}
4766

4867
func testPost() throws {
4968
let httpBin = HttpBin()

0 commit comments

Comments
 (0)