Repository: apple/swift-nio
File: Tests/NIOPosixTests/AsyncChannelBootstrapTests.swift (Swift)

Code before:

                let firstNegotiationResult = try await group.next()
                let secondNegotiationResult = try await group.next()

                switch (firstNegotiationResult, secondNegotiationResult) {
                case (.string(let firstChannel), .string(let secondChannel)):
                    var firstInboundIterator = firstChannel.inboundStream.makeAsyncIterator()
                    var secondInboundIterator = secondChannel.inboundStream.makeAsyncIterator()

                    try await firstChannel.outboundWriter.write("request")
                    try await XCTAsyncAssertEqual(try await secondInboundIterator.next(), "request")

                    try await secondChannel.outboundWriter.write("response")
                    try await XCTAsyncAssertEqual(try await firstInboundIterator.next(), "response")

                default:
                    preconditionFailure()
                }
            }
        }
    }
    
    // MARK: - Pipe Bootstrap
    
    func testPipeBootstrap() throws {
        XCTAsyncTest {
            let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
            let (pipe1ReadFH, pipe1WriteFH, pipe2ReadFH, pipe2WriteFH) = self.makePipeFileDescriptors()
            let toChannel = FileHandle(fileDescriptor: pipe1WriteFH, closeOnDealloc: false)
            let fromChannel = FileHandle(fileDescriptor: pipe2ReadFH, closeOnDealloc: false)
            let channel: NIOAsyncChannel<ByteBuffer, ByteBuffer>
            
            do {
                channel = try await NIOPipeBootstrap(group: eventLoopGroup)
                    .withPipes(
                        inputDescriptor: pipe1ReadFH,
                        outputDescriptor: pipe2WriteFH
                    )
            } catch {
                [pipe1ReadFH, pipe1WriteFH, pipe2ReadFH, pipe2WriteFH].forEach { try? SystemCalls.close(descriptor: $0) }
                throw error
            }
            
            var inboundIterator = channel.inboundStream.makeAsyncIterator()
            
            do {
                try toChannel.writeBytes(.init(string: "Request"))
                try await XCTAsyncAssertEqual(try await inboundIterator.next(), ByteBuffer(string: "Request"))
                
                let response = ByteBuffer(string: "Response")
                try await channel.outboundWriter.write(response)

Review comment (suggestion):
I'm pretty sure we no longer need `XCTAsyncTest`, `async` tests should work from 5.6

Code after:
                let secondNegotiationResult = try await group.next()

                switch (firstNegotiationResult, secondNegotiationResult) {
                case (.string(let firstChannel), .string(let secondChannel)):
                    var firstInboundIterator = firstChannel.inboundStream.makeAsyncIterator()
                    var secondInboundIterator = secondChannel.inboundStream.makeAsyncIterator()

                    try await firstChannel.outboundWriter.write("request")
                    try await XCTAsyncAssertEqual(try await secondInboundIterator.next(), "request")

                    try await secondChannel.outboundWriter.write("response")
                    try await XCTAsyncAssertEqual(try await firstInboundIterator.next(), "response")

                default:
                    preconditionFailure()
                }
            }
        }
    }
    
    // MARK: - Pipe Bootstrap
    
    func testPipeBootstrap() async throws {
        let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
        let (pipe1ReadFH, pipe1WriteFH, pipe2ReadFH, pipe2WriteFH) = self.makePipeFileDescriptors()
        let toChannel = FileHandle(fileDescriptor: pipe1WriteFH, closeOnDealloc: false)
        let fromChannel = FileHandle(fileDescriptor: pipe2ReadFH, closeOnDealloc: false)
        let channel: NIOAsyncChannel<ByteBuffer, ByteBuffer>
        
        do {
            channel = try await NIOPipeBootstrap(group: eventLoopGroup)
                .takingOwnershipOfDescriptors(
                    input: pipe1ReadFH,
                    output: pipe2WriteFH
                )
        } catch {
            [pipe1ReadFH, pipe1WriteFH, pipe2ReadFH, pipe2WriteFH].forEach { try? SystemCalls.close(descriptor: $0) }
            throw error
        }
        
        var inboundIterator = channel.inboundStream.makeAsyncIterator()
        
        do {
            try toChannel.writeBytes(.init(string: "Request"))
            try await XCTAsyncAssertEqual(try await inboundIterator.next(), ByteBuffer(string: "Request"))
            
            let response = ByteBuffer(string: "Response")
            try await channel.outboundWriter.write(response)
            XCTAssertEqual(try fromChannel.readBytes(ofExactLength: response.readableBytes), Array(buffer: response))
        } catch {
            // We only got to close the FDs that are not owned by the PipeChannel