Skip to content

Commit 3980b20

Browse files
committed
Add a waiter box for the connection pool
1 parent bccb075 commit 3980b20

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the AsyncHTTPClient open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import NIO
16+
17+
extension HTTPConnectionPool {
18+
struct Waiter {
19+
struct ID: Hashable {
20+
private let objectIdentifier: ObjectIdentifier
21+
22+
init(_ request: HTTPScheduledRequest) {
23+
self.objectIdentifier = ObjectIdentifier(request)
24+
}
25+
}
26+
27+
let id: ID
28+
let request: HTTPScheduledRequest
29+
let eventLoopRequirement: EventLoop?
30+
31+
init(request: HTTPScheduledRequest, eventLoopRequirement: EventLoop?) {
32+
self.id = ID(request)
33+
self.request = request
34+
self.eventLoopRequirement = eventLoopRequirement
35+
}
36+
37+
func canBeRun(on option: EventLoop) -> Bool {
38+
guard let requirement = self.eventLoopRequirement else {
39+
// if no requirement exists we can run on any EventLoop
40+
return true
41+
}
42+
43+
return requirement === option
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the AsyncHTTPClient open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
@testable import AsyncHTTPClient
16+
import Logging
17+
import NIO
18+
import XCTest
19+
20+
class HTTPConnectionPool_WaiterTests: XCTestCase {
21+
func testCanBeRunIfEventLoopIsSpecified() {
22+
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)
23+
24+
let theRightEL = eventLoopGroup.next()
25+
let theFalseEL = eventLoopGroup.next()
26+
27+
let waiter = HTTPConnectionPool.Waiter(request: MockScheduledRequest(), eventLoopRequirement: theRightEL)
28+
29+
XCTAssertTrue(waiter.canBeRun(on: theRightEL))
30+
XCTAssertFalse(waiter.canBeRun(on: theFalseEL))
31+
}
32+
33+
func testCanBeRunIfNoEventLoopIsSpecified() {
34+
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)
35+
36+
let waiter = HTTPConnectionPool.Waiter(request: MockScheduledRequest(), eventLoopRequirement: nil)
37+
38+
for el in eventLoopGroup.makeIterator() {
39+
XCTAssertTrue(waiter.canBeRun(on: el))
40+
}
41+
}
42+
}
43+
44+
private class MockScheduledRequest: HTTPScheduledRequest {
45+
init() {}
46+
47+
var logger: Logger { preconditionFailure("Unimplemented") }
48+
var connectionDeadline: NIODeadline { preconditionFailure("Unimplemented") }
49+
var eventLoopPreference: HTTPClient.EventLoopPreference { preconditionFailure("Unimplemented") }
50+
51+
func requestWasQueued(_: HTTPRequestScheduler) {
52+
preconditionFailure("Unimplemented")
53+
}
54+
55+
func fail(_: Error) {
56+
preconditionFailure("Unimplemented")
57+
}
58+
}

0 commit comments

Comments
 (0)