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

Code before:
                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
                )
                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

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

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