Skip to content

Commit 6f446fe

Browse files
committed
Add a waiter box for the connection pool
1 parent ed44283 commit 6f446fe

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-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,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the AsyncHTTPClient open source project
4+
//
5+
// Copyright (c) 2018-2019 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+
// HTTPConnectionPool+WaiterTests+XCTest.swift
16+
//
17+
import XCTest
18+
19+
///
20+
/// NOTE: This file was generated by generate_linux_tests.rb
21+
///
22+
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
23+
///
24+
25+
extension HTTPConnectionPool_WaiterTests {
26+
static var allTests: [(String, (HTTPConnectionPool_WaiterTests) -> () throws -> Void)] {
27+
return [
28+
("testCanBeRunIfEventLoopIsSpecified", testCanBeRunIfEventLoopIsSpecified),
29+
("testCanBeRunIfNoEventLoopIsSpecified", testCanBeRunIfNoEventLoopIsSpecified),
30+
]
31+
}
32+
}
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+
}

Tests/LinuxMain.swift

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import XCTest
3636
testCase(HTTPClientSOCKSTests.allTests),
3737
testCase(HTTPClientTests.allTests),
3838
testCase(HTTPConnectionPool_FactoryTests.allTests),
39+
testCase(HTTPConnectionPool_WaiterTests.allTests),
3940
testCase(HTTPRequestStateMachineTests.allTests),
4041
testCase(LRUCacheTests.allTests),
4142
testCase(RequestBagTests.allTests),

0 commit comments

Comments
 (0)