Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a waiter box for the connection pool #397

Merged
merged 3 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===----------------------------------------------------------------------===//
//
// 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 RequestID: Hashable {
private let objectIdentifier: ObjectIdentifier

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

struct Waiter {
var requestID: RequestID {
RequestID(self.request)
}

var request: HTTPScheduledRequest

private var eventLoopRequirement: EventLoop? {
switch self.request.eventLoopPreference.preference {
case .delegateAndChannel(on: let eventLoop),
.testOnly_exact(channelOn: let eventLoop, delegateOn: _):
return eventLoop
case .delegate(on: _),
.indifferent:
return nil
}
}

init(request: HTTPScheduledRequest) {
self.request = request
}

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
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2018-2019 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
//
//===----------------------------------------------------------------------===//
//
// HTTPConnectionPool+WaiterTests+XCTest.swift
//
import XCTest

///
/// NOTE: This file was generated by generate_linux_tests.rb
///
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
///

extension HTTPConnectionPool_WaiterTests {
static var allTests: [(String, (HTTPConnectionPool_WaiterTests) -> () throws -> Void)] {
return [
("testCanBeRunIfEventLoopIsSpecified", testCanBeRunIfEventLoopIsSpecified),
("testCanBeRunIfNoEventLoopIsSpecified", testCanBeRunIfNoEventLoopIsSpecified),
]
}
}
63 changes: 63 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPConnectionPool+WaiterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//===----------------------------------------------------------------------===//
//
// 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 mockRequest = MockScheduledRequest(eventLoopPreference: .init(.testOnly_exact(channelOn: theRightEL, delegateOn: theFalseEL)))

let waiter = HTTPConnectionPool.Waiter(request: mockRequest)

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

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

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

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

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

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

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

func fail(_: Error) {
preconditionFailure("Unimplemented")
}
}
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import XCTest
testCase(HTTPClientSOCKSTests.allTests),
testCase(HTTPClientTests.allTests),
testCase(HTTPConnectionPool_FactoryTests.allTests),
testCase(HTTPConnectionPool_WaiterTests.allTests),
testCase(HTTPRequestStateMachineTests.allTests),
testCase(LRUCacheTests.allTests),
testCase(RequestBagTests.allTests),
Expand Down