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
lettask=Task<T.Response>(eventLoop: eventLoop)
125125

126-
varbootstrap=ClientBootstrap(group:self.eventLoopGroup)
126+
varbootstrap=ClientBootstrap(group:eventLoop)
127127
.channelOption(ChannelOptions.socket(SocketOptionLevel(IPPROTO_TCP), TCP_NODELAY), value:1)
128128
.channelInitializer{ channel in
129129
letencoder=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
returnself.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
letresponse=try httpClient.get(url:"http://localhost:\(httpBin.port)/get").wait()
4545
XCTAssertEqual(.ok, response.status)
4646
}
47+
48+
func testGetWithSharedEventLoopGroup()throws{
49+
lethttpBin=HttpBin()
50+
letelg=MultiThreadedEventLoopGroup(numberOfThreads:8)
51+
lethttpClient=HTTPClient(eventLoopGroupProvider:.shared(elg))
52+
defer{
53+
try! elg.syncShutdownGracefully()
54+
httpBin.shutdown()
55+
}
56+
57+
letdelegate=TestHTTPDelegate()
58+
letrequest=tryHTTPClient.Request(url:"http://localhost:\(httpBin.port)/events/10/1")
59+
lettask= httpClient.execute(request: request, delegate: delegate)
60+
letexpectedEventLoop= task.eventLoop
61+
task.futureResult.whenComplete{(_)in
62+
XCTAssertTrue(expectedEventLoop.inEventLoop)
63+
}
64+
try task.wait()
65+
}
4766

4867
func testPost()throws{
4968
lethttpBin=HttpBin()

0 commit comments

Comments
(0)