port to latest NIO 2 APIs (#20)

Motivation:

Compiling code is good so let's make it compile again with the latest
NIO APIs.

Modifications:
- made NIOTS use `_NIO1APIShims`
- clicked 'Fix All' a couple of times to adjust the API

Result:
Code compiles again
This commit is contained in:
Johannes Weiss 2019-01-22 15:38:13 +00:00 committed by Cory Benfield
parent 9fa52bd443
commit 4587afdf46
7 changed files with 54 additions and 53 deletions

View File

@ -30,12 +30,12 @@ let package = Package(
], ],
targets: [ targets: [
.target(name: "NIOTransportServices", .target(name: "NIOTransportServices",
dependencies: ["NIO", "NIOFoundationCompat", "NIOConcurrencyHelpers", "NIOTLS"]), dependencies: ["NIO", "NIOFoundationCompat", "NIOConcurrencyHelpers", "NIOTLS", "_NIO1APIShims"]),
.target(name: "NIOTSHTTPClient", .target(name: "NIOTSHTTPClient",
dependencies: ["NIO", "NIOTransportServices", "NIOHTTP1"]), dependencies: ["NIO", "NIOTransportServices", "NIOHTTP1", "_NIO1APIShims"]),
.target(name: "NIOTSHTTPServer", .target(name: "NIOTSHTTPServer",
dependencies: ["NIO", "NIOTransportServices", "NIOHTTP1"]), dependencies: ["NIO", "NIOTransportServices", "NIOHTTP1", "_NIO1APIShims"]),
.testTarget(name: "NIOTransportServicesTests", .testTarget(name: "NIOTransportServicesTests",
dependencies: ["NIO", "NIOTransportServices"]), dependencies: ["NIO", "NIOTransportServices", "_NIO1APIShims"]),
] ]
) )

View File

@ -150,11 +150,11 @@ public final class NIOTSConnectionBootstrap {
let channelOptions = self.channelOptions let channelOptions = self.channelOptions
return conn.eventLoop.submit { return conn.eventLoop.submit {
return channelOptions.applyAll(channel: conn).then { return channelOptions.applyAll(channel: conn).flatMap {
initializer(conn) initializer(conn)
}.then { }.flatMap {
conn.register() conn.register()
}.then { }.flatMap {
let connectPromise: EventLoopPromise<Void> = conn.eventLoop.makePromise() let connectPromise: EventLoopPromise<Void> = conn.eventLoop.makePromise()
connectAction(conn, connectPromise) connectAction(conn, connectPromise)
let cancelTask = conn.eventLoop.scheduleTask(in: self.connectTimeout) { let cancelTask = conn.eventLoop.scheduleTask(in: self.connectTimeout) {
@ -166,11 +166,11 @@ public final class NIOTSConnectionBootstrap {
cancelTask.cancel() cancelTask.cancel()
} }
return connectPromise.futureResult return connectPromise.futureResult
}.map { conn }.thenIfErrorThrowing { }.map { conn }.flatMapErrorThrowing {
conn.close(promise: nil) conn.close(promise: nil)
throw $0 throw $0
} }
}.then { $0 } }.flatMap { $0 }
} }
} }

View File

@ -166,7 +166,7 @@ extension NIOTSEventLoop {
// We need to tell all currently-registered channels to close. // We need to tell all currently-registered channels to close.
let futures: [EventLoopFuture<Void>] = self.registeredChannels.map { _, channel in let futures: [EventLoopFuture<Void>] = self.registeredChannels.map { _, channel in
channel.close(promise: nil) channel.close(promise: nil)
return channel.closeFuture.thenIfErrorThrowing { error in return channel.closeFuture.flatMapErrorThrowing { error in
if let error = error as? ChannelError, error == .alreadyClosed { if let error = error as? ChannelError, error == .alreadyClosed {
return () return ()
} else { } else {

View File

@ -16,7 +16,7 @@
import NIO import NIO
import Dispatch import Dispatch
import Network import Network
import _NIO1APIShims
public final class NIOTSListenerBootstrap { public final class NIOTSListenerBootstrap {
private let group: EventLoopGroup private let group: EventLoopGroup
@ -210,26 +210,26 @@ public final class NIOTSListenerBootstrap {
tlsOptions: self.tlsOptions) tlsOptions: self.tlsOptions)
return eventLoop.submit { return eventLoop.submit {
return serverChannelOptions.applyAll(channel: serverChannel).then { return serverChannelOptions.applyAll(channel: serverChannel).flatMap {
serverChannelInit(serverChannel) serverChannelInit(serverChannel)
}.then { }.flatMap {
serverChannel.pipeline.add(handler: AcceptHandler(childChannelInitializer: childChannelInit, serverChannel.pipeline.add(handler: AcceptHandler(childChannelInitializer: childChannelInit,
childGroup: childEventLoopGroup, childGroup: childEventLoopGroup,
childChannelOptions: childChannelOptions, childChannelOptions: childChannelOptions,
childChannelQoS: self.childQoS, childChannelQoS: self.childQoS,
tcpOptions: self.tcpOptions, tcpOptions: self.tcpOptions,
tlsOptions: self.tlsOptions)) tlsOptions: self.tlsOptions))
}.then { }.flatMap {
serverChannel.register() serverChannel.register()
}.then { }.flatMap {
binder(serverChannel) binder(serverChannel)
}.map { }.map {
serverChannel as Channel serverChannel as Channel
}.thenIfError { error in }.flatMapError { error in
serverChannel.close0(error: error, mode: .all, promise: nil) serverChannel.close0(error: error, mode: .all, promise: nil)
return eventLoop.makeFailedFuture(error: error) return eventLoop.makeFailedFuture(error: error)
} }
}.then { }.flatMap {
$0 $0
} }
} }
@ -275,7 +275,7 @@ private class AcceptHandler: ChannelInboundHandler {
@inline(__always) @inline(__always)
func setupChildChannel() -> EventLoopFuture<Void> { func setupChildChannel() -> EventLoopFuture<Void> {
return self.childChannelOptions.applyAll(channel: newChannel).then { () -> EventLoopFuture<Void> in return self.childChannelOptions.applyAll(channel: newChannel).flatMap { () -> EventLoopFuture<Void> in
childLoop.assertInEventLoop() childLoop.assertInEventLoop()
return childInitializer(newChannel) return childInitializer(newChannel)
} }
@ -284,10 +284,10 @@ private class AcceptHandler: ChannelInboundHandler {
@inline(__always) @inline(__always)
func fireThroughPipeline(_ future: EventLoopFuture<Void>) { func fireThroughPipeline(_ future: EventLoopFuture<Void>) {
ctxEventLoop.assertInEventLoop() ctxEventLoop.assertInEventLoop()
future.then { (_) -> EventLoopFuture<Void> in future.flatMap { (_) -> EventLoopFuture<Void> in
ctxEventLoop.assertInEventLoop() ctxEventLoop.assertInEventLoop()
guard ctx.channel.isActive else { guard ctx.channel.isActive else {
return newChannel.close().thenThrowing { return newChannel.close().flatMapThrowing {
throw ChannelError.ioOnClosedChannel throw ChannelError.ioOnClosedChannel
} }
} }
@ -305,7 +305,7 @@ private class AcceptHandler: ChannelInboundHandler {
} else { } else {
fireThroughPipeline(childLoop.submit { fireThroughPipeline(childLoop.submit {
return setupChildChannel() return setupChildChannel()
}.then { $0 }.hopTo(eventLoop: ctxEventLoop)) }.flatMap { $0 }.hopTo(eventLoop: ctxEventLoop))
} }
} }
} }

View File

@ -18,6 +18,7 @@ import Network
import NIO import NIO
import NIOTransportServices import NIOTransportServices
import Foundation import Foundation
import _NIO1APIShims
final class ConnectRecordingHandler: ChannelOutboundHandler { final class ConnectRecordingHandler: ChannelOutboundHandler {
@ -75,7 +76,7 @@ final class DisableWaitingAfterConnect: ChannelOutboundHandler {
func connect(ctx: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) { func connect(ctx: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
let f = ctx.channel.setOption(option: NIOTSChannelOptions.waitForActivity, value: false).then { let f = ctx.channel.setOption(option: NIOTSChannelOptions.waitForActivity, value: false).flatMap {
ctx.connect(to: address) ctx.connect(to: address)
} }
if let promise = promise { if let promise = promise {
@ -255,12 +256,12 @@ class NIOTSConnectionChannelTests: XCTestCase {
XCTAssertNoThrow(try connection.close().wait()) XCTAssertNoThrow(try connection.close().wait())
} }
try connection.getOption(option: ChannelOptions.writeBufferWaterMark).then { option -> EventLoopFuture<Void> in try connection.getOption(option: ChannelOptions.writeBufferWaterMark).flatMap { option -> EventLoopFuture<Void> in
XCTAssertEqual(option.high, 64 * 1024) XCTAssertEqual(option.high, 64 * 1024)
XCTAssertEqual(option.low, 32 * 1024) XCTAssertEqual(option.low, 32 * 1024)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 1, high: 101)) return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 1, high: 101))
}.then { }.flatMap {
connection.getOption(option: ChannelOptions.writeBufferWaterMark) connection.getOption(option: ChannelOptions.writeBufferWaterMark)
}.map { }.map {
XCTAssertEqual($0.high, 101) XCTAssertEqual($0.high, 101)
@ -422,31 +423,31 @@ class NIOTSConnectionChannelTests: XCTestCase {
XCTAssertTrue(connection.isWritable) XCTAssertTrue(connection.isWritable)
}.wait() }.wait()
try connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 256)).then { try connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 256)).flatMap {
// High to 256, low to 128. No writability change. // High to 256, low to 128. No writability change.
XCTAssertEqual(writabilities, []) XCTAssertEqual(writabilities, [])
XCTAssertTrue(connection.isWritable) XCTAssertTrue(connection.isWritable)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 255)) return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 255))
}.then { }.flatMap {
// High to 255, low to 127. Channel becomes not writable. // High to 255, low to 127. Channel becomes not writable.
XCTAssertEqual(writabilities, [false]) XCTAssertEqual(writabilities, [false])
XCTAssertFalse(connection.isWritable) XCTAssertFalse(connection.isWritable)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 256)) return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 128, high: 256))
}.then { }.flatMap {
// High back to 256, low to 128. No writability change. // High back to 256, low to 128. No writability change.
XCTAssertEqual(writabilities, [false]) XCTAssertEqual(writabilities, [false])
XCTAssertFalse(connection.isWritable) XCTAssertFalse(connection.isWritable)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 256, high: 1024)) return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 256, high: 1024))
}.then { }.flatMap {
// High to 1024, low to 128. No writability change. // High to 1024, low to 128. No writability change.
XCTAssertEqual(writabilities, [false]) XCTAssertEqual(writabilities, [false])
XCTAssertFalse(connection.isWritable) XCTAssertFalse(connection.isWritable)
return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 257, high: 1024)) return connection.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 257, high: 1024))
}.then { }.flatMap {
// Low to 257, channel becomes writable again. // Low to 257, channel becomes writable again.
XCTAssertEqual(writabilities, [false, true]) XCTAssertEqual(writabilities, [false, true])
XCTAssertTrue(connection.isWritable) XCTAssertTrue(connection.isWritable)
@ -565,9 +566,9 @@ class NIOTSConnectionChannelTests: XCTestCase {
.channelInitializer { channel in .channelInitializer { channel in
return channel.getOption(option: NIOTSChannelOptions.waitForActivity).map { value in return channel.getOption(option: NIOTSChannelOptions.waitForActivity).map { value in
XCTAssertTrue(value) XCTAssertTrue(value)
}.then { }.flatMap {
channel.setOption(option: NIOTSChannelOptions.waitForActivity, value: false) channel.setOption(option: NIOTSChannelOptions.waitForActivity, value: false)
}.then { }.flatMap {
channel.getOption(option: NIOTSChannelOptions.waitForActivity) channel.getOption(option: NIOTSChannelOptions.waitForActivity)
}.map { value in }.map { value in
XCTAssertFalse(value) XCTAssertFalse(value)

View File

@ -148,7 +148,7 @@ extension Channel {
/// Expect that the given bytes will be received. /// Expect that the given bytes will be received.
func expectRead(_ bytes: ByteBuffer) -> EventLoopFuture<Void> { func expectRead(_ bytes: ByteBuffer) -> EventLoopFuture<Void> {
let expecter = ReadExpecter(expecting: bytes) let expecter = ReadExpecter(expecting: bytes)
return self.pipeline.add(handler: expecter).then { return self.pipeline.add(handler: expecter).flatMap {
return expecter.readFuture! return expecter.readFuture!
} }
} }
@ -204,7 +204,7 @@ class NIOTSEndToEndTests: XCTestCase {
let bootstrap = NIOTSConnectionBootstrap(group: self.group) let bootstrap = NIOTSConnectionBootstrap(group: self.group)
let completeFutures: [EventLoopFuture<Void>] = (0..<10).map { _ in let completeFutures: [EventLoopFuture<Void>] = (0..<10).map { _ in
return bootstrap.connect(to: listener.localAddress!).then { channel -> EventLoopFuture<Void> in return bootstrap.connect(to: listener.localAddress!).flatMap { channel -> EventLoopFuture<Void> in
let buffer = channel.allocator.bufferFor(string: "hello, world!") let buffer = channel.allocator.bufferFor(string: "hello, world!")
let completeFuture = channel.expectRead(buffer) let completeFuture = channel.expectRead(buffer)
channel.writeAndFlush(buffer, promise: nil) channel.writeAndFlush(buffer, promise: nil)
@ -227,7 +227,7 @@ class NIOTSEndToEndTests: XCTestCase {
let bootstrap = NIOTSConnectionBootstrap(group: self.group) let bootstrap = NIOTSConnectionBootstrap(group: self.group)
let closeFutures: [EventLoopFuture<Void>] = (0..<10).map { _ in let closeFutures: [EventLoopFuture<Void>] = (0..<10).map { _ in
bootstrap.connect(to: listener.localAddress!).then { channel in bootstrap.connect(to: listener.localAddress!).flatMap { channel in
channel.closeFuture channel.closeFuture
} }
} }
@ -306,7 +306,7 @@ class NIOTSEndToEndTests: XCTestCase {
let halfClosedPromise: EventLoopPromise<Void> = self.group.next().makePromise() let halfClosedPromise: EventLoopPromise<Void> = self.group.next().makePromise()
let listener = try NIOTSListenerBootstrap(group: self.group) let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in .childChannelInitializer { channel in
channel.pipeline.add(handler: EchoHandler()).then { _ in channel.pipeline.add(handler: EchoHandler()).flatMap { _ in
channel.pipeline.add(handler: HalfCloseHandler(halfClosedPromise)) channel.pipeline.add(handler: HalfCloseHandler(halfClosedPromise))
} }
} }
@ -336,7 +336,7 @@ class NIOTSEndToEndTests: XCTestCase {
func testDisabledHalfClosureCausesFullClosure() throws { func testDisabledHalfClosureCausesFullClosure() throws {
let listener = try NIOTSListenerBootstrap(group: self.group) let listener = try NIOTSListenerBootstrap(group: self.group)
.childChannelInitializer { channel in .childChannelInitializer { channel in
channel.pipeline.add(handler: EchoHandler()).then { _ in channel.pipeline.add(handler: EchoHandler()).flatMap { _ in
channel.pipeline.add(handler: FailOnHalfCloseHandler()) channel.pipeline.add(handler: FailOnHalfCloseHandler())
} }
} }

View File

@ -31,15 +31,15 @@ private extension Channel {
/// 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 /// 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
/// switched back. /// switched back.
func assertOptionRoundTrips(option: SocketOption, initialValue: SocketOptionValue, testAlternativeValue: SocketOptionValue) -> EventLoopFuture<Void> { func assertOptionRoundTrips(option: SocketOption, initialValue: SocketOptionValue, testAlternativeValue: SocketOptionValue) -> EventLoopFuture<Void> {
return self.getSocketOption(option).then { actualInitialValue in return self.getSocketOption(option).flatMap { actualInitialValue in
XCTAssertEqual(actualInitialValue, initialValue) XCTAssertEqual(actualInitialValue, initialValue)
return self.setSocketOption(option, to: testAlternativeValue) return self.setSocketOption(option, to: testAlternativeValue)
}.then { }.flatMap {
self.getSocketOption(option) self.getSocketOption(option)
}.then { actualNewValue in }.flatMap { actualNewValue in
XCTAssertEqual(actualNewValue, testAlternativeValue) XCTAssertEqual(actualNewValue, testAlternativeValue)
return self.setSocketOption(option, to: initialValue) return self.setSocketOption(option, to: initialValue)
}.then { }.flatMap {
self.getSocketOption(option) self.getSocketOption(option)
}.map { returnedToValue in }.map { returnedToValue in
XCTAssertEqual(returnedToValue, initialValue) XCTAssertEqual(returnedToValue, initialValue)