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

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

            try await group.waitForAll()
        }
    }

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

        let randomElements: [UInt8] = (0..<elementCount).map {

Review comment (suggestion):
I don't think we need to simulate async work here: we can assume the base async sequence we're wrapping works so it's fine to just prefill the `AsyncStream`

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