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

Code before:
                                }
                                .wait()
                                .close()
                                .wait())
        }
    }

    // There was a bug where file handle ownership was not released when creating pipe channels failed.
    func testReleaseFileHandleOnOwningFailure() {
        struct NIOPipeBootstrapHooksChannelFail: NIOPipeBootstrapHooks {
            func makePipeChannel(eventLoop: NIOPosix.SelectableEventLoop, inputPipe: NIOCore.NIOFileHandle?, outputPipe: NIOCore.NIOFileHandle?) throws -> NIOPosix.PipeChannel {
                throw IOError(errnoCode: EBADF, reason: "testing")
            }
        }

        let sock = socket(AF_LOCAL, SOCK_STREAM, 0)
        defer {
            close(sock)
        }
        let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
        defer {
            try! elg.syncShutdownGracefully()
        }

        let bootstrap = NIOPipeBootstrap(validatingGroup: elg, hooks: NIOPipeBootstrapHooksChannelFail())
            XCTAssertNotNil(bootstrap)

        let channelFuture = bootstrap?.takingOwnershipOfDescriptor(inputOutput: sock)
        XCTAssertThrowsError(try channelFuture?.wait())
    }
}

private final class WriteStringOnChannelActive: ChannelInboundHandler {
    typealias InboundIn = Never
    typealias OutboundOut = ByteBuffer

    let string: String

    init(_ string: String) {
        self.string = string
    }

    func channelActive(context: ChannelHandlerContext) {
        var buffer = context.channel.allocator.buffer(capacity: self.string.utf8.count)
        buffer.writeString(string)
        context.writeAndFlush(self.wrapOutboundOut(buffer), promise: nil)
    }
}

private final class MakeSureAutoReadIsOffInChannelInitializer:  ChannelInboundHandler {
    typealias InboundIn = Channel

Review comment (suggestion):
```suggestion
        XCTAssertNotNil(bootstrap)
```

Code after:
                                }
                                .wait()
                                .close()
                                .wait())
        }
    }

    // There was a bug where file handle ownership was not released when creating pipe channels failed.
    func testReleaseFileHandleOnOwningFailure() {
        struct NIOPipeBootstrapHooksChannelFail: NIOPipeBootstrapHooks {
            func makePipeChannel(eventLoop: NIOPosix.SelectableEventLoop, inputPipe: NIOCore.NIOFileHandle?, outputPipe: NIOCore.NIOFileHandle?) throws -> NIOPosix.PipeChannel {
                throw IOError(errnoCode: EBADF, reason: "testing")
            }
        }

        let sock = socket(NIOBSDSocket.ProtocolFamily.local.rawValue, NIOBSDSocket.SocketType.stream.rawValue, 0)
        defer {
            close(sock)
        }
        let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
        defer {
            try! elg.syncShutdownGracefully()
        }

        let bootstrap = NIOPipeBootstrap(validatingGroup: elg, hooks: NIOPipeBootstrapHooksChannelFail())
        XCTAssertNotNil(bootstrap)

        let channelFuture = bootstrap?.takingOwnershipOfDescriptor(inputOutput: sock)
        XCTAssertThrowsError(try channelFuture?.wait())
    }
}

private final class WriteStringOnChannelActive: ChannelInboundHandler {
    typealias InboundIn = Never
    typealias OutboundOut = ByteBuffer

    let string: String

    init(_ string: String) {
        self.string = string
    }

    func channelActive(context: ChannelHandlerContext) {
        var buffer = context.channel.allocator.buffer(capacity: self.string.utf8.count)
        buffer.writeString(string)
        context.writeAndFlush(self.wrapOutboundOut(buffer), promise: nil)
    }
}

private final class MakeSureAutoReadIsOffInChannelInitializer:  ChannelInboundHandler {
    typealias InboundIn = Channel