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

Code before:
            #expect(seenEOF)
            return try self.decode(buffer: &buffer)
        }
    }

    static let testingArguments = [
        (elementCount: 100, chunkSize: 4),
        (elementCount: 89, chunkSize: 4),
        (elementCount: 77, chunkSize: 1),
        (elementCount: 65, chunkSize: 3),
        (elementCount: 61, chunkSize: 100),
        (elementCount: 55, chunkSize: 15),
    ]

    @Test(arguments: Self.testingArguments)
    func decodingWorks(elementCount: Int, chunkSize: Int) async throws {
        let baseSequence = AsyncStream<ByteBuffer>.makeStream()

        let randomElements: [UInt8] = (0..<elementCount).map {
            _ in UInt8.random(in: .min ... .max)
        }

        let buffers =
            randomElements
            .chunks(ofSize: chunkSize)
            .map(ByteBuffer.init(bytes:))

        try await withThrowingTaskGroup(of: Void.self) { group in
            group.addTask {
                for buffer in buffers {
                    /// Sleep for 10ms to simulate asynchronous work
                    try await Task.sleep(nanoseconds: 10_000_000)
                    baseSequence.continuation.yield(buffer)
                }
                baseSequence.continuation.finish()
            }

            group.addTask {
                var randomElements = randomElements
                let decodedSequence = baseSequence.stream.decode(using: ByteToInt32Decoder())

                for try await element in decodedSequence {
                    // Create an Int32 from the first 4 UInt8s
                    let int32 = randomElements[0..<4].enumerated().reduce(into: Int32(0)) { result, next in
                        result |= Int32(next.element) << ((3 - next.offset) * 8)
                    }
                    randomElements = Array(randomElements[4...])
                    #expect(element == int32)
                }
            }


Review comment (suggestion):
Can I suggest you do something like this:
- create an array of random `Int32`s
- create one large `ByteBuffer`
- read N slices of len `chunkSize` from the large `ByteBuffer`

Your N slices will then be input to the async stream, and then you don't need to form an `Int32` manually from four bytes as you're currently doing.

Code after:
            #expect(seenEOF)
            return try self.decode(buffer: &buffer)
        }
    }

    static let testingArguments = [
        (elementCount: 100, chunkSize: 4),
        (elementCount: 89, chunkSize: 4),
        (elementCount: 77, chunkSize: 1),
        (elementCount: 65, chunkSize: 3),
        (elementCount: 61, chunkSize: 100),
        (elementCount: 55, chunkSize: 15),
    ]

    @Test(arguments: Self.testingArguments)
    func decodingWorks(elementCount: Int, chunkSize: Int) async throws {
        var randomElements: [Int32] = (0..<elementCount).map {
            _ in Int32.random(in: .min ... .max)
        }

        var buffer = ByteBuffer()
        buffer.reserveCapacity(minimumWritableBytes: elementCount)
        for element in randomElements {
            buffer.writeInteger(element)
        }

        let baseSequence = AsyncStream<ByteBuffer>.makeStream()
        while buffer.readableBytes > 0 {
            let length = min(buffer.readableBytes, chunkSize)
            let _slice = buffer.readSlice(length: length)
            let slice = try #require(_slice)
            baseSequence.continuation.yield(slice)
        }
        baseSequence.continuation.finish()

        let decodedSequence = baseSequence.stream.decode(using: ByteToInt32Decoder())

        for try await element in decodedSequence {
            #expect(element == randomElements.removeFirst())
        }
    }

    @Test(arguments: Self.testingArguments)
    func decodingThrowsWhenDecoderThrows(elementCount: Int, chunkSize: Int) async throws {
        let randomElements: [Int32] = (0..<elementCount).map {
            _ in Int32.random(in: .min ... .max)
        }

        var buffer = ByteBuffer()
        buffer.reserveCapacity(minimumWritableBytes: elementCount)
        for element in randomElements {