Skip to content

Commit 834d0d5

Browse files
committed
Code review
1 parent 6f446fe commit 834d0d5

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

Sources/AsyncHTTPClient/ConnectionPool/HTTPConnectionPool+Waiter.swift

+24-6
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ extension HTTPConnectionPool {
2424
}
2525
}
2626

27-
let id: ID
28-
let request: HTTPScheduledRequest
29-
let eventLoopRequirement: EventLoop?
27+
var id: ID {
28+
ID(self.request)
29+
}
30+
31+
var request: HTTPScheduledRequest {
32+
didSet {
33+
self.updateEventLoopRequirement()
34+
}
35+
}
3036

31-
init(request: HTTPScheduledRequest, eventLoopRequirement: EventLoop?) {
32-
self.id = ID(request)
37+
private var eventLoopRequirement: EventLoop?
38+
39+
init(request: HTTPScheduledRequest) {
3340
self.request = request
34-
self.eventLoopRequirement = eventLoopRequirement
41+
self.updateEventLoopRequirement()
3542
}
3643

3744
func canBeRun(on option: EventLoop) -> Bool {
@@ -42,5 +49,16 @@ extension HTTPConnectionPool {
4249

4350
return requirement === option
4451
}
52+
53+
private mutating func updateEventLoopRequirement() {
54+
switch self.request.eventLoopPreference.preference {
55+
case .delegateAndChannel(on: let eventLoop),
56+
.testOnly_exact(channelOn: let eventLoop, delegateOn: _):
57+
self.eventLoopRequirement = eventLoop
58+
case .delegate(on: _),
59+
.indifferent:
60+
self.eventLoopRequirement = nil
61+
}
62+
}
4563
}
4664
}

Tests/AsyncHTTPClientTests/HTTPConnectionPool+WaiterTests.swift

+9-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ class HTTPConnectionPool_WaiterTests: XCTestCase {
2424
let theRightEL = eventLoopGroup.next()
2525
let theFalseEL = eventLoopGroup.next()
2626

27-
let waiter = HTTPConnectionPool.Waiter(request: MockScheduledRequest(), eventLoopRequirement: theRightEL)
27+
let mockRequest = MockScheduledRequest(eventLoopPreference: .init(.testOnly_exact(channelOn: theRightEL, delegateOn: theFalseEL)))
28+
29+
let waiter = HTTPConnectionPool.Waiter(request: mockRequest)
2830

2931
XCTAssertTrue(waiter.canBeRun(on: theRightEL))
3032
XCTAssertFalse(waiter.canBeRun(on: theFalseEL))
@@ -33,7 +35,8 @@ class HTTPConnectionPool_WaiterTests: XCTestCase {
3335
func testCanBeRunIfNoEventLoopIsSpecified() {
3436
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)
3537

36-
let waiter = HTTPConnectionPool.Waiter(request: MockScheduledRequest(), eventLoopRequirement: nil)
38+
let mockRequest = MockScheduledRequest(eventLoopPreference: .indifferent)
39+
let waiter = HTTPConnectionPool.Waiter(request: mockRequest)
3740

3841
for el in eventLoopGroup.makeIterator() {
3942
XCTAssertTrue(waiter.canBeRun(on: el))
@@ -42,11 +45,13 @@ class HTTPConnectionPool_WaiterTests: XCTestCase {
4245
}
4346

4447
private class MockScheduledRequest: HTTPScheduledRequest {
45-
init() {}
48+
init(eventLoopPreference: HTTPClient.EventLoopPreference) {
49+
self.eventLoopPreference = eventLoopPreference
50+
}
4651

4752
var logger: Logger { preconditionFailure("Unimplemented") }
4853
var connectionDeadline: NIODeadline { preconditionFailure("Unimplemented") }
49-
var eventLoopPreference: HTTPClient.EventLoopPreference { preconditionFailure("Unimplemented") }
54+
let eventLoopPreference: HTTPClient.EventLoopPreference
5055

5156
func requestWasQueued(_: HTTPRequestScheduler) {
5257
preconditionFailure("Unimplemented")

0 commit comments

Comments
 (0)