Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfett committed Jul 8, 2021
1 parent eac6731 commit 0266fef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ extension HTTPConnectionPool {
}
}

let id: ID
let request: HTTPScheduledRequest
let eventLoopRequirement: EventLoop?
var id: ID {
ID(request)
}

var request: HTTPScheduledRequest {
didSet {
self.updateEventLoopRequirement()
}
}
private var eventLoopRequirement: EventLoop?

init(request: HTTPScheduledRequest, eventLoopRequirement: EventLoop?) {
self.id = ID(request)
init(request: HTTPScheduledRequest) {
self.request = request
self.eventLoopRequirement = eventLoopRequirement
self.updateEventLoopRequirement()
}

func canBeRun(on option: EventLoop) -> Bool {
Expand All @@ -42,5 +48,16 @@ extension HTTPConnectionPool {

return requirement === option
}

private mutating func updateEventLoopRequirement() {
switch request.eventLoopPreference.preference {
case .delegateAndChannel(on: let eventLoop),
.testOnly_exact(channelOn: let eventLoop, delegateOn: _):
self.eventLoopRequirement = eventLoop
case .delegate(on: _),
.indifferent:
self.eventLoopRequirement = nil
}
}
}
}
13 changes: 9 additions & 4 deletions Tests/AsyncHTTPClientTests/HTTPConnectionPool+WaiterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class HTTPConnectionPool_WaiterTests: XCTestCase {

let theRightEL = eventLoopGroup.next()
let theFalseEL = eventLoopGroup.next()

let mockRequest = MockScheduledRequest(eventLoopPreference: .init(.testOnly_exact(channelOn: theRightEL, delegateOn: theFalseEL)))

let waiter = HTTPConnectionPool.Waiter(request: MockScheduledRequest(), eventLoopRequirement: theRightEL)
let waiter = HTTPConnectionPool.Waiter(request: mockRequest)

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

let waiter = HTTPConnectionPool.Waiter(request: MockScheduledRequest(), eventLoopRequirement: nil)
let mockRequest = MockScheduledRequest(eventLoopPreference: .indifferent)
let waiter = HTTPConnectionPool.Waiter(request: mockRequest)

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

private class MockScheduledRequest: HTTPScheduledRequest {
init() {}
init(eventLoopPreference: HTTPClient.EventLoopPreference) {
self.eventLoopPreference = eventLoopPreference
}

var logger: Logger { preconditionFailure("Unimplemented") }
var connectionDeadline: NIODeadline { preconditionFailure("Unimplemented") }
var eventLoopPreference: HTTPClient.EventLoopPreference { preconditionFailure("Unimplemented") }
let eventLoopPreference: HTTPClient.EventLoopPreference

func requestWasQueued(_: HTTPRequestScheduler) {
preconditionFailure("Unimplemented")
Expand Down

0 comments on commit 0266fef

Please sign in to comment.