mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
### Motivation: The NIOTooManyBytesError doesn't have any documentation for someone that encounters this error. Also, they have no idea the magnitude of the limit that was set to decide if the payload is an unreasonable size. ### Modifications: Provide documentation that explains the situation when the error occurs, which is the upTo limit of an AsyncSequence is exceeded. Describe one potential action, which is to increase this limit. Provide the maxBytes in the error so that the user can gauge whether the upTo limit is already reasonable and the payload size is excessive, or the limit needs to be increased to suit more situations. ### Result: There will be documentation for the NIOTooManyBytesError so that if someone looks it up they will have a better understanding of the situation, and possible actions. The limit will be included in the error to give them an idea of the scale of the payload limit that is in place, or potentially the configuration value that they can change. --------- Co-authored-by: Cory Benfield <lukasa@apple.com>
217 lines
6.6 KiB
Swift
217 lines
6.6 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import NIOCore
|
|
import XCTest
|
|
|
|
private struct TestCase {
|
|
var buffers: [[UInt8]]
|
|
var file: StaticString
|
|
var line: UInt
|
|
init(_ buffers: [[UInt8]], file: StaticString = #filePath, line: UInt = #line) {
|
|
self.buffers = buffers
|
|
self.file = file
|
|
self.line = line
|
|
}
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
|
final class AsyncSequenceCollectTests: XCTestCase {
|
|
func testAsyncSequenceCollect() async throws {
|
|
let testCases = [
|
|
TestCase([
|
|
[]
|
|
]),
|
|
TestCase([
|
|
[],
|
|
[],
|
|
]),
|
|
TestCase([
|
|
[0],
|
|
[],
|
|
]),
|
|
TestCase([
|
|
[],
|
|
[0],
|
|
]),
|
|
TestCase([
|
|
[0],
|
|
[1],
|
|
]),
|
|
TestCase([
|
|
[0],
|
|
[1],
|
|
]),
|
|
TestCase([
|
|
[0],
|
|
[1],
|
|
[2],
|
|
]),
|
|
TestCase([
|
|
[],
|
|
[0],
|
|
[],
|
|
[1],
|
|
[],
|
|
[2],
|
|
[],
|
|
]),
|
|
TestCase([
|
|
[0],
|
|
[1],
|
|
[2],
|
|
[],
|
|
[],
|
|
]),
|
|
TestCase([
|
|
Array(0..<10)
|
|
]),
|
|
TestCase([
|
|
Array(0..<10),
|
|
Array(10..<20),
|
|
]),
|
|
TestCase([
|
|
Array(0..<10),
|
|
Array(10..<20),
|
|
Array(20..<30),
|
|
]),
|
|
TestCase([
|
|
Array(0..<10),
|
|
Array(10..<20),
|
|
Array(20..<30),
|
|
Array(repeating: 99, count: 1000),
|
|
]),
|
|
]
|
|
for testCase in testCases {
|
|
let expectedBytes = testCase.buffers.flatMap({ $0 })
|
|
|
|
// happy case where maxBytes is exactly the same as number of buffers received
|
|
|
|
// test for the generic version
|
|
let accumulatedBytes1 = try await testCase.buffers
|
|
.asAsyncSequence()
|
|
.collect(upTo: expectedBytes.count, using: .init())
|
|
XCTAssertEqual(
|
|
accumulatedBytes1,
|
|
ByteBuffer(bytes: expectedBytes),
|
|
file: testCase.file,
|
|
line: testCase.line
|
|
)
|
|
|
|
// test for the `ByteBuffer` optimised version
|
|
let accumulatedBytes2 = try await testCase.buffers
|
|
.map(ByteBuffer.init(bytes:))
|
|
.asAsyncSequence()
|
|
.collect(upTo: expectedBytes.count)
|
|
XCTAssertEqual(
|
|
accumulatedBytes2,
|
|
ByteBuffer(bytes: expectedBytes),
|
|
file: testCase.file,
|
|
line: testCase.line
|
|
)
|
|
|
|
// unhappy case where maxBytes is one byte less than actually received
|
|
guard expectedBytes.count >= 1 else {
|
|
continue
|
|
}
|
|
|
|
// test for the generic version
|
|
let maxBytes = max(expectedBytes.count - 1, 0)
|
|
await XCTAssertThrowsError(
|
|
try await testCase.buffers
|
|
.asAsyncSequence()
|
|
.collect(upTo: maxBytes, using: .init()),
|
|
file: testCase.file,
|
|
line: testCase.line
|
|
) { error in
|
|
XCTAssertTrue(
|
|
error is NIOTooManyBytesError,
|
|
file: testCase.file,
|
|
line: testCase.line
|
|
)
|
|
guard let tooManyBytesErr = error as? NIOTooManyBytesError else {
|
|
XCTFail(
|
|
"Error was not an NIOTooManyBytesError",
|
|
file: testCase.file,
|
|
line: testCase.line
|
|
)
|
|
return
|
|
}
|
|
|
|
XCTAssertEqual(
|
|
maxBytes,
|
|
tooManyBytesErr.maxBytes,
|
|
file: testCase.file,
|
|
line: testCase.line
|
|
)
|
|
}
|
|
|
|
// test for the `ByteBuffer` optimised version
|
|
await XCTAssertThrowsError(
|
|
try await testCase.buffers
|
|
.map(ByteBuffer.init(bytes:))
|
|
.asAsyncSequence()
|
|
.collect(upTo: maxBytes),
|
|
file: testCase.file,
|
|
line: testCase.line
|
|
) { error in
|
|
XCTAssertTrue(
|
|
error is NIOTooManyBytesError,
|
|
file: testCase.file,
|
|
line: testCase.line
|
|
)
|
|
guard let tooManyBytesErr = error as? NIOTooManyBytesError else {
|
|
XCTFail(
|
|
"Error was not an NIOTooManyBytesError",
|
|
file: testCase.file,
|
|
line: testCase.line
|
|
)
|
|
return
|
|
}
|
|
|
|
// Sometimes the max bytes is subtracted from the header size
|
|
XCTAssertTrue(
|
|
tooManyBytesErr.maxBytes != nil && tooManyBytesErr.maxBytes! <= maxBytes,
|
|
file: testCase.file,
|
|
line: testCase.line
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
|
struct AsyncSequenceFromSyncSequence<Wrapped: Sequence>: AsyncSequence {
|
|
typealias Element = Wrapped.Element
|
|
struct AsyncIterator: AsyncIteratorProtocol {
|
|
fileprivate var iterator: Wrapped.Iterator
|
|
mutating func next() async throws -> Wrapped.Element? {
|
|
self.iterator.next()
|
|
}
|
|
}
|
|
|
|
fileprivate let wrapped: Wrapped
|
|
|
|
func makeAsyncIterator() -> AsyncIterator {
|
|
.init(iterator: self.wrapped.makeIterator())
|
|
}
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
|
extension Sequence {
|
|
/// Turns `self` into an `AsyncSequence` by wending each element of `self` asynchronously.
|
|
func asAsyncSequence() -> AsyncSequenceFromSyncSequence<Self> {
|
|
.init(wrapped: self)
|
|
}
|
|
}
|