Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Embedded: getOption(.allowRemoteHalfClosure) -> OK
Motivation:

In `swift-nio-ssl`, I am currently working on allowing half-closures
which relies on querying the underlying channel if
`ChannelOptions.Types.AllowRemoteHalfClosureOption` is enabled. As a lot of
`swift-nio-ssl`'s tests rely on `EmbeddedChannel` and it did not support
this option, a lot of the tests failed.

Modifications:

* add a `public var allowRemoteHalfClosure` to `EmbeddedChannel`
* enable setting/getting
  `ChannelOptions.Types.AllowRemoteHalfClosureOption` in
`EmbeddedChannel` (only modifies the `allowRemoteHalfClosure` variable
* add test for new behaviour
  • Loading branch information
felixschlegel committed May 19, 2023
commit c194bcd5b36e5246792ca7645ea5f4d7095370d8
20 changes: 15 additions & 5 deletions Sources/NIOEmbedded/Embedded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class EmbeddedChannelCore: ChannelCore {
/// Contains the flushed items that went into the `Channel` (and on a regular channel would have hit the network).
@usableFromInline
var outboundBuffer: CircularBuffer<NIOAny> = CircularBuffer()

/// Contains observers that want to consume the first element that would be appended to the `outboundBuffer`
@usableFromInline
var outboundBufferConsumer: Deque<(NIOAny) -> Void> = []
Expand All @@ -294,7 +294,7 @@ class EmbeddedChannelCore: ChannelCore {
/// regular `Channel` these items would be lost.
@usableFromInline
var inboundBuffer: CircularBuffer<NIOAny> = CircularBuffer()

/// Contains observers that want to consume the first element that would be appended to the `inboundBuffer`
@usableFromInline
var inboundBufferConsumer: Deque<(NIOAny) -> Void> = []
Expand Down Expand Up @@ -551,6 +551,9 @@ public final class EmbeddedChannel: Channel {
/// - note: An `EmbeddedChannel` starts _inactive_ and can be activated, for example by calling `connect`.
public var isActive: Bool { return channelcore.isActive }

/// - see: `ChannelOptions.Types.AllowRemoteHalfClosureOption`
public var allowRemoteHalfClosure: Bool = false

/// - see: `Channel.closeFuture`
public var closeFuture: EventLoopFuture<Void> { return channelcore.closePromise.futureResult }

Expand Down Expand Up @@ -749,7 +752,7 @@ public final class EmbeddedChannel: Channel {
let handlers = handler.map { [$0] } ?? []
self.init(handlers: handlers, loop: loop)
}

/// Create a new instance.
///
/// During creation it will automatically also register itself on the `EmbeddedEventLoop`.
Expand All @@ -776,8 +779,12 @@ public final class EmbeddedChannel: Channel {

@inlinable
internal func setOptionSync<Option: ChannelOption>(_ option: Option, value: Option.Value) {
// No options supported
fatalError("no options supported")
if option is ChannelOptions.Types.AllowRemoteHalfClosureOption {
self.allowRemoteHalfClosure = value as! Bool
return
}
// No other options supported
fatalError("option not supported")
}

/// - see: `Channel.getOption`
Expand All @@ -791,6 +798,9 @@ public final class EmbeddedChannel: Channel {
if option is ChannelOptions.Types.AutoReadOption {
return true as! Option.Value
}
if option is ChannelOptions.Types.AllowRemoteHalfClosureOption {
return self.allowRemoteHalfClosure as! Option.Value
}
fatalError("option \(option) not supported")
}

Expand Down
38 changes: 26 additions & 12 deletions Tests/NIOEmbeddedTests/EmbeddedChannelTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,50 +69,50 @@ class ChannelLifecycleHandler: ChannelInboundHandler {
}

class EmbeddedChannelTest: XCTestCase {

func testSingleHandlerInit() {
class Handler: ChannelInboundHandler {
typealias InboundIn = Never
}

let channel = EmbeddedChannel(handler: Handler())
XCTAssertNoThrow(try channel.pipeline.handler(type: Handler.self).wait())
}

func testSingleHandlerInitNil() {
class Handler: ChannelInboundHandler {
typealias InboundIn = Never
}

let channel = EmbeddedChannel(handler: nil)
XCTAssertThrowsError(try channel.pipeline.handler(type: Handler.self).wait()) { e in
XCTAssertEqual(e as? ChannelPipelineError, .notFound)
}
}

func testMultipleHandlerInit() {
class Handler: ChannelInboundHandler, RemovableChannelHandler {
typealias InboundIn = Never
let identifier: String

init(identifier: String) {
self.identifier = identifier
}
}

let channel = EmbeddedChannel(
handlers: [Handler(identifier: "0"), Handler(identifier: "1"), Handler(identifier: "2")]
)
XCTAssertNoThrow(XCTAssertEqual(try channel.pipeline.handler(type: Handler.self).wait().identifier, "0"))
XCTAssertNoThrow(try channel.pipeline.removeHandler(name: "handler0").wait())

XCTAssertNoThrow(XCTAssertEqual(try channel.pipeline.handler(type: Handler.self).wait().identifier, "1"))
XCTAssertNoThrow(try channel.pipeline.removeHandler(name: "handler1").wait())

XCTAssertNoThrow(XCTAssertEqual(try channel.pipeline.handler(type: Handler.self).wait().identifier, "2"))
XCTAssertNoThrow(try channel.pipeline.removeHandler(name: "handler2").wait())
}

func testWriteOutboundByteBuffer() throws {
let channel = EmbeddedChannel()
var buf = channel.allocator.buffer(capacity: 1024)
Expand Down Expand Up @@ -481,13 +481,27 @@ class EmbeddedChannelTest: XCTestCase {
XCTAssertEqual(invocations, 1)
}

func testSyncOptionsAreSupported() throws {
func testGetChannelOptionAutoReadIsSupported() {
let channel = EmbeddedChannel()
let options = channel.syncOptions
XCTAssertNotNil(options)
// Unconditionally returns true.
XCTAssertEqual(try options?.getOption(ChannelOptions.autoRead), true)
// (Setting options isn't supported.)
}

func testSetGetChannelOptionAllowRemoteHalfClosueIsSupported() {
let channel = EmbeddedChannel()
let options = channel.syncOptions
XCTAssertNotNil(options)

// allowRemoteHalfClosure should be false by default
XCTAssertEqual(try options?.getOption(ChannelOptions.allowRemoteHalfClosure), false)

channel.allowRemoteHalfClosure = true
XCTAssertEqual(try options?.getOption(ChannelOptions.allowRemoteHalfClosure), true)

XCTAssertNoThrow(try options?.setOption(ChannelOptions.allowRemoteHalfClosure, value: false))
XCTAssertEqual(try options?.getOption(ChannelOptions.allowRemoteHalfClosure), false)
}

func testLocalAddress0() throws {
Expand Down