Repository: apple/swift-nio
File: Tests/NIOCoreTests/AsyncSequenceTests.swift (Swift)

Code before:
                )
                if let tooManyBytesErr = error as? NIOTooManyBytesError {
                    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
                )
                if let tooManyBytesErr = error as? NIOTooManyBytesError {
                    // 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 {

Review comment (bug):
This should fail the test if the cast doesn't pass.

Code after:
                )
                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
                )
            }
        }
    }
}