|
| 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 NIOCore |
| 18 | +import NIOEmbedded |
| 19 | +import NIOHTTP1 |
| 20 | +import XCTest |
| 21 | + |
| 22 | +class HTTPConnectionPool_RequestQueueTests: XCTestCase { |
| 23 | + func testCountAndIsEmptyWorks() { |
| 24 | + var queue = HTTPConnectionPool.RequestQueue() |
| 25 | + XCTAssertTrue(queue.isEmpty) |
| 26 | + XCTAssertEqual(queue.count, 0) |
| 27 | + let req1 = MockScheduledRequest(eventLoopPreference: .indifferent) |
| 28 | + let req1ID = queue.push(.init(req1)) |
| 29 | + XCTAssertFalse(queue.isEmpty) |
| 30 | + XCTAssertFalse(queue.isEmpty(for: nil)) |
| 31 | + XCTAssertEqual(queue.count, 1) |
| 32 | + XCTAssertEqual(queue.count(for: nil), 1) |
| 33 | + |
| 34 | + let req2 = MockScheduledRequest(eventLoopPreference: .indifferent) |
| 35 | + let req2ID = queue.push(.init(req2)) |
| 36 | + XCTAssertEqual(queue.count, 2) |
| 37 | + |
| 38 | + XCTAssert(queue.popFirst()?.__testOnly_wrapped_request() === req1) |
| 39 | + XCTAssertEqual(queue.count, 1) |
| 40 | + XCTAssertFalse(queue.isEmpty) |
| 41 | + XCTAssert(queue.remove(req2ID)?.__testOnly_wrapped_request() === req2) |
| 42 | + XCTAssertNil(queue.remove(req1ID)) |
| 43 | + XCTAssertEqual(queue.count, 0) |
| 44 | + XCTAssertTrue(queue.isEmpty) |
| 45 | + |
| 46 | + let eventLoop = EmbeddedEventLoop() |
| 47 | + |
| 48 | + XCTAssertTrue(queue.isEmpty(for: eventLoop)) |
| 49 | + XCTAssertEqual(queue.count(for: eventLoop), 0) |
| 50 | + let req3 = MockScheduledRequest(eventLoopPreference: .delegateAndChannel(on: eventLoop)) |
| 51 | + let req3ID = queue.push(.init(req3)) |
| 52 | + XCTAssertFalse(queue.isEmpty(for: eventLoop)) |
| 53 | + XCTAssertEqual(queue.count(for: eventLoop), 1) |
| 54 | + XCTAssertFalse(queue.isEmpty) |
| 55 | + XCTAssertEqual(queue.count, 1) |
| 56 | + XCTAssert(queue.popFirst(for: eventLoop)?.__testOnly_wrapped_request() === req3) |
| 57 | + XCTAssertNil(queue.remove(req3ID)) |
| 58 | + XCTAssertTrue(queue.isEmpty(for: eventLoop)) |
| 59 | + XCTAssertEqual(queue.count(for: eventLoop), 0) |
| 60 | + XCTAssertTrue(queue.isEmpty) |
| 61 | + XCTAssertEqual(queue.count, 0) |
| 62 | + |
| 63 | + let req4 = MockScheduledRequest(eventLoopPreference: .delegateAndChannel(on: eventLoop)) |
| 64 | + let req4ID = queue.push(.init(req4)) |
| 65 | + XCTAssert(queue.remove(req4ID)?.__testOnly_wrapped_request() === req4) |
| 66 | + |
| 67 | + let req5 = MockScheduledRequest(eventLoopPreference: .indifferent) |
| 68 | + queue.push(.init(req5)) |
| 69 | + let req6 = MockScheduledRequest(eventLoopPreference: .delegateAndChannel(on: eventLoop)) |
| 70 | + queue.push(.init(req6)) |
| 71 | + let all = queue.removeAll() |
| 72 | + let testSet = all.map { $0.__testOnly_wrapped_request() } |
| 73 | + XCTAssertEqual(testSet.count, 2) |
| 74 | + XCTAssertTrue(testSet.contains(where: { $0 === req5 })) |
| 75 | + XCTAssertTrue(testSet.contains(where: { $0 === req6 })) |
| 76 | + XCTAssertFalse(testSet.contains(where: { $0 === req4 })) |
| 77 | + XCTAssertTrue(queue.isEmpty(for: eventLoop)) |
| 78 | + XCTAssertEqual(queue.count(for: eventLoop), 0) |
| 79 | + XCTAssertTrue(queue.isEmpty) |
| 80 | + XCTAssertEqual(queue.count, 0) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +private class MockScheduledRequest: HTTPSchedulableRequest { |
| 85 | + init(eventLoopPreference: HTTPClient.EventLoopPreference) { |
| 86 | + self.eventLoopPreference = eventLoopPreference |
| 87 | + } |
| 88 | + |
| 89 | + var logger: Logger { preconditionFailure("Unimplemented") } |
| 90 | + var connectionDeadline: NIODeadline { preconditionFailure("Unimplemented") } |
| 91 | + let eventLoopPreference: HTTPClient.EventLoopPreference |
| 92 | + |
| 93 | + func requestWasQueued(_: HTTPRequestScheduler) { |
| 94 | + preconditionFailure("Unimplemented") |
| 95 | + } |
| 96 | + |
| 97 | + func fail(_: Error) { |
| 98 | + preconditionFailure("Unimplemented") |
| 99 | + } |
| 100 | + |
| 101 | + // MARK: HTTPExecutableRequest |
| 102 | + |
| 103 | + var requestHead: HTTPRequestHead { preconditionFailure("Unimplemented") } |
| 104 | + var requestFramingMetadata: RequestFramingMetadata { preconditionFailure("Unimplemented") } |
| 105 | + var idleReadTimeout: TimeAmount? { preconditionFailure("Unimplemented") } |
| 106 | + |
| 107 | + func willExecuteRequest(_: HTTPRequestExecutor) { |
| 108 | + preconditionFailure("Unimplemented") |
| 109 | + } |
| 110 | + |
| 111 | + func requestHeadSent() { |
| 112 | + preconditionFailure("Unimplemented") |
| 113 | + } |
| 114 | + |
| 115 | + func resumeRequestBodyStream() { |
| 116 | + preconditionFailure("Unimplemented") |
| 117 | + } |
| 118 | + |
| 119 | + func pauseRequestBodyStream() { |
| 120 | + preconditionFailure("Unimplemented") |
| 121 | + } |
| 122 | + |
| 123 | + func receiveResponseHead(_: HTTPResponseHead) { |
| 124 | + preconditionFailure("Unimplemented") |
| 125 | + } |
| 126 | + |
| 127 | + func receiveResponseBodyParts(_: CircularBuffer<ByteBuffer>) { |
| 128 | + preconditionFailure("Unimplemented") |
| 129 | + } |
| 130 | + |
| 131 | + func succeedRequest(_: CircularBuffer<ByteBuffer>?) { |
| 132 | + preconditionFailure("Unimplemented") |
| 133 | + } |
| 134 | +} |
0 commit comments