mirror of
https://github.com/apple/swift-nio
synced 2026-11-22 04:30:41 +00:00
Add API to enable throwing in EmbeddedChannel.getOption and .setOption if channel is closed (#3495)
### Motivation: Channels based on `BaseSocketChannel` throw in both `getOption` and `setOption` if the channel has been closed, since the `setsockopt` will fail. However, the current behavior of `EmbeddedChannel` is options remain writable and readable on closed channels. There are situations where we'd like to be able to model the runtime behaviour of the real channel in tests, e.g. to test this fix: https://github.com/apple/swift-nio-extras/pull/304. ### Modifications: - Add API to enable throwing in `EmbeddedChannel.getOption` and `.setOption` if channel is closed. - Add a test for this new behavior. ### Result: - New API to enable throwing in `EmbeddedChannel.getOption` and `.setOption` if channel is closed. - No observable change for existing users of `EmbeddedChannel`.
This commit is contained in:
1 parent
b2a14468d9
commit
37ffc4b8d5
7 files changed
+67
-22
No files matched your search
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"1_reqs_1000_conn": 375050,
|
||||
"1000_addHandlers_sync": 38050,
|
||||
"1000_addHandlers": 45050,
|
||||
"1000_addHandlers_sync": 39050,
|
||||
"1000_addHandlers": 46050,
|
||||
"1000_addRemoveHandlers_handlercontext": 8050,
|
||||
"1000_addRemoveHandlers_handlername": 8050,
|
||||
"1000_addRemoveHandlers_handlertype": 8050,
|
||||
@@ -9,7 +9,7 @@
|
||||
"1000_autoReadGetAndSet": 18050,
|
||||
"1000_copying_bytebufferview_to_array": 1050,
|
||||
"1000_copying_circularbuffer_to_array": 1050,
|
||||
"1000_getHandlers_sync": 36,
|
||||
"1000_getHandlers_sync": 37,
|
||||
"1000_getHandlers": 8050,
|
||||
"1000_reqs_1_conn": 26350,
|
||||
"1000_rst_connections": 138050,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"1_reqs_1000_conn": 375050,
|
||||
"1000_addHandlers_sync": 38050,
|
||||
"1000_addHandlers": 45050,
|
||||
"1000_addHandlers_sync": 39050,
|
||||
"1000_addHandlers": 46050,
|
||||
"1000_addRemoveHandlers_handlercontext": 8050,
|
||||
"1000_addRemoveHandlers_handlername": 8050,
|
||||
"1000_addRemoveHandlers_handlertype": 8050,
|
||||
@@ -9,7 +9,7 @@
|
||||
"1000_autoReadGetAndSet": 18050,
|
||||
"1000_copying_bytebufferview_to_array": 1050,
|
||||
"1000_copying_circularbuffer_to_array": 1050,
|
||||
"1000_getHandlers_sync": 36,
|
||||
"1000_getHandlers_sync": 37,
|
||||
"1000_getHandlers": 8050,
|
||||
"1000_reqs_1_conn": 26350,
|
||||
"1000_rst_connections": 138050,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"1_reqs_1000_conn": 375050,
|
||||
"1000_addHandlers_sync": 38050,
|
||||
"1000_addHandlers": 45050,
|
||||
"1000_addHandlers_sync": 39050,
|
||||
"1000_addHandlers": 46050,
|
||||
"1000_addRemoveHandlers_handlercontext": 8050,
|
||||
"1000_addRemoveHandlers_handlername": 8050,
|
||||
"1000_addRemoveHandlers_handlertype": 8050,
|
||||
@@ -9,7 +9,7 @@
|
||||
"1000_autoReadGetAndSet": 18050,
|
||||
"1000_copying_bytebufferview_to_array": 1050,
|
||||
"1000_copying_circularbuffer_to_array": 1050,
|
||||
"1000_getHandlers_sync": 36,
|
||||
"1000_getHandlers_sync": 37,
|
||||
"1000_getHandlers": 8050,
|
||||
"1000_reqs_1_conn": 26350,
|
||||
"1000_rst_connections": 138050,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"1_reqs_1000_conn": 372050,
|
||||
"1000_addHandlers_sync": 38050,
|
||||
"1000_addHandlers": 45050,
|
||||
"1000_addHandlers_sync": 39050,
|
||||
"1000_addHandlers": 46050,
|
||||
"1000_addRemoveHandlers_handlercontext": 8050,
|
||||
"1000_addRemoveHandlers_handlername": 8050,
|
||||
"1000_addRemoveHandlers_handlertype": 8050,
|
||||
@@ -9,7 +9,7 @@
|
||||
"1000_autoReadGetAndSet": 18050,
|
||||
"1000_copying_bytebufferview_to_array": 1050,
|
||||
"1000_copying_circularbuffer_to_array": 1050,
|
||||
"1000_getHandlers_sync": 36,
|
||||
"1000_getHandlers_sync": 37,
|
||||
"1000_getHandlers": 8050,
|
||||
"1000_reqs_1_conn": 26350,
|
||||
"1000_rst_connections": 136050,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"1_reqs_1000_conn": 375050,
|
||||
"1000_addHandlers_sync": 38050,
|
||||
"1000_addHandlers": 45050,
|
||||
"1000_addHandlers_sync": 39050,
|
||||
"1000_addHandlers": 46050,
|
||||
"1000_addRemoveHandlers_handlercontext": 8050,
|
||||
"1000_addRemoveHandlers_handlername": 8050,
|
||||
"1000_addRemoveHandlers_handlertype": 8050,
|
||||
@@ -9,7 +9,7 @@
|
||||
"1000_autoReadGetAndSet": 18050,
|
||||
"1000_copying_bytebufferview_to_array": 1050,
|
||||
"1000_copying_circularbuffer_to_array": 1050,
|
||||
"1000_getHandlers_sync": 36,
|
||||
"1000_getHandlers_sync": 37,
|
||||
"1000_getHandlers": 8050,
|
||||
"1000_reqs_1_conn": 26350,
|
||||
"1000_rst_connections": 138050,
|
||||
|
||||
@@ -456,9 +456,19 @@ class EmbeddedChannelCore: ChannelCore {
|
||||
}
|
||||
}
|
||||
|
||||
var allowOptionsWhenClosed: Bool {
|
||||
get {
|
||||
self._allowOptionsWhenClosed.load(ordering: .sequentiallyConsistent)
|
||||
}
|
||||
set {
|
||||
self._allowOptionsWhenClosed.store(newValue, ordering: .sequentiallyConsistent)
|
||||
}
|
||||
}
|
||||
|
||||
private let _isOpen = ManagedAtomic(true)
|
||||
private let _isActive = ManagedAtomic(false)
|
||||
private let _allowRemoteHalfClosure = ManagedAtomic(false)
|
||||
private let _allowOptionsWhenClosed = ManagedAtomic(true)
|
||||
|
||||
let eventLoop: EventLoop
|
||||
let closePromise: EventLoopPromise<Void>
|
||||
@@ -825,6 +835,9 @@ public final class EmbeddedChannel: Channel {
|
||||
/// - Note: An `EmbeddedChannel` starts _inactive_ and can be activated, for example by calling `connect`.
|
||||
public var isActive: Bool { channelcore.isActive }
|
||||
|
||||
@usableFromInline
|
||||
internal var isOpen: Bool { channelcore.isOpen }
|
||||
|
||||
/// - see: `ChannelOptions.Types.AllowRemoteHalfClosureOption`
|
||||
public var allowRemoteHalfClosure: Bool {
|
||||
get {
|
||||
@@ -837,6 +850,17 @@ public final class EmbeddedChannel: Channel {
|
||||
}
|
||||
}
|
||||
|
||||
public var allowOptionsWhenClosed: Bool {
|
||||
get {
|
||||
self.embeddedEventLoop.checkCorrectThread()
|
||||
return channelcore.allowOptionsWhenClosed
|
||||
}
|
||||
set {
|
||||
self.embeddedEventLoop.checkCorrectThread()
|
||||
channelcore.allowOptionsWhenClosed = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// - see: `Channel.closeFuture`
|
||||
public var closeFuture: EventLoopFuture<Void> { channelcore.closePromise.futureResult }
|
||||
|
||||
@@ -1089,14 +1113,17 @@ public final class EmbeddedChannel: Channel {
|
||||
@inlinable
|
||||
public func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> EventLoopFuture<Void> {
|
||||
self.embeddedEventLoop.checkCorrectThread()
|
||||
self.setOptionSync(option, value: value)
|
||||
return self.eventLoop.makeSucceededVoidFuture()
|
||||
return self.eventLoop.makeCompletedFuture { try self.setOptionSync(option, value: value) }
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal func setOptionSync<Option: ChannelOption>(_ option: Option, value: Option.Value) {
|
||||
internal func setOptionSync<Option: ChannelOption>(_ option: Option, value: Option.Value) throws {
|
||||
self.embeddedEventLoop.checkCorrectThread()
|
||||
|
||||
guard self.isOpen || self.allowOptionsWhenClosed else {
|
||||
throw ChannelError.alreadyClosed
|
||||
}
|
||||
|
||||
self.addOption(option, value: value)
|
||||
|
||||
if option is ChannelOptions.Types.AllowRemoteHalfClosureOption {
|
||||
@@ -1109,12 +1136,17 @@ public final class EmbeddedChannel: Channel {
|
||||
@inlinable
|
||||
public func getOption<Option: ChannelOption>(_ option: Option) -> EventLoopFuture<Option.Value> {
|
||||
self.embeddedEventLoop.checkCorrectThread()
|
||||
return self.eventLoop.makeSucceededFuture(self.getOptionSync(option))
|
||||
return self.eventLoop.makeCompletedFuture { try self.getOptionSync(option) }
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal func getOptionSync<Option: ChannelOption>(_ option: Option) -> Option.Value {
|
||||
internal func getOptionSync<Option: ChannelOption>(_ option: Option) throws -> Option.Value {
|
||||
self.embeddedEventLoop.checkCorrectThread()
|
||||
|
||||
guard self.isOpen || self.allowOptionsWhenClosed else {
|
||||
throw ChannelError.alreadyClosed
|
||||
}
|
||||
|
||||
if option is ChannelOptions.Types.AutoReadOption {
|
||||
return true as! Option.Value
|
||||
}
|
||||
@@ -1231,12 +1263,12 @@ extension EmbeddedChannel {
|
||||
|
||||
@inlinable
|
||||
public func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) throws {
|
||||
self.channel.setOptionSync(option, value: value)
|
||||
try self.channel.setOptionSync(option, value: value)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func getOption<Option: ChannelOption>(_ option: Option) throws -> Option.Value {
|
||||
self.channel.getOptionSync(option)
|
||||
try self.channel.getOptionSync(option)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -836,4 +836,17 @@ class EmbeddedChannelTest: XCTestCase {
|
||||
let optionValue2 = try channel.getOption(option).wait()
|
||||
XCTAssertEqual(2, optionValue2)
|
||||
}
|
||||
|
||||
func testGetSetOptionOptInThrowing() {
|
||||
let channel = EmbeddedChannel()
|
||||
XCTAssertNoThrow(try channel.close().wait())
|
||||
|
||||
XCTAssert(channel.allowOptionsWhenClosed)
|
||||
XCTAssertNoThrow(try channel.setOption(.connectTimeout, value: .milliseconds(42)).wait())
|
||||
XCTAssertNoThrow(XCTAssertEqual(try channel.getOption(.connectTimeout).wait(), .milliseconds(42)))
|
||||
|
||||
channel.allowOptionsWhenClosed = false
|
||||
XCTAssertThrowsError(try channel.setOption(.connectTimeout, value: .milliseconds(42)).wait())
|
||||
XCTAssertThrowsError(try channel.getOption(.connectTimeout).wait())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user