Skip to content

Commit 6492685

Browse files
committed
Add HTTPConnectionPool.Waiter
1 parent d949734 commit 6492685

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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(_ task: HTTPRequestTask) {
23+
self.objectIdentifier = ObjectIdentifier(task)
24+
}
25+
}
26+
27+
let id: ID
28+
let task: HTTPRequestTask
29+
let eventLoopRequirement: EventLoop?
30+
31+
init(task: HTTPRequestTask, eventLoopRequirement: EventLoop?) {
32+
self.id = ID(task)
33+
self.task = task
34+
self.eventLoopRequirement = eventLoopRequirement
35+
}
36+
37+
func canBeRun(on option: EventLoop) -> Bool {
38+
guard let requirement = self.eventLoopRequirement else {
39+
return true
40+
}
41+
42+
return requirement === option
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 NIOHTTP1
19+
20+
class MockHTTPRequestTask: HTTPRequestTask {
21+
let eventLoopPreference: HTTPClient.EventLoopPreference
22+
let logger: Logger
23+
let connectionDeadline: NIODeadline
24+
let idleReadTimeout: TimeAmount?
25+
26+
init(eventLoop: EventLoop,
27+
logger: Logger = Logger(label: "mock"),
28+
connectionTimeout: TimeAmount = .seconds(60),
29+
idleReadTimeout: TimeAmount? = nil,
30+
requiresEventLoopForChannel: Bool = false) {
31+
self.logger = logger
32+
33+
self.connectionDeadline = .now() + connectionTimeout
34+
self.idleReadTimeout = idleReadTimeout
35+
36+
if requiresEventLoopForChannel {
37+
self.eventLoopPreference = .delegateAndChannel(on: eventLoop)
38+
} else {
39+
self.eventLoopPreference = .delegate(on: eventLoop)
40+
}
41+
}
42+
43+
var eventLoop: EventLoop {
44+
switch self.eventLoopPreference.preference {
45+
case .indifferent, .testOnly_exact:
46+
preconditionFailure("Unimplemented")
47+
case .delegate(on: let eventLoop), .delegateAndChannel(on: let eventLoop):
48+
return eventLoop
49+
}
50+
}
51+
52+
func requestWasQueued(_: HTTP1RequestQueuer) {
53+
preconditionFailure("Unimplemented")
54+
}
55+
56+
func willBeExecutedOnConnection(_: HTTPConnectionPool.Connection) {
57+
preconditionFailure("Unimplemented")
58+
}
59+
60+
func willExecuteRequest(_: HTTP1RequestExecutor) -> Bool {
61+
preconditionFailure("Unimplemented")
62+
}
63+
64+
func requestHeadSent(_: HTTPRequestHead) {
65+
preconditionFailure("Unimplemented")
66+
}
67+
68+
func startRequestBodyStream() {
69+
preconditionFailure("Unimplemented")
70+
}
71+
72+
func pauseRequestBodyStream() {
73+
preconditionFailure("Unimplemented")
74+
}
75+
76+
func resumeRequestBodyStream() {
77+
preconditionFailure("Unimplemented")
78+
}
79+
80+
var request: HTTPClient.Request {
81+
preconditionFailure("Unimplemented")
82+
}
83+
84+
func nextRequestBodyPart(channelEL: EventLoop) -> EventLoopFuture<IOData?> {
85+
preconditionFailure("Unimplemented")
86+
}
87+
88+
func didSendRequestHead(_: HTTPRequestHead) {
89+
preconditionFailure("Unimplemented")
90+
}
91+
92+
func didSendRequestPart(_: IOData) {
93+
preconditionFailure("Unimplemented")
94+
}
95+
96+
func didSendRequest() {
97+
preconditionFailure("Unimplemented")
98+
}
99+
100+
func receiveResponseHead(_: HTTPResponseHead) {
101+
preconditionFailure("Unimplemented")
102+
}
103+
104+
func receiveResponseBodyPart(_: ByteBuffer) {
105+
preconditionFailure("Unimplemented")
106+
}
107+
108+
func receiveResponseEnd() {
109+
preconditionFailure("Unimplemented")
110+
}
111+
112+
func fail(_: Error) {
113+
preconditionFailure("Unimplemented")
114+
}
115+
}

0 commit comments

Comments
 (0)