Skip to content

Commit

Permalink
Add a waiter box for the connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfett committed Jul 7, 2021
1 parent bccb075 commit 3980b20
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import NIO

extension HTTPConnectionPool {
struct Waiter {
struct ID: Hashable {
private let objectIdentifier: ObjectIdentifier

init(_ request: HTTPScheduledRequest) {
self.objectIdentifier = ObjectIdentifier(request)
}
}

let id: ID
let request: HTTPScheduledRequest
let eventLoopRequirement: EventLoop?

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

func canBeRun(on option: EventLoop) -> Bool {
guard let requirement = self.eventLoopRequirement else {
// if no requirement exists we can run on any EventLoop
return true
}

return requirement === option
}
}
}
58 changes: 58 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPConnectionPool+WaiterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

@testable import AsyncHTTPClient
import Logging
import NIO
import XCTest

class HTTPConnectionPool_WaiterTests: XCTestCase {
func testCanBeRunIfEventLoopIsSpecified() {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)

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

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

XCTAssertTrue(waiter.canBeRun(on: theRightEL))
XCTAssertFalse(waiter.canBeRun(on: theFalseEL))
}

func testCanBeRunIfNoEventLoopIsSpecified() {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)

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

for el in eventLoopGroup.makeIterator() {
XCTAssertTrue(waiter.canBeRun(on: el))
}
}
}

private class MockScheduledRequest: HTTPScheduledRequest {
init() {}

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

func requestWasQueued(_: HTTPRequestScheduler) {
preconditionFailure("Unimplemented")
}

func fail(_: Error) {
preconditionFailure("Unimplemented")
}
}

0 comments on commit 3980b20

Please sign in to comment.