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
Prev Previous commit
Next Next commit
AsyncTestingChannel: getOption(.allowRemoteHalfClosure) -> OK
Motivation:

`AsyncTestingChannel` interface should be in step with `EmbeddedChannel`
interface. Therefore also add support for the
`AllowRemoteHalfClosureOption`

Modifications:

* add a `public var allowRemoteHalfClosure` to `AsyncTestingChannel`
* enable setting/getting
  `ChannelOptions.Types.AllowRemoteHalfClosureOption` in `AsyncTestingChannel`
  (only modifies the `allowRemoteHalfClosure` variable
* add tests for new behaviour
  • Loading branch information
felixschlegel committed May 19, 2023
commit 7560113aeb672dc073db8f1e7acc6602c5d1f654
22 changes: 16 additions & 6 deletions Sources/NIOEmbedded/AsyncTestingChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public final class NIOAsyncTestingChannel: Channel {
/// - note: An ``NIOAsyncTestingChannel`` 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs synchronization around it. Can you follow the pattern we used for isActive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out! Have moved allowRemoteClosure to EmbeddedChannelCore now!


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

Expand Down Expand Up @@ -362,7 +365,7 @@ public final class NIOAsyncTestingChannel: Channel {
try self._readFromBuffer(buffer: &self.channelcore.outboundBuffer)
}
}

/// This method is similar to ``NIOAsyncTestingChannel/readOutbound(as:)`` but will wait if the outbound buffer is empty.
/// If available, this method reads one element of type `T` out of the ``NIOAsyncTestingChannel``'s outbound buffer. If the
/// first element was of a different type than requested, ``WrongTypeError`` will be thrown, if there
Expand Down Expand Up @@ -412,7 +415,7 @@ public final class NIOAsyncTestingChannel: Channel {
try self._readFromBuffer(buffer: &self.channelcore.inboundBuffer)
}
}

/// This method is similar to ``NIOAsyncTestingChannel/readInbound(as:)`` but will wait if the inbound buffer is empty.
/// If available, this method reads one element of type `T` out of the ``NIOAsyncTestingChannel``'s inbound buffer. If the
/// first element was of a different type than requested, ``WrongTypeError`` will be thrown, if there
Expand Down Expand Up @@ -499,7 +502,7 @@ public final class NIOAsyncTestingChannel: Channel {
throw error
}
}


@inlinable
func _readFromBuffer<T>(buffer: inout CircularBuffer<NIOAny>) throws -> T? {
Expand All @@ -510,7 +513,7 @@ public final class NIOAsyncTestingChannel: Channel {
}
return try self._cast(buffer.removeFirst(), to: T.self)
}

@inlinable
func _cast<T>(_ element: NIOAny, to: T.Type = T.self) throws -> T {
guard let t = self._channelCore.tryUnwrapData(element, as: T.self) else {
Expand All @@ -532,8 +535,12 @@ public final class NIOAsyncTestingChannel: 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 @@ -551,6 +558,9 @@ public final class NIOAsyncTestingChannel: 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
45 changes: 37 additions & 8 deletions Tests/NIOEmbeddedTests/AsyncTestingChannelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class AsyncTestingChannelTests: XCTestCase {
XCTAssertNoThrow(try channel.pipeline.removeHandler(name: "handler2").wait())
}
}

func testWaitForInboundWrite() throws {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
XCTAsyncTest {
Expand All @@ -79,14 +79,14 @@ class AsyncTestingChannelTests: XCTestCase {
try await XCTAsyncAssertEqual(try await channel.waitForInboundWrite(), 2)
try await XCTAsyncAssertEqual(try await channel.waitForInboundWrite(), 3)
}

try await channel.writeInbound(1)
try await channel.writeInbound(2)
try await channel.writeInbound(3)
try await task.value
}
}

func testWaitForMultipleInboundWritesInParallel() throws {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
XCTAsyncTest {
Expand All @@ -101,14 +101,14 @@ class AsyncTestingChannelTests: XCTestCase {
try await task3.value,
]), [1, 2, 3])
}

try await channel.writeInbound(1)
try await channel.writeInbound(2)
try await channel.writeInbound(3)
try await task.value
}
}

func testWaitForOutboundWrite() throws {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
XCTAsyncTest {
Expand All @@ -118,14 +118,14 @@ class AsyncTestingChannelTests: XCTestCase {
try await XCTAsyncAssertEqual(try await channel.waitForOutboundWrite(), 2)
try await XCTAsyncAssertEqual(try await channel.waitForOutboundWrite(), 3)
}

try await channel.writeOutbound(1)
try await channel.writeOutbound(2)
try await channel.writeOutbound(3)
try await task.value
}
}

func testWaitForMultipleOutboundWritesInParallel() throws {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
XCTAsyncTest {
Expand All @@ -140,7 +140,7 @@ class AsyncTestingChannelTests: XCTestCase {
try await task3.value,
]), [1, 2, 3])
}

try await channel.writeOutbound(1)
try await channel.writeOutbound(2)
try await channel.writeOutbound(3)
Expand Down Expand Up @@ -575,6 +575,35 @@ class AsyncTestingChannelTests: XCTestCase {
}.wait()
}

func testGetChannelOptionAutoReadIsSupported() throws {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
let channel = NIOAsyncTestingChannel()
try channel.testingEventLoop.submit {
let options = channel.syncOptions
XCTAssertNotNil(options)
// Unconditionally returns true.
XCTAssertEqual(try options?.getOption(ChannelOptions.autoRead), true)
}.wait()
}

func testSetGetChannelOptionAllowRemoteHalfClosureIsSupported() throws {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
let channel = NIOAsyncTestingChannel()
try channel.testingEventLoop.submit {
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)
}.wait()
}

func testSecondFinishThrows() throws {
guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
XCTAsyncTest {
Expand Down
2 changes: 1 addition & 1 deletion Tests/NIOEmbeddedTests/EmbeddedChannelTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ class EmbeddedChannelTest: XCTestCase {
XCTAssertEqual(try options?.getOption(ChannelOptions.autoRead), true)
}

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