Repository: apple/swift-nio
File: Sources/NIOEmbedded/AsyncTestingChannel.swift (Swift)

Code before:
        }
        if option is ChannelOptions.Types.BufferedWritableBytesOption {
            let result = self.channelcore.pendingOutboundBuffer.reduce(0) { partialResult, dataAndPromise in
                let buffer = self.channelcore.unwrapData(dataAndPromise.0, as: ByteBuffer.self)
                return partialResult + buffer.readableBytes
            }

            return result as! Option.Value
        }

        guard let value = optionValue(for: option) else {
            fatalError("option \(option) not supported")
        }

        return value
    }

    @inlinable
    internal func optionValue<Option: ChannelOption>(for option: Option) -> Option.Value? {
        self.options.first(where: { $0.option is Option })?.value as? Option.Value
    }

    @inlinable
    internal func addOption<Option: ChannelOption>(_ option: Option, value: Option.Value) {
        // override the option if it exists
        _stateLock.withLockedValue { state in
            var options = state.options
            let optionIndex = options.firstIndex(where: { $0.option is Option })
            if let optionIndex = optionIndex {
               options[optionIndex] = (option, value)
            } else {
                options.append((option, value))
            }
            state.options = options
        }
    }

    /// Fires the (outbound) `bind` event through the `ChannelPipeline`. If the event hits the ``NIOAsyncTestingChannel`` which
    /// happens when it travels the `ChannelPipeline` all the way to the front, this will also set the
    /// ``NIOAsyncTestingChannel``'s ``localAddress``.
    ///
    /// - Parameters:
    ///   - address: The address to fake-bind to.
    ///   - promise: The `EventLoopPromise` which will be fulfilled when the fake-bind operation has been done.
    public func bind(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
        let promise = promise ?? self.testingEventLoop.makePromise()
        promise.futureResult.whenSuccess {
            self.localAddress = address
        }
        if self.eventLoop.inEventLoop {
            self.pipeline.bind(to: address, promise: promise)

Review comment (suggestion):
Nit:

```suggestion
        self._stateLock.withLockedValue { state in
```

Code after:
        }
        if option is ChannelOptions.Types.BufferedWritableBytesOption {
            let result = self.channelcore.pendingOutboundBuffer.reduce(0) { partialResult, dataAndPromise in
                let buffer = self.channelcore.unwrapData(dataAndPromise.0, as: ByteBuffer.self)
                return partialResult + buffer.readableBytes
            }

            return result as! Option.Value
        }

        guard let value = self.optionValue(for: option) else {
            fatalError("option \(option) not supported")
        }

        return value
    }

    @inlinable
    internal func optionValue<Option: ChannelOption>(for option: Option) -> Option.Value? {
        self.options.first(where: { $0.option is Option })?.value as? Option.Value
    }

    @inlinable
    internal func addOption<Option: ChannelOption>(_ option: Option, value: Option.Value) {
        // override the option if it exists
        self._stateLock.withLockedValue { state in
            var options = state.options
            let optionIndex = options.firstIndex(where: { $0.option is Option })
            if let optionIndex = optionIndex {
                options[optionIndex] = (option, value)
            } else {
                options.append((option, value))
            }
            state.options = options
        }
    }

    /// Fires the (outbound) `bind` event through the `ChannelPipeline`. If the event hits the ``NIOAsyncTestingChannel`` which
    /// happens when it travels the `ChannelPipeline` all the way to the front, this will also set the
    /// ``NIOAsyncTestingChannel``'s ``localAddress``.
    ///
    /// - Parameters:
    ///   - address: The address to fake-bind to.
    ///   - promise: The `EventLoopPromise` which will be fulfilled when the fake-bind operation has been done.
    public func bind(to address: SocketAddress, promise: EventLoopPromise<Void>?) {
        let promise = promise ?? self.testingEventLoop.makePromise()
        promise.futureResult.whenSuccess {
            self.localAddress = address
        }
        if self.eventLoop.inEventLoop {
            self.pipeline.bind(to: address, promise: promise)