update to latest NIO 2 (#26)

Motivation:

Code should compile and have the latest NIO 2 API.

Modifications:

- rename `ctx` to `context`
- apply all fixits

Result:

compiles again
This commit is contained in:
Johannes Weiss 2019-02-26 13:01:17 +00:00 committed by Cory Benfield
parent 971a6a37ec
commit 15fe53093c
14 changed files with 188 additions and 191 deletions

View File

@ -22,17 +22,17 @@ final class HTTP1ClientHandler: ChannelInboundHandler {
typealias OutboundOut = HTTPClientRequestPart
typealias InboundIn = HTTPClientResponsePart
func channelActive(ctx: ChannelHandlerContext) {
func channelActive(context: ChannelHandlerContext) {
var head = HTTPRequestHead(version: .init(major: 1, minor: 1), method: .GET, uri: "/get")
head.headers.add(name: "Host", value: "httpbin.org")
head.headers.add(name: "User-Agent", value: "SwiftNIO")
ctx.write(self.wrapOutboundOut(.head(head)), promise: nil)
ctx.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
context.write(self.wrapOutboundOut(.head(head)), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
print("Connected to \(ctx.channel.remoteAddress!) from \(ctx.channel.localAddress!)")
print("Connected to \(context.channel.remoteAddress!) from \(context.channel.localAddress!)")
}
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let part = self.unwrapInboundIn(data)
switch part {
@ -43,7 +43,7 @@ final class HTTP1ClientHandler: ChannelInboundHandler {
case .end:
// Print a newline.
print("")
ctx.close(promise: nil)
context.close(promise: nil)
}
}
@ -62,8 +62,8 @@ let channel = try! NIOTSConnectionBootstrap(group: group)
.channelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)
.tlsOptions(NWProtocolTLS.Options())
.channelInitializer { channel in
channel.pipeline.addHTTPClientHandlers().then {
channel.pipeline.add(handler: HTTP1ClientHandler())
channel.pipeline.addHTTPClientHandlers().flatMap {
channel.pipeline.addHandler(HTTP1ClientHandler())
}
}.connect(host: "httpbin.org", port: 443).wait()

View File

@ -22,7 +22,7 @@ final class HTTP1ServerHandler: ChannelInboundHandler {
typealias InboundIn = HTTPServerRequestPart
typealias OutboundOut = HTTPServerResponsePart
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let part = self.unwrapInboundIn(data)
guard case .head = part else {
@ -31,16 +31,16 @@ final class HTTP1ServerHandler: ChannelInboundHandler {
let responseHeaders = HTTPHeaders([("server", "nio-transport-services"), ("content-length", "0")])
let responseHead = HTTPResponseHead(version: .init(major: 1, minor: 1), status: .ok, headers: responseHeaders)
ctx.write(self.wrapOutboundOut(.head(responseHead)), promise: nil)
ctx.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
context.write(self.wrapOutboundOut(.head(responseHead)), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
}
}
let group = NIOTSEventLoopGroup()
let channel = try! NIOTSListenerBootstrap(group: group)
.childChannelInitializer { channel in
channel.pipeline.configureHTTPServerPipeline(withPipeliningAssistance: true, withErrorHandling: true).then {
channel.pipeline.add(handler: HTTP1ServerHandler())
channel.pipeline.configureHTTPServerPipeline(withPipeliningAssistance: true, withErrorHandling: true).flatMap {
channel.pipeline.addHandler(HTTP1ServerHandler())
}
}.bind(host: "127.0.0.1", port: 8888).wait()

View File

@ -24,11 +24,10 @@ import NIO
/// transitioning into the `failed` state, causing immediate connection failure.
///
/// This option is only valid with `NIOTSConnectionBootstrap`.
public enum NIOTSWaitForActivityOption: ChannelOption {
public typealias AssociatedValueType = ()
public typealias OptionType = Bool
public struct NIOTSWaitForActivityOption: ChannelOption, Equatable {
public typealias Value = Bool
case const(())
public init() {}
}
@ -37,18 +36,17 @@ public enum NIOTSWaitForActivityOption: ChannelOption {
/// `true`. By default this option is set to `false`.
///
/// This option must be set on the bootstrap: setting it after the channel is initialized will have no effect.
public enum NIOTSEnablePeerToPeerOption: ChannelOption {
public typealias AssociatedValueType = ()
public typealias OptionType = Bool
public struct NIOTSEnablePeerToPeerOption: ChannelOption, Equatable {
public typealias Value = Bool
case const(())
public init() {}
}
/// Options that can be set explicitly and only on bootstraps provided by `NIOTransportServices`.
public struct NIOTSChannelOptions {
/// - seealso: `NIOTSWaitForActivityOption`.
public static let waitForActivity = NIOTSWaitForActivityOption.const(())
public static let waitForActivity = NIOTSWaitForActivityOption()
public static let enablePeerToPeer = NIOTSEnablePeerToPeerOption.const(())
public static let enablePeerToPeer = NIOTSEnablePeerToPeerOption()
}

View File

@ -52,7 +52,7 @@ public final class NIOTSConnectionBootstrap {
/// - parameters:
/// - option: The option to be applied.
/// - value: The value for the option.
public func channelOption<T: ChannelOption>(_ option: T, value: T.OptionType) -> Self {
public func channelOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> Self {
channelOptions.put(key: option, value: value)
return self
}
@ -178,10 +178,10 @@ internal struct ChannelOptionStorage {
private var storage: [(Any, (Any, (Channel) -> (Any, Any) -> EventLoopFuture<Void>))] = []
mutating func put<K: ChannelOption>(key: K,
value newValue: K.OptionType) {
value newValue: K.Value) {
func applier(_ t: Channel) -> (Any, Any) -> EventLoopFuture<Void> {
return { (x, y) in
return t.setOption(option: x as! K, value: y as! K.OptionType)
return t.setOption(x as! K, value: y as! K.Value)
}
}
var hasSet = false

View File

@ -270,11 +270,11 @@ extension NIOTSConnectionChannel: Channel {
return self.backpressureManager.writable.load()
}
public var _unsafe: ChannelCore {
public var _channelCore: ChannelCore {
return self
}
public func setOption<T>(option: T, value: T.OptionType) -> EventLoopFuture<Void> where T : ChannelOption {
public func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> EventLoopFuture<Void> {
if eventLoop.inEventLoop {
let promise: EventLoopPromise<Void> = eventLoop.makePromise()
executeAndComplete(promise) { try setOption0(option: option, value: value) }
@ -284,7 +284,7 @@ extension NIOTSConnectionChannel: Channel {
}
}
private func setOption0<T: ChannelOption>(option: T, value: T.OptionType) throws {
private func setOption0<Option: ChannelOption>(option: Option, value: Option.Value) throws {
self.eventLoop.assertInEventLoop()
guard !self.closed else {
@ -301,7 +301,7 @@ extension NIOTSConnectionChannel: Channel {
let optionValue = option as! SocketOption
// SO_REUSEADDR and SO_REUSEPORT are handled here.
switch optionValue.value {
switch (optionValue.level, optionValue.name) {
case (SOL_SOCKET, SO_REUSEADDR):
self.reuseAddress = (value as! SocketOptionValue) != Int32(0)
case (SOL_SOCKET, SO_REUSEPORT):
@ -322,15 +322,15 @@ extension NIOTSConnectionChannel: Channel {
self.close0(error: err, mode: .all, promise: nil)
}
case is NIOTSEnablePeerToPeerOption:
self.enablePeerToPeer = value as! NIOTSEnablePeerToPeerOption.OptionType
self.enablePeerToPeer = value as! NIOTSEnablePeerToPeerOption.Value
default:
fatalError("option \(type(of: option)).\(option) not supported")
}
}
public func getOption<T>(option: T) -> EventLoopFuture<T.OptionType> where T : ChannelOption {
public func getOption<Option: ChannelOption>(_ option: Option) -> EventLoopFuture<Option.Value> {
if eventLoop.inEventLoop {
let promise: EventLoopPromise<T.OptionType> = eventLoop.makePromise()
let promise: EventLoopPromise<Option.Value> = eventLoop.makePromise()
executeAndComplete(promise) { try getOption0(option: option) }
return promise.futureResult
} else {
@ -338,7 +338,7 @@ extension NIOTSConnectionChannel: Channel {
}
}
func getOption0<T: ChannelOption>(option: T) throws -> T.OptionType {
func getOption0<Option: ChannelOption>(option: Option) throws -> Option.Value {
self.eventLoop.assertInEventLoop()
guard !self.closed else {
@ -347,27 +347,27 @@ extension NIOTSConnectionChannel: Channel {
switch option {
case _ as AutoReadOption:
return self.options.autoRead as! T.OptionType
return self.options.autoRead as! Option.Value
case _ as AllowRemoteHalfClosureOption:
return self.options.supportRemoteHalfClosure as! T.OptionType
return self.options.supportRemoteHalfClosure as! Option.Value
case _ as SocketOption:
let optionValue = option as! SocketOption
// SO_REUSEADDR and SO_REUSEPORT are handled here.
switch optionValue.value {
switch (optionValue.level, optionValue.name) {
case (SOL_SOCKET, SO_REUSEADDR):
return Int32(self.reuseAddress ? 1 : 0) as! T.OptionType
return Int32(self.reuseAddress ? 1 : 0) as! Option.Value
case (SOL_SOCKET, SO_REUSEPORT):
return Int32(self.reusePort ? 1 : 0) as! T.OptionType
return Int32(self.reusePort ? 1 : 0) as! Option.Value
default:
return try self.tcpOptions.valueFor(socketOption: optionValue) as! T.OptionType
return try self.tcpOptions.valueFor(socketOption: optionValue) as! Option.Value
}
case _ as WriteBufferWaterMarkOption:
return self.backpressureManager.waterMarks as! T.OptionType
return self.backpressureManager.waterMarks as! Option.Value
case _ as NIOTSWaitForActivityOption:
return self.options.waitForActivity as! T.OptionType
return self.options.waitForActivity as! Option.Value
case is NIOTSEnablePeerToPeerOption:
return self.enablePeerToPeer as! T.OptionType
return self.enablePeerToPeer as! Option.Value
default:
fatalError("option \(type(of: option)).\(option) not supported")
}
@ -683,7 +683,7 @@ extension NIOTSConnectionChannel {
// It would be nice if we didn't have to do this copy, but I'm not sure how to avoid it with the current Data
// APIs.
var buffer = self.allocator.buffer(capacity: content.count)
buffer.write(bytes: content)
buffer.writeBytes(content)
self.pipeline.fireChannelRead(NIOAny(buffer))
self.pipeline.fireChannelReadComplete()
}

View File

@ -33,7 +33,7 @@ public enum NIOTSErrors {
/// `UnsupportedSocketOption` is thrown when an attempt is made to configure a socket option that
/// is not supported by Network.framework.
public struct UnsupportedSocketOption: NIOTSError {
public let optionValue: SocketOption.AssociatedValueType
public let optionValue: SocketOption
public static func ==(lhs: UnsupportedSocketOption, rhs: UnsupportedSocketOption) -> Bool {
return lhs.optionValue == rhs.optionValue

View File

@ -83,7 +83,7 @@ public final class NIOTSListenerBootstrap {
/// - parameters:
/// - option: The option to be applied.
/// - value: The value for the option.
public func serverChannelOption<T: ChannelOption>(_ option: T, value: T.OptionType) -> Self {
public func serverChannelOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> Self {
self.serverChannelOptions.put(key: option, value: value)
return self
}
@ -93,7 +93,7 @@ public final class NIOTSListenerBootstrap {
/// - parameters:
/// - option: The option to be applied.
/// - value: The value for the option.
public func childChannelOption<T: ChannelOption>(_ option: T, value: T.OptionType) -> Self {
public func childChannelOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> Self {
self.childChannelOptions.put(key: option, value: value)
return self
}
@ -213,7 +213,7 @@ public final class NIOTSListenerBootstrap {
return serverChannelOptions.applyAll(channel: serverChannel).flatMap {
serverChannelInit(serverChannel)
}.flatMap {
serverChannel.pipeline.add(handler: AcceptHandler(childChannelInitializer: childChannelInit,
serverChannel.pipeline.addHandler(AcceptHandler(childChannelInitializer: childChannelInit,
childGroup: childEventLoopGroup,
childChannelOptions: childChannelOptions,
childChannelQoS: self.childQoS,
@ -261,14 +261,14 @@ private class AcceptHandler: ChannelInboundHandler {
self.originalTLSOptions = tlsOptions
}
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let conn = self.unwrapInboundIn(data)
let childLoop = self.childGroup.next() as! NIOTSEventLoop
let ctxEventLoop = ctx.eventLoop
let ctxEventLoop = context.eventLoop
let childInitializer = self.childChannelInitializer ?? { _ in childLoop.makeSucceededFuture(()) }
let newChannel = NIOTSConnectionChannel(wrapping: conn,
on: childLoop,
parent: ctx.channel,
parent: context.channel,
qos: self.childChannelQoS,
tcpOptions: self.originalTCPOptions,
tlsOptions: self.originalTLSOptions)
@ -286,17 +286,17 @@ private class AcceptHandler: ChannelInboundHandler {
ctxEventLoop.assertInEventLoop()
future.flatMap { (_) -> EventLoopFuture<Void> in
ctxEventLoop.assertInEventLoop()
guard ctx.channel.isActive else {
guard context.channel.isActive else {
return newChannel.close().flatMapThrowing {
throw ChannelError.ioOnClosedChannel
}
}
ctx.fireChannelRead(self.wrapInboundOut(newChannel))
return ctx.eventLoop.makeSucceededFuture(())
context.fireChannelRead(self.wrapInboundOut(newChannel))
return context.eventLoop.makeSucceededFuture(())
}.whenFailure { error in
ctx.eventLoop.assertInEventLoop()
context.eventLoop.assertInEventLoop()
_ = newChannel.close()
ctx.fireErrorCaught(error)
context.fireErrorCaught(error)
}
}
@ -305,7 +305,7 @@ private class AcceptHandler: ChannelInboundHandler {
} else {
fireThroughPipeline(childLoop.submit {
return setupChildChannel()
}.flatMap { $0 }.hopTo(eventLoop: ctxEventLoop))
}.flatMap { $0 }.hop(to: ctxEventLoop))
}
}
}

View File

@ -130,12 +130,11 @@ extension NIOTSListenerChannel: Channel {
return true
}
public var _unsafe: ChannelCore {
public var _channelCore: ChannelCore {
return self
}
public func setOption<T>(option: T, value: T.OptionType) -> EventLoopFuture<Void> where T : ChannelOption {
public func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> EventLoopFuture<Void> {
if eventLoop.inEventLoop {
let promise: EventLoopPromise<Void> = eventLoop.makePromise()
executeAndComplete(promise) { try setOption0(option: option, value: value) }
@ -145,7 +144,7 @@ extension NIOTSListenerChannel: Channel {
}
}
private func setOption0<T: ChannelOption>(option: T, value: T.OptionType) throws {
private func setOption0<Option: ChannelOption>(option: Option, value: Option.Value) throws {
self.eventLoop.assertInEventLoop()
guard !self.closed else {
@ -156,12 +155,12 @@ extension NIOTSListenerChannel: Channel {
switch option {
case is AutoReadOption:
// AutoRead is currently mandatory for TS listeners.
if value as! AutoReadOption.OptionType == false {
if value as! AutoReadOption.Value == false {
throw ChannelError.operationUnsupported
}
case let optionValue as SocketOption:
// SO_REUSEADDR and SO_REUSEPORT are handled here.
switch optionValue.value {
switch (optionValue.level, optionValue.name) {
case (SOL_SOCKET, SO_REUSEADDR):
self.reuseAddress = (value as! SocketOptionValue) != Int32(0)
case (SOL_SOCKET, SO_REUSEPORT):
@ -170,15 +169,15 @@ extension NIOTSListenerChannel: Channel {
try self.tcpOptions.applyChannelOption(option: optionValue, value: value as! SocketOptionValue)
}
case is NIOTSEnablePeerToPeerOption:
self.enablePeerToPeer = value as! NIOTSEnablePeerToPeerOption.OptionType
self.enablePeerToPeer = value as! NIOTSEnablePeerToPeerOption.Value
default:
fatalError("option \(option) not supported")
}
}
public func getOption<T>(option: T) -> EventLoopFuture<T.OptionType> where T : ChannelOption {
public func getOption<Option: ChannelOption>(_ option: Option) -> EventLoopFuture<Option.Value> {
if eventLoop.inEventLoop {
let promise: EventLoopPromise<T.OptionType> = eventLoop.makePromise()
let promise: EventLoopPromise<Option.Value> = eventLoop.makePromise()
executeAndComplete(promise) { try getOption0(option: option) }
return promise.futureResult
} else {
@ -186,7 +185,7 @@ extension NIOTSListenerChannel: Channel {
}
}
func getOption0<T: ChannelOption>(option: T) throws -> T.OptionType {
func getOption0<Option: ChannelOption>(option: Option) throws -> Option.Value {
self.eventLoop.assertInEventLoop()
guard !self.closed else {
@ -195,19 +194,19 @@ extension NIOTSListenerChannel: Channel {
switch option {
case is AutoReadOption:
return autoRead as! T.OptionType
return autoRead as! Option.Value
case let optionValue as SocketOption:
// SO_REUSEADDR and SO_REUSEPORT are handled here.
switch optionValue.value {
switch (optionValue.level, optionValue.name) {
case (SOL_SOCKET, SO_REUSEADDR):
return Int32(self.reuseAddress ? 1 : 0) as! T.OptionType
return Int32(self.reuseAddress ? 1 : 0) as! Option.Value
case (SOL_SOCKET, SO_REUSEPORT):
return Int32(self.reusePort ? 1 : 0) as! T.OptionType
return Int32(self.reusePort ? 1 : 0) as! Option.Value
default:
return try self.tcpOptions.valueFor(socketOption: optionValue) as! T.OptionType
return try self.tcpOptions.valueFor(socketOption: optionValue) as! Option.Value
}
case is NIOTSEnablePeerToPeerOption:
return self.enablePeerToPeer as! T.OptionType
return self.enablePeerToPeer as! Option.Value
default:
fatalError("option \(option) not supported")
}

View File

@ -20,7 +20,7 @@ import Network
internal extension NWProtocolTCP.Options {
/// Apply a given channel `SocketOption` to this protocol options state.
func applyChannelOption(option: SocketOption, value: SocketOptionValue) throws {
switch option.value {
switch (option.level, option.name) {
case (IPPROTO_TCP, TCP_NODELAY):
self.noDelay = value != 0
case (IPPROTO_TCP, TCP_NOPUSH):
@ -46,13 +46,13 @@ internal extension NWProtocolTCP.Options {
case (SOL_SOCKET, SO_KEEPALIVE):
self.enableKeepalive = value != 0
default:
throw NIOTSErrors.UnsupportedSocketOption(optionValue: option.value)
throw NIOTSErrors.UnsupportedSocketOption(optionValue: option)
}
}
/// Obtain the given `SocketOption` value for this protocol options state.
func valueFor(socketOption option: SocketOption) throws -> SocketOptionValue {
switch option.value {
switch (option.level, option.name) {
case (IPPROTO_TCP, TCP_NODELAY):
return self.noDelay ? 1 : 0
case (IPPROTO_TCP, TCP_NOPUSH):
@ -78,7 +78,7 @@ internal extension NWProtocolTCP.Options {
case (SOL_SOCKET, SO_KEEPALIVE):
return self.enableKeepalive ? 1 : 0
default:
throw NIOTSErrors.UnsupportedSocketOption(optionValue: option.value)
throw NIOTSErrors.UnsupportedSocketOption(optionValue: option)
}
}
}

View File

@ -28,19 +28,19 @@ final class ConnectRecordingHandler: ChannelOutboundHandler {
var connectTargets: [SocketAddress] = []
var endpointTargets: [NWEndpoint] = []
func connect(ctx: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
func connect(context: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
self.connectTargets.append(address)
ctx.connect(to: address, promise: promise)
context.connect(to: address, promise: promise)
}
func triggerUserOutboundEvent(ctx: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
switch event {
case let evt as NIOTSNetworkEvents.ConnectToNWEndpoint:
self.endpointTargets.append(evt.endpoint)
default:
break
}
ctx.triggerUserOutboundEvent(event, promise: promise)
context.triggerUserOutboundEvent(event, promise: promise)
}
}
@ -48,9 +48,9 @@ final class ConnectRecordingHandler: ChannelOutboundHandler {
final class FailOnReadHandler: ChannelInboundHandler {
typealias InboundIn = Any
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
XCTFail("Must not read")
ctx.fireChannelRead(data)
context.fireChannelRead(data)
}
}
@ -64,8 +64,8 @@ final class WritabilityChangedHandler: ChannelInboundHandler {
self.cb = cb
}
func channelWritabilityChanged(ctx: ChannelHandlerContext) {
self.cb(ctx.channel.isWritable)
func channelWritabilityChanged(context: ChannelHandlerContext) {
self.cb(context.channel.isWritable)
}
}
@ -74,10 +74,10 @@ final class DisableWaitingAfterConnect: ChannelOutboundHandler {
typealias OutboundIn = Any
typealias OutboundOut = Any
func connect(ctx: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
func connect(context: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
let f = ctx.channel.setOption(option: NIOTSChannelOptions.waitForActivity, value: false).flatMap {
ctx.connect(to: address)
let f = context.channel.setOption(NIOTSChannelOptions.waitForActivity, value: false).flatMap {
context.connect(to: address)
}
if let promise = promise {
f.cascade(to: promise)
@ -96,7 +96,7 @@ final class PromiseOnActiveHandler: ChannelInboundHandler {
self.promise = promise
}
func channelActive(ctx: ChannelHandlerContext) {
func channelActive(context: ChannelHandlerContext) {
self.promise.succeed(())
}
}
@ -122,7 +122,7 @@ class NIOTSConnectionChannelTests: XCTestCase {
}
let connectBootstrap = NIOTSConnectionBootstrap(group: self.group)
.channelInitializer { channel in channel.pipeline.add(handler: connectRecordingHandler) }
.channelInitializer { channel in channel.pipeline.addHandler(connectRecordingHandler) }
XCTAssertEqual(connectRecordingHandler.connectTargets, [])
XCTAssertEqual(connectRecordingHandler.endpointTargets, [])
@ -146,7 +146,7 @@ class NIOTSConnectionChannelTests: XCTestCase {
}
let connectBootstrap = NIOTSConnectionBootstrap(group: self.group)
.channelInitializer { channel in channel.pipeline.add(handler: connectRecordingHandler) }
.channelInitializer { channel in channel.pipeline.addHandler(connectRecordingHandler) }
XCTAssertEqual(connectRecordingHandler.connectTargets, [])
XCTAssertEqual(connectRecordingHandler.endpointTargets, [])
@ -170,7 +170,7 @@ class NIOTSConnectionChannelTests: XCTestCase {
}
let connectBootstrap = NIOTSConnectionBootstrap(group: self.group)
.channelInitializer { channel in channel.pipeline.add(handler: connectRecordingHandler) }
.channelInitializer { channel in channel.pipeline.addHandler(connectRecordingHandler) }
XCTAssertEqual(connectRecordingHandler.connectTargets, [])
XCTAssertEqual(connectRecordingHandler.endpointTargets, [])
@ -189,7 +189,7 @@ class NIOTSConnectionChannelTests: XCTestCase {
func testZeroLengthWritesHaveSatisfiedPromises() throws {
let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in channel.pipeline.add(handler: FailOnReadHandler())}
.childChannelInitializer { channel in channel.pipeline.addHandler(FailOnReadHandler())}
.bind(host: "localhost", port: 0).wait()
defer {
XCTAssertNoThrow(try listener.close().wait())
@ -212,12 +212,12 @@ class NIOTSConnectionChannelTests: XCTestCase {
let listener = try NIOTSListenerBootstrap(group: self.group)
.tcpOptions(tcpOptions)
.serverChannelInitializer { channel in
channel.getOption(option: ChannelOptions.socket(IPPROTO_TCP, TCP_SENDMOREACKS)).map { value in
channel.getOption(ChannelOptions.socket(IPPROTO_TCP, TCP_SENDMOREACKS)).map { value in
XCTAssertEqual(value, 1)
}
}
.childChannelInitializer { channel in
channel.getOption(option: ChannelOptions.socket(IPPROTO_TCP, TCP_SENDMOREACKS)).map { value in
channel.getOption(ChannelOptions.socket(IPPROTO_TCP, TCP_SENDMOREACKS)).map { value in
XCTAssertEqual(value, 1)
}
}
@ -229,7 +229,7 @@ class NIOTSConnectionChannelTests: XCTestCase {
let connection = try NIOTSConnectionBootstrap(group: self.group)
.tcpOptions(tcpOptions)
.channelInitializer { channel in
channel.getOption(option: ChannelOptions.socket(IPPROTO_TCP, TCP_SENDMOREACKS)).map { value in
channel.getOption(ChannelOptions.socket(IPPROTO_TCP, TCP_SENDMOREACKS)).map { value in
XCTAssertEqual(value, 1)
}
}
@ -256,13 +256,13 @@ class NIOTSConnectionChannelTests: XCTestCase {
XCTAssertNoThrow(try connection.close().wait())
}
try connection.getOption(option: ChannelOptions.writeBufferWaterMark).flatMap { option -> EventLoopFuture<Void> in
try connection.getOption(ChannelOptions.writeBufferWaterMark).flatMap { option -> EventLoopFuture<Void> in
XCTAssertEqual(option.high, 64 * 1024)
XCTAssertEqual(option.low, 32 * 1024)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 1, high: 101))
return connection.setOption(ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 1, high: 101))
}.flatMap {
connection.getOption(option: ChannelOptions.writeBufferWaterMark)
connection.getOption(ChannelOptions.writeBufferWaterMark)
}.map {
XCTAssertEqual($0.high, 101)
XCTAssertEqual($0.low, 1)
@ -282,14 +282,14 @@ class NIOTSConnectionChannelTests: XCTestCase {
}
let connection = try NIOTSConnectionBootstrap(group: self.group)
.channelInitializer { channel in channel.pipeline.add(handler: handler) }
.channelInitializer { channel in channel.pipeline.addHandler(handler) }
.connect(to: listener.localAddress!)
.wait()
// We're going to set some helpful watermarks, and allocate a big buffer.
XCTAssertNoThrow(try connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 2, high: 2048)).wait())
XCTAssertNoThrow(try connection.setOption(ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 2, high: 2048)).wait())
var buffer = connection.allocator.buffer(capacity: 2048)
buffer.write(bytes: repeatElement(UInt8(4), count: 2048))
buffer.writeBytes(repeatElement(UInt8(4), count: 2048))
// We're going to issue the following pattern of writes:
// a: 1 byte
@ -382,7 +382,7 @@ class NIOTSConnectionChannelTests: XCTestCase {
}
let connection = try NIOTSConnectionBootstrap(group: self.group)
.channelInitializer { channel in channel.pipeline.add(handler: handler) }
.channelInitializer { channel in channel.pipeline.addHandler(handler) }
.connect(to: listener.localAddress!)
.wait()
defer {
@ -391,7 +391,7 @@ class NIOTSConnectionChannelTests: XCTestCase {
// We're going to allocate a buffer.
var buffer = connection.allocator.buffer(capacity: 256)
buffer.write(bytes: repeatElement(UInt8(4), count: 256))
buffer.writeBytes(repeatElement(UInt8(4), count: 256))
// We're going to issue a 256-byte write. This write will not cause any change in channel writability
// state.
@ -423,36 +423,36 @@ class NIOTSConnectionChannelTests: XCTestCase {
XCTAssertTrue(connection.isWritable)
}.wait()
try connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 256)).flatMap {
try connection.setOption(ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 256)).flatMap {
// High to 256, low to 128. No writability change.
XCTAssertEqual(writabilities, [])
XCTAssertTrue(connection.isWritable)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 255))
return connection.setOption(ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 255))
}.flatMap {
// High to 255, low to 127. Channel becomes not writable.
XCTAssertEqual(writabilities, [false])
XCTAssertFalse(connection.isWritable)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 256))
return connection.setOption(ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 256))
}.flatMap {
// High back to 256, low to 128. No writability change.
XCTAssertEqual(writabilities, [false])
XCTAssertFalse(connection.isWritable)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 256, high: 1024))
return connection.setOption(ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 256, high: 1024))
}.flatMap {
// High to 1024, low to 128. No writability change.
XCTAssertEqual(writabilities, [false])
XCTAssertFalse(connection.isWritable)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 257, high: 1024))
return connection.setOption(ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 257, high: 1024))
}.flatMap {
// Low to 257, channel becomes writable again.
XCTAssertEqual(writabilities, [false, true])
XCTAssertTrue(connection.isWritable)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 256, high: 1024))
return connection.setOption(ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 256, high: 1024))
}.map {
// Low back to 256, no writability change.
XCTAssertEqual(writabilities, [false, true])
@ -472,11 +472,11 @@ class NIOTSConnectionChannelTests: XCTestCase {
XCTAssertNoThrow(try connection.close().wait())
}
XCTAssertEqual(0, try connection.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
XCTAssertNoThrow(try connection.setOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR), value: 5).wait())
XCTAssertEqual(1, try connection.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
XCTAssertNoThrow(try connection.setOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR), value: 0).wait())
XCTAssertEqual(0, try connection.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
XCTAssertEqual(0, try connection.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
XCTAssertNoThrow(try connection.setOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR), value: 5).wait())
XCTAssertEqual(1, try connection.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
XCTAssertNoThrow(try connection.setOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR), value: 0).wait())
XCTAssertEqual(0, try connection.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
}
func testSettingGettingReuseport() throws {
@ -491,11 +491,11 @@ class NIOTSConnectionChannelTests: XCTestCase {
XCTAssertNoThrow(try connection.close().wait())
}
XCTAssertEqual(0, try connection.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
XCTAssertNoThrow(try connection.setOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT), value: 5).wait())
XCTAssertEqual(1, try connection.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
XCTAssertNoThrow(try connection.setOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT), value: 0).wait())
XCTAssertEqual(0, try connection.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
XCTAssertEqual(0, try connection.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
XCTAssertNoThrow(try connection.setOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT), value: 5).wait())
XCTAssertEqual(1, try connection.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
XCTAssertNoThrow(try connection.setOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT), value: 0).wait())
XCTAssertEqual(0, try connection.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
}
func testErrorsInChannelSetupAreFine() throws {
@ -541,7 +541,7 @@ class NIOTSConnectionChannelTests: XCTestCase {
func testEarlyExitCanBeSetInWaitingState() throws {
let connectFuture = NIOTSConnectionBootstrap(group: self.group)
.channelInitializer { channel in
channel.pipeline.add(handler: DisableWaitingAfterConnect())
channel.pipeline.addHandler(DisableWaitingAfterConnect())
}.connect(to: try SocketAddress(unixDomainSocketPath: "/this/path/definitely/doesnt/exist"))
do {
@ -564,12 +564,12 @@ class NIOTSConnectionChannelTests: XCTestCase {
let connectFuture = NIOTSConnectionBootstrap(group: self.group)
.channelInitializer { channel in
return channel.getOption(option: NIOTSChannelOptions.waitForActivity).map { value in
return channel.getOption(NIOTSChannelOptions.waitForActivity).map { value in
XCTAssertTrue(value)
}.flatMap {
channel.setOption(option: NIOTSChannelOptions.waitForActivity, value: false)
channel.setOption(NIOTSChannelOptions.waitForActivity, value: false)
}.flatMap {
channel.getOption(option: NIOTSChannelOptions.waitForActivity)
channel.getOption(NIOTSChannelOptions.waitForActivity)
}.map { value in
XCTAssertFalse(value)
}
@ -588,12 +588,12 @@ class NIOTSConnectionChannelTests: XCTestCase {
let connectFuture = NIOTSConnectionBootstrap(group: self.group)
.channelInitializer { channel in
return channel.getOption(option: NIOTSChannelOptions.enablePeerToPeer).map { value in
return channel.getOption(NIOTSChannelOptions.enablePeerToPeer).map { value in
XCTAssertFalse(value)
}.then {
channel.setOption(option: NIOTSChannelOptions.enablePeerToPeer, value: true)
}.then {
channel.getOption(option: NIOTSChannelOptions.enablePeerToPeer)
}.flatMap {
channel.setOption(NIOTSChannelOptions.enablePeerToPeer, value: true)
}.flatMap {
channel.getOption(NIOTSChannelOptions.enablePeerToPeer)
}.map { value in
XCTAssertTrue(value)
}
@ -615,7 +615,7 @@ class NIOTSConnectionChannelTests: XCTestCase {
let channel = try NIOTSConnectionBootstrap(group: self.group)
.channelInitializer { channel in
channel.pipeline.add(handler: PromiseOnActiveHandler(activePromise))
channel.pipeline.addHandler(PromiseOnActiveHandler(activePromise))
}.connect(to: listener.localAddress!).wait()
XCTAssertNoThrow(try activePromise.futureResult.wait())

View File

@ -24,12 +24,12 @@ final class EchoHandler: ChannelInboundHandler {
typealias InboundIn = Any
typealias OutboundOut = Any
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
ctx.write(data, promise: nil)
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
context.write(data, promise: nil)
}
func channelReadComplete(ctx: ChannelHandlerContext) {
ctx.flush()
func channelReadComplete(context: ChannelHandlerContext) {
context.flush()
}
}
@ -51,22 +51,22 @@ final class ReadExpecter: ChannelInboundHandler {
self.expectedRead = expecting
}
func handlerAdded(ctx: ChannelHandlerContext) {
self.readPromise = ctx.eventLoop.makePromise()
func handlerAdded(context: ChannelHandlerContext) {
self.readPromise = context.eventLoop.makePromise()
}
func handlerRemoved(ctx: ChannelHandlerContext) {
func handlerRemoved(context: ChannelHandlerContext) {
if let promise = self.readPromise {
promise.fail(DidNotReadError())
}
}
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
var bytes = self.unwrapInboundIn(data)
if self.cumulationBuffer == nil {
self.cumulationBuffer = bytes
} else {
self.cumulationBuffer!.write(buffer: &bytes)
self.cumulationBuffer!.writeBuffer(&bytes)
}
self.maybeFulfillPromise()
@ -85,8 +85,8 @@ final class CloseOnActiveHandler: ChannelInboundHandler {
typealias InboundIn = Never
typealias OutboundOut = Never
func channelActive(ctx: ChannelHandlerContext) {
ctx.close(promise: nil)
func channelActive(context: ChannelHandlerContext) {
context.close(promise: nil)
}
}
@ -103,7 +103,7 @@ final class HalfCloseHandler: ChannelInboundHandler {
self.halfClosedPromise = halfClosedPromise
}
func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) {
func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
switch event {
case ChannelEvent.inputClosed:
XCTAssertFalse(self.alreadyHalfClosed)
@ -111,15 +111,15 @@ final class HalfCloseHandler: ChannelInboundHandler {
self.alreadyHalfClosed = true
self.halfClosedPromise.succeed(())
ctx.close(mode: .output, promise: nil)
context.close(mode: .output, promise: nil)
default:
break
}
ctx.fireUserInboundEventTriggered(event)
context.fireUserInboundEventTriggered(event)
}
func channelInactive(ctx: ChannelHandlerContext) {
func channelInactive(context: ChannelHandlerContext) {
XCTAssertTrue(self.alreadyHalfClosed)
XCTAssertFalse(self.closed)
self.closed = true
@ -130,16 +130,16 @@ final class HalfCloseHandler: ChannelInboundHandler {
final class FailOnHalfCloseHandler: ChannelInboundHandler {
typealias InboundIn = Any
func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) {
func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
switch event {
case ChannelEvent.inputClosed:
XCTFail("Must not receive half-closure")
ctx.close(promise: nil)
context.close(promise: nil)
default:
break
}
ctx.fireUserInboundEventTriggered(event)
context.fireUserInboundEventTriggered(event)
}
}
@ -148,7 +148,7 @@ extension Channel {
/// Expect that the given bytes will be received.
func expectRead(_ bytes: ByteBuffer) -> EventLoopFuture<Void> {
let expecter = ReadExpecter(expecting: bytes)
return self.pipeline.add(handler: expecter).flatMap {
return self.pipeline.addHandler(expecter).flatMap {
return expecter.readFuture!
}
}
@ -157,7 +157,7 @@ extension Channel {
extension ByteBufferAllocator {
func bufferFor(string: String) -> ByteBuffer {
var buffer = self.buffer(capacity: string.count)
buffer.write(string: string)
buffer.writeString(string)
return buffer
}
}
@ -176,7 +176,7 @@ class NIOTSEndToEndTests: XCTestCase {
func testSimpleListener() throws {
let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in channel.pipeline.add(handler: EchoHandler())}
.childChannelInitializer { channel in channel.pipeline.addHandler(EchoHandler())}
.bind(host: "localhost", port: 0).wait()
defer {
XCTAssertNoThrow(try listener.close().wait())
@ -195,7 +195,7 @@ class NIOTSEndToEndTests: XCTestCase {
func testMultipleConnectionsOneListener() throws {
let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in channel.pipeline.add(handler: EchoHandler())}
.childChannelInitializer { channel in channel.pipeline.addHandler(EchoHandler())}
.bind(host: "localhost", port: 0).wait()
defer {
XCTAssertNoThrow(try listener.close().wait())
@ -218,7 +218,7 @@ class NIOTSEndToEndTests: XCTestCase {
func testBasicConnectionTeardown() throws {
let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in channel.pipeline.add(handler: CloseOnActiveHandler())}
.childChannelInitializer { channel in channel.pipeline.addHandler(CloseOnActiveHandler())}
.bind(host: "localhost", port: 0).wait()
defer {
XCTAssertNoThrow(try listener.close().wait())
@ -256,7 +256,7 @@ class NIOTSEndToEndTests: XCTestCase {
}
let bootstrap = NIOTSConnectionBootstrap(group: self.group).channelInitializer { channel in
channel.pipeline.add(handler: CloseOnActiveHandler())
channel.pipeline.addHandler(CloseOnActiveHandler())
}
for _ in (0..<10) {
@ -283,7 +283,7 @@ class NIOTSEndToEndTests: XCTestCase {
let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in
serverSideConnectionPromise.succeed(channel)
return channel.pipeline.add(handler: EchoHandler())
return channel.pipeline.addHandler(EchoHandler())
}
.bind(host: "localhost", port: 0).wait()
defer {
@ -306,8 +306,8 @@ class NIOTSEndToEndTests: XCTestCase {
let halfClosedPromise: EventLoopPromise<Void> = self.group.next().makePromise()
let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in
channel.pipeline.add(handler: EchoHandler()).flatMap { _ in
channel.pipeline.add(handler: HalfCloseHandler(halfClosedPromise))
channel.pipeline.addHandler(EchoHandler()).flatMap { _ in
channel.pipeline.addHandler(HalfCloseHandler(halfClosedPromise))
}
}
.childChannelOption(ChannelOptions.allowRemoteHalfClosure, value: true)
@ -336,8 +336,8 @@ class NIOTSEndToEndTests: XCTestCase {
func testDisabledHalfClosureCausesFullClosure() throws {
let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in
channel.pipeline.add(handler: EchoHandler()).flatMap { _ in
channel.pipeline.add(handler: FailOnHalfCloseHandler())
channel.pipeline.addHandler(EchoHandler()).flatMap { _ in
channel.pipeline.addHandler(FailOnHalfCloseHandler())
}
}
.bind(host: "localhost", port: 0).wait()
@ -416,7 +416,7 @@ class NIOTSEndToEndTests: XCTestCase {
let udsPath = "/tmp/\(UUID().uuidString)_testBasicUnixSockets.sock"
let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in channel.pipeline.add(handler: EchoHandler())}
.childChannelInitializer { channel in channel.pipeline.addHandler(EchoHandler())}
.bind(unixDomainSocketPath: udsPath).wait()
defer {
XCTAssertNoThrow(try listener.close().wait())
@ -446,7 +446,7 @@ class NIOTSEndToEndTests: XCTestCase {
let serviceEndpoint = NWEndpoint.service(name: name, type: "_niots._tcp", domain: "local", interface: nil)
let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in channel.pipeline.add(handler: EchoHandler())}
.childChannelInitializer { channel in channel.pipeline.addHandler(EchoHandler())}
.bind(endpoint: serviceEndpoint).wait()
defer {
XCTAssertNoThrow(try listener.close().wait())

View File

@ -26,19 +26,19 @@ final class BindRecordingHandler: ChannelOutboundHandler {
var bindTargets: [SocketAddress] = []
var endpointTargets: [NWEndpoint] = []
func bind(ctx: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
func bind(context: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
self.bindTargets.append(address)
ctx.bind(to: address, promise: promise)
context.bind(to: address, promise: promise)
}
func triggerUserOutboundEvent(ctx: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
switch event {
case let evt as NIOTSNetworkEvents.BindToNWEndpoint:
self.endpointTargets.append(evt.endpoint)
default:
break
}
ctx.triggerUserOutboundEvent(event, promise: promise)
context.triggerUserOutboundEvent(event, promise: promise)
}
}
@ -58,7 +58,7 @@ class NIOTSListenerChannelTests: XCTestCase {
let bindRecordingHandler = BindRecordingHandler()
let target = try SocketAddress.makeAddressResolvingHost("localhost", port: 0)
let bindBootstrap = NIOTSListenerBootstrap(group: self.group)
.serverChannelInitializer { channel in channel.pipeline.add(handler: bindRecordingHandler)}
.serverChannelInitializer { channel in channel.pipeline.addHandler(bindRecordingHandler)}
XCTAssertEqual(bindRecordingHandler.bindTargets, [])
XCTAssertEqual(bindRecordingHandler.endpointTargets, [])
@ -77,7 +77,7 @@ class NIOTSListenerChannelTests: XCTestCase {
func testConnectingToHostPortTraversesPipeline() throws {
let bindRecordingHandler = BindRecordingHandler()
let bindBootstrap = NIOTSListenerBootstrap(group: self.group)
.serverChannelInitializer { channel in channel.pipeline.add(handler: bindRecordingHandler)}
.serverChannelInitializer { channel in channel.pipeline.addHandler(bindRecordingHandler)}
XCTAssertEqual(bindRecordingHandler.bindTargets, [])
XCTAssertEqual(bindRecordingHandler.endpointTargets, [])
@ -97,7 +97,7 @@ class NIOTSListenerChannelTests: XCTestCase {
let endpoint = NWEndpoint.hostPort(host: .ipv4(.loopback), port: .any)
let bindRecordingHandler = BindRecordingHandler()
let bindBootstrap = NIOTSListenerBootstrap(group: self.group)
.serverChannelInitializer { channel in channel.pipeline.add(handler: bindRecordingHandler)}
.serverChannelInitializer { channel in channel.pipeline.addHandler(bindRecordingHandler)}
XCTAssertEqual(bindRecordingHandler.bindTargets, [])
XCTAssertEqual(bindRecordingHandler.endpointTargets, [])
@ -119,11 +119,11 @@ class NIOTSListenerChannelTests: XCTestCase {
XCTAssertNoThrow(try listener.close().wait())
}
XCTAssertEqual(0, try listener.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
XCTAssertNoThrow(try listener.setOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR), value: 5).wait())
XCTAssertEqual(1, try listener.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
XCTAssertNoThrow(try listener.setOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR), value: 0).wait())
XCTAssertEqual(0, try listener.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
XCTAssertEqual(0, try listener.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
XCTAssertNoThrow(try listener.setOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR), value: 5).wait())
XCTAssertEqual(1, try listener.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
XCTAssertNoThrow(try listener.setOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR), value: 0).wait())
XCTAssertEqual(0, try listener.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEADDR)).wait())
}
func testSettingGettingReuseport() throws {
@ -132,11 +132,11 @@ class NIOTSListenerChannelTests: XCTestCase {
XCTAssertNoThrow(try listener.close().wait())
}
XCTAssertEqual(0, try listener.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
XCTAssertNoThrow(try listener.setOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT), value: 5).wait())
XCTAssertEqual(1, try listener.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
XCTAssertNoThrow(try listener.setOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT), value: 0).wait())
XCTAssertEqual(0, try listener.getOption(option: ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
XCTAssertEqual(0, try listener.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
XCTAssertNoThrow(try listener.setOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT), value: 5).wait())
XCTAssertEqual(1, try listener.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
XCTAssertNoThrow(try listener.setOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT), value: 0).wait())
XCTAssertEqual(0, try listener.getOption(ChannelOptions.socket(SOL_SOCKET, SO_REUSEPORT)).wait())
}
func testErrorsInChannelSetupAreFine() throws {
@ -166,7 +166,7 @@ class NIOTSListenerChannelTests: XCTestCase {
let listener = try NIOTSListenerBootstrap(group: self.group, childGroup: childGroup)
.childChannelInitializer { channel in
childChannelPromise.succeed(channel)
return channel.pipeline.add(handler: PromiseOnActiveHandler(activePromise))
return channel.pipeline.addHandler(PromiseOnActiveHandler(activePromise))
}.bind(host: "localhost", port: 0).wait()
defer {
XCTAssertNoThrow(try listener.close().wait())
@ -203,18 +203,18 @@ class NIOTSListenerChannelTests: XCTestCase {
func testCanObserveValueOfEnablePeerToPeer() throws {
let listener = try NIOTSListenerBootstrap(group: self.group)
.serverChannelInitializer { channel in
return channel.getOption(option: NIOTSChannelOptions.enablePeerToPeer).map { value in
return channel.getOption(NIOTSChannelOptions.enablePeerToPeer).map { value in
XCTAssertFalse(value)
}.then {
channel.setOption(option: NIOTSChannelOptions.enablePeerToPeer, value: true)
}.then {
channel.getOption(option: NIOTSChannelOptions.enablePeerToPeer)
}.flatMap {
channel.setOption(NIOTSChannelOptions.enablePeerToPeer, value: true)
}.flatMap {
channel.getOption(NIOTSChannelOptions.enablePeerToPeer)
}.map { value in
XCTAssertTrue(value)
}
}
.bind(host: "localhost", port: 0).wait()
defer {
do {
XCTAssertNoThrow(try listener.close().wait())
}
}

View File

@ -142,8 +142,8 @@ class NIOTSSocketOptionTests: XCTestCase {
do {
try self.options.applyChannelOption(option: option, value: 0)
} catch let err as NIOTSErrors.UnsupportedSocketOption {
XCTAssertEqual(err.optionValue.0, Int32.max)
XCTAssertEqual(err.optionValue.1, Int32.max)
XCTAssertEqual(err.optionValue.level, Int32.max)
XCTAssertEqual(err.optionValue.name, Int32.max)
} catch {
XCTFail("Unexpected error \(error)")
}
@ -155,8 +155,8 @@ class NIOTSSocketOptionTests: XCTestCase {
do {
_ = try self.options.valueFor(socketOption: option)
} catch let err as NIOTSErrors.UnsupportedSocketOption {
XCTAssertEqual(err.optionValue.0, Int32.max)
XCTAssertEqual(err.optionValue.1, Int32.max)
XCTAssertEqual(err.optionValue.level, Int32.max)
XCTAssertEqual(err.optionValue.name, Int32.max)
} catch {
XCTFail("Unexpected error \(error)")
}

View File

@ -21,11 +21,11 @@ import Network
private extension Channel {
private func getSocketOption(_ option: SocketOption) -> EventLoopFuture<SocketOptionValue> {
return self.getOption(option: ChannelOptions.socket(option.value.0, option.value.1))
return self.getOption(option)
}
private func setSocketOption(_ option: SocketOption, to value: SocketOptionValue) -> EventLoopFuture<Void> {
return self.setOption(option: ChannelOptions.socket(option.value.0, option.value.1), value: value)
return self.setOption(option, value: value)
}
/// Asserts that a given socket option has a default value, that its value can be changed to a new value, and that it can then be