From 684cad331cac8b14dadf826b5527e4b819addc8a Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Sat, 5 Jan 2019 08:08:47 +0000 Subject: [PATCH] EventLoopFuture: use Result type (#734) Motivation: Now that the stdlib has introduced the Result type, we can use it in the implementation (and the whenComplete) function of EventLoopFuture Modifications: - replace EventLoopValue with Result - make whenComplete provide the Result Result: use the new shiny stuff --- Sources/NIO/Bootstrap.swift | 2 +- Sources/NIO/ChannelHandlers.swift | 2 +- Sources/NIO/EventLoop.swift | 4 +- Sources/NIO/EventLoopFuture.swift | 26 +++------ Sources/NIO/HappyEyeballs.swift | 4 +- Sources/NIOHTTP1/HTTPUpgradeHandler.swift | 2 +- Sources/NIOHTTP1Server/main.swift | 6 +- ...pplicationProtocolNegotiationHandler.swift | 2 +- .../NIOWebSocket/WebSocketFrameDecoder.swift | 2 +- .../WebSocketProtocolErrorHandler.swift | 2 +- Sources/NIOWebSocketServer/main.swift | 6 +- .../HTTPResponseCompressorTest.swift | 2 +- .../NIOHTTP1Tests/HTTPServerClientTest.swift | 20 +++---- Tests/NIOTests/ChannelTests.swift | 2 +- Tests/NIOTests/EchoServerClientTest.swift | 8 +-- Tests/NIOTests/EmbeddedChannelTest.swift | 4 +- Tests/NIOTests/EmbeddedEventLoopTest.swift | 2 +- Tests/NIOTests/HappyEyeballsTest.swift | 2 +- .../PendingDatagramWritesManagerTests.swift | 55 +++++++++---------- Tests/NIOTests/SocketChannelTest.swift | 6 +- docs/public-api-changes-NIO1-to-NIO2.md | 1 + 21 files changed, 74 insertions(+), 86 deletions(-) diff --git a/Sources/NIO/Bootstrap.swift b/Sources/NIO/Bootstrap.swift index 1858728ae..d3cde7b4e 100644 --- a/Sources/NIO/Bootstrap.swift +++ b/Sources/NIO/Bootstrap.swift @@ -440,7 +440,7 @@ public final class ClientBootstrap { channel.close(promise: nil) } - connectPromise.futureResult.whenComplete { + connectPromise.futureResult.whenComplete { (_: Result) in cancelTask.cancel() } return connectPromise.futureResult diff --git a/Sources/NIO/ChannelHandlers.swift b/Sources/NIO/ChannelHandlers.swift index 775236a6f..727e46920 100644 --- a/Sources/NIO/ChannelHandlers.swift +++ b/Sources/NIO/ChannelHandlers.swift @@ -223,7 +223,7 @@ public class IdleStateHandler: ChannelDuplexHandler { } let writePromise = promise ?? ctx.eventLoop.makePromise() - writePromise.futureResult.whenComplete { + writePromise.futureResult.whenComplete { (_: Result) in self.lastWriteCompleteTime = DispatchTime.now() } ctx.write(data, promise: writePromise) diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index c077125cf..8b81aedff 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -116,7 +116,7 @@ public final class RepeatedTask { } scheduled.futureResult.whenSuccess { future in - future.whenComplete { + future.whenComplete { (_: Result) in self.reschedule0() } } @@ -957,7 +957,7 @@ final public class MultiThreadedEventLoopGroup: EventLoopGroup { g.enter() loop.closeGently().mapIfError { err in q.sync { error = err } - }.whenComplete { + }.whenComplete { (_: Result) in g.leave() } } diff --git a/Sources/NIO/EventLoopFuture.swift b/Sources/NIO/EventLoopFuture.swift index 601a95115..412710eb6 100644 --- a/Sources/NIO/EventLoopFuture.swift +++ b/Sources/NIO/EventLoopFuture.swift @@ -14,14 +14,6 @@ import NIOConcurrencyHelpers - -/// A `Result`-like type that is used to track the data through the -/// callback pipeline. -private enum EventLoopFutureValue { - case success(T) - case failure(Error) -} - /// Internal list of callbacks. /// /// Most of these are closures that pull a value from one future, call a user callback, push the @@ -187,7 +179,7 @@ public struct EventLoopPromise { /// /// - parameters: /// - value: The value to fire the future with. - private func _resolve(value: EventLoopFutureValue) { + private func _resolve(value: Result) { if futureResult.eventLoop.inEventLoop { _setValue(value: value)._run() } else { @@ -202,7 +194,7 @@ public struct EventLoopPromise { /// - parameters: /// - value: The result of the promise. /// - returns: The callback list to run. - fileprivate func _setValue(value: EventLoopFutureValue) -> CallbackList { + fileprivate func _setValue(value: Result) -> CallbackList { return futureResult._setValue(value: value) } } @@ -333,7 +325,7 @@ public struct EventLoopPromise { /// `EventLoopFuture` should be sufficient to guarantee thread-safety. public final class EventLoopFuture { // TODO: Provide a tracing facility. It would be nice to be able to set '.debugTrace = true' on any EventLoopFuture or EventLoopPromise and have every subsequent chained EventLoopFuture report the success result or failure error. That would simplify some debugging scenarios. - fileprivate var value: EventLoopFutureValue? { + fileprivate var value: Result? { didSet { _isFulfilled.store(true) } @@ -356,7 +348,7 @@ public final class EventLoopFuture { /// the entire chain from the top without recursing. fileprivate var callbacks: CallbackList = CallbackList() - private init(eventLoop: EventLoop, value: EventLoopFutureValue?, file: StaticString, line: UInt) { + private init(eventLoop: EventLoop, value: Result?, file: StaticString, line: UInt) { self.eventLoop = eventLoop self.value = value self._isFulfilled = UnsafeEmbeddedAtomic(value: value != nil) @@ -608,7 +600,7 @@ extension EventLoopFuture { } } - fileprivate func _whenCompleteWithValue(_ callback: @escaping (EventLoopFutureValue) -> Void) { + fileprivate func _whenCompleteWithValue(_ callback: @escaping (Result) -> Void) { _whenComplete { callback(self.value!) return CallbackList() @@ -662,16 +654,16 @@ extension EventLoopFuture { /// /// - parameters: /// - callback: The callback that is called when the `EventLoopFuture` is fulfilled. - public func whenComplete(_ callback: @escaping () -> Void) { + public func whenComplete(_ callback: @escaping (Result) -> Void) { _whenComplete { - callback() + callback(self.value!) return CallbackList() } } /// Internal: Set the value and return a list of callbacks that should be invoked as a result. - fileprivate func _setValue(value: EventLoopFutureValue) -> CallbackList { + fileprivate func _setValue(value: Result) -> CallbackList { self.eventLoop.assertInEventLoop() if self.value == nil { self.value = value @@ -813,7 +805,7 @@ Further information: precondition(MultiThreadedEventLoopGroup.currentEventLoop == nil, explainer(), file: file, line: line) } - var v: EventLoopFutureValue ? = nil + var v: Result? = nil let lock = ConditionLock(value: 0) _whenComplete { () -> CallbackList in lock.lock() diff --git a/Sources/NIO/HappyEyeballs.swift b/Sources/NIO/HappyEyeballs.swift index e020f5060..4520b3a1c 100644 --- a/Sources/NIO/HappyEyeballs.swift +++ b/Sources/NIO/HappyEyeballs.swift @@ -606,7 +606,7 @@ internal class HappyEyeballsConnector { self.targets.aResultsAvailable(results) }.mapIfError { err in self.error.dnsAError = err - }.whenComplete { + }.whenComplete { (_: Result) in self.dnsResolutions += 1 self.processInput(.resolverACompleted) } @@ -618,7 +618,7 @@ internal class HappyEyeballsConnector { self.targets.aaaaResultsAvailable(results) }.mapIfError { err in self.error.dnsAAAAError = err - }.whenComplete { + }.whenComplete { (_: Result) in // It's possible that we were waiting to time out here, so if we were we should // cancel that. self.resolutionTask?.cancel() diff --git a/Sources/NIOHTTP1/HTTPUpgradeHandler.swift b/Sources/NIOHTTP1/HTTPUpgradeHandler.swift index 763cfbff0..7476cca57 100644 --- a/Sources/NIOHTTP1/HTTPUpgradeHandler.swift +++ b/Sources/NIOHTTP1/HTTPUpgradeHandler.swift @@ -206,7 +206,7 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler { if bufferedMessages.count > 0 { ctx.fireChannelReadComplete() } - }.whenComplete { + }.whenComplete { (_: Result) in ctx.pipeline.remove(ctx: ctx, promise: nil) } } diff --git a/Sources/NIOHTTP1Server/main.swift b/Sources/NIOHTTP1Server/main.swift index 4ec5d3415..48811e7fc 100644 --- a/Sources/NIOHTTP1Server/main.swift +++ b/Sources/NIOHTTP1Server/main.swift @@ -358,7 +358,7 @@ private final class HTTPHandler: ChannelInboundHandler { } else { return ctx.close() } - }.whenComplete { + }.whenComplete { (_: Result) in _ = try? file.close() } case .sendfile: @@ -370,7 +370,7 @@ private final class HTTPHandler: ChannelInboundHandler { return p.futureResult }.thenIfError { (_: Error) in ctx.close() - }.whenComplete { + }.whenComplete { (_: Result) in _ = try? file.close() } } @@ -387,7 +387,7 @@ private final class HTTPHandler: ChannelInboundHandler { let promise = self.keepAlive ? promise : (promise ?? ctx.eventLoop.makePromise()) if !self.keepAlive { - promise!.futureResult.whenComplete { ctx.close(promise: nil) } + promise!.futureResult.whenComplete { (_: Result) in ctx.close(promise: nil) } } self.handler = nil diff --git a/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift b/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift index b33257ec2..651cb9674 100644 --- a/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift +++ b/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift @@ -118,7 +118,7 @@ public class ApplicationProtocolNegotiationHandler: ChannelInboundHandler { } let switchFuture = completionHandler(result) - switchFuture.whenComplete { + switchFuture.whenComplete { (_: Result) in self.unbuffer(context: context) context.pipeline.remove(handler: self, promise: nil) } diff --git a/Sources/NIOWebSocket/WebSocketFrameDecoder.swift b/Sources/NIOWebSocket/WebSocketFrameDecoder.swift index 8eed16a2e..1cb0dc62a 100644 --- a/Sources/NIOWebSocket/WebSocketFrameDecoder.swift +++ b/Sources/NIOWebSocket/WebSocketFrameDecoder.swift @@ -309,7 +309,7 @@ public final class WebSocketFrameDecoder: ByteToMessageDecoder { let frame = WebSocketFrame(fin: true, opcode: .connectionClose, data: data) - ctx.writeAndFlush(self.wrapInboundOut(frame)).whenComplete { + ctx.writeAndFlush(self.wrapInboundOut(frame)).whenComplete { (_: Result) in ctx.close(promise: nil) } } diff --git a/Sources/NIOWebSocket/WebSocketProtocolErrorHandler.swift b/Sources/NIOWebSocket/WebSocketProtocolErrorHandler.swift index c03051d00..bc9e63bb6 100644 --- a/Sources/NIOWebSocket/WebSocketProtocolErrorHandler.swift +++ b/Sources/NIOWebSocket/WebSocketProtocolErrorHandler.swift @@ -32,7 +32,7 @@ public final class WebSocketProtocolErrorHandler: ChannelInboundHandler { let frame = WebSocketFrame(fin: true, opcode: .connectionClose, data: data) - ctx.writeAndFlush(self.wrapOutboundOut(frame)).whenComplete { + ctx.writeAndFlush(self.wrapOutboundOut(frame)).whenComplete { (_: Result) in ctx.close(promise: nil) } } diff --git a/Sources/NIOWebSocketServer/main.swift b/Sources/NIOWebSocketServer/main.swift index f0e450d95..2ca893b77 100644 --- a/Sources/NIOWebSocketServer/main.swift +++ b/Sources/NIOWebSocketServer/main.swift @@ -81,7 +81,7 @@ private final class HTTPHandler: ChannelInboundHandler { headers: headers) ctx.write(self.wrapOutboundOut(.head(responseHead)), promise: nil) ctx.write(self.wrapOutboundOut(.body(.byteBuffer(self.responseBody))), promise: nil) - ctx.write(self.wrapOutboundOut(.end(nil))).whenComplete { + ctx.write(self.wrapOutboundOut(.end(nil))).whenComplete { (_: Result) in ctx.close(promise: nil) } ctx.flush() @@ -95,7 +95,7 @@ private final class HTTPHandler: ChannelInboundHandler { status: .methodNotAllowed, headers: headers) ctx.write(self.wrapOutboundOut(.head(head)), promise: nil) - ctx.write(self.wrapOutboundOut(.end(nil))).whenComplete { + ctx.write(self.wrapOutboundOut(.end(nil))).whenComplete { (_: Result) in ctx.close(promise: nil) } ctx.flush() @@ -194,7 +194,7 @@ private final class WebSocketTimeHandler: ChannelInboundHandler { var data = ctx.channel.allocator.buffer(capacity: 2) data.write(webSocketErrorCode: .protocolError) let frame = WebSocketFrame(fin: true, opcode: .connectionClose, data: data) - ctx.write(self.wrapOutboundOut(frame)).whenComplete { + ctx.write(self.wrapOutboundOut(frame)).whenComplete { (_: Result) in ctx.close(mode: .output, promise: nil) } awaitingClose = true diff --git a/Tests/NIOHTTP1Tests/HTTPResponseCompressorTest.swift b/Tests/NIOHTTP1Tests/HTTPResponseCompressorTest.swift index 3ac9935d8..d2872bd72 100644 --- a/Tests/NIOHTTP1Tests/HTTPResponseCompressorTest.swift +++ b/Tests/NIOHTTP1Tests/HTTPResponseCompressorTest.swift @@ -36,7 +36,7 @@ private class PromiseOrderer { let thisPromiseIndex = promiseArray.count promiseArray.append(promise) - promise.futureResult.whenComplete { + promise.futureResult.whenComplete { (_: Result) in let priorFutures = self.promiseArray[0..) in outbound.destructor() } ctx.write(self.wrapOutboundOut(.end(nil))).mapIfError { error in XCTFail("unexpected error \(error)") - }.whenComplete { + }.whenComplete { (_: Result) in self.sentEnd = true self.maybeClose(ctx: ctx) } @@ -148,13 +148,13 @@ class HTTPServerClientTest : XCTestCase { let outbound = self.outboundBody(b) ctx.write(self.wrapOutboundOut(outbound.body)).mapIfError { error in XCTFail("unexpected error \(error)") - }.whenComplete { + }.whenComplete { (_: Result) in outbound.destructor() } } ctx.write(self.wrapOutboundOut(.end(nil))).mapIfError { error in XCTFail("unexpected error \(error)") - }.whenComplete { + }.whenComplete { (_: Result) in self.sentEnd = true self.maybeClose(ctx: ctx) } @@ -174,7 +174,7 @@ class HTTPServerClientTest : XCTestCase { let outbound = self.outboundBody(b) ctx.write(self.wrapOutboundOut(outbound.body)).mapIfError { error in XCTFail("unexpected error \(error)") - }.whenComplete { + }.whenComplete { (_: Result) in outbound.destructor() } } @@ -184,7 +184,7 @@ class HTTPServerClientTest : XCTestCase { trailers.add(name: "X-Should-Trail", value: "sure") ctx.write(self.wrapOutboundOut(.end(trailers))).mapIfError { error in XCTFail("unexpected error \(error)") - }.whenComplete { + }.whenComplete { (_: Result) in self.sentEnd = true self.maybeClose(ctx: ctx) } @@ -209,12 +209,12 @@ class HTTPServerClientTest : XCTestCase { let outbound = self.outboundBody(buf) ctx.writeAndFlush(self.wrapOutboundOut(outbound.body)).mapIfError { error in XCTFail("unexpected error \(error)") - }.whenComplete { + }.whenComplete { (_: Result) in outbound.destructor() } ctx.write(self.wrapOutboundOut(.end(nil))).mapIfError { error in XCTFail("unexpected error \(error)") - }.whenComplete { + }.whenComplete { (_: Result) in self.sentEnd = true self.maybeClose(ctx: ctx) } @@ -227,7 +227,7 @@ class HTTPServerClientTest : XCTestCase { } ctx.write(self.wrapOutboundOut(.end(nil))).mapIfError { error in XCTFail("unexpected error \(error)") - }.whenComplete { + }.whenComplete { (_: Result) in self.sentEnd = true self.maybeClose(ctx: ctx) } @@ -239,7 +239,7 @@ class HTTPServerClientTest : XCTestCase { } ctx.write(self.wrapOutboundOut(.end(nil))).mapIfError { error in XCTFail("unexpected error \(error)") - }.whenComplete { + }.whenComplete { (_: Result) in self.sentEnd = true self.maybeClose(ctx: ctx) } diff --git a/Tests/NIOTests/ChannelTests.swift b/Tests/NIOTests/ChannelTests.swift index 748845770..1fd8aa8c9 100644 --- a/Tests/NIOTests/ChannelTests.swift +++ b/Tests/NIOTests/ChannelTests.swift @@ -972,7 +972,7 @@ public class ChannelTests: XCTestCase { pwm.markFlushCheckpoint() _ = pwm.add(data: .byteBuffer(buffer), promise: ps[2]) - ps[0].futureResult.whenComplete { + ps[0].futureResult.whenComplete { (_: Result) in pwm.failAll(error: ChannelError.inputClosed, close: true) } diff --git a/Tests/NIOTests/EchoServerClientTest.swift b/Tests/NIOTests/EchoServerClientTest.swift index e3681e8a5..321f9193c 100644 --- a/Tests/NIOTests/EchoServerClientTest.swift +++ b/Tests/NIOTests/EchoServerClientTest.swift @@ -350,7 +350,7 @@ class EchoServerClientTest : XCTestCase { default: XCTFail("unexpected error: \(err)") } - }.whenComplete { + }.whenComplete { (_: Result) in self.channelInactivePromise.succeed(result: ()) } } @@ -370,7 +370,7 @@ class EchoServerClientTest : XCTestCase { default: XCTFail("unexpected error: \(err)") } - }.whenComplete { + }.whenComplete { (_: Result) in self.channelUnregisteredPromise.succeed(result: ()) } } @@ -603,7 +603,7 @@ class EchoServerClientTest : XCTestCase { } private func writeUntilFailed(_ ctx: ChannelHandlerContext, _ buffer: ByteBuffer) { - ctx.writeAndFlush(NIOAny(buffer)).whenComplete { + ctx.writeAndFlush(NIOAny(buffer)).whenComplete { (_: Result) in ctx.eventLoop.execute { self.writeUntilFailed(ctx, buffer) } @@ -635,7 +635,7 @@ class EchoServerClientTest : XCTestCase { ctx.writeAndFlush(NIOAny(buffer)) }.then { ctx.close() - }.whenComplete { + }.whenComplete { (_: Result) in self.dpGroup.leave() } } diff --git a/Tests/NIOTests/EmbeddedChannelTest.swift b/Tests/NIOTests/EmbeddedChannelTest.swift index 84a250e01..0dc38b608 100644 --- a/Tests/NIOTests/EmbeddedChannelTest.swift +++ b/Tests/NIOTests/EmbeddedChannelTest.swift @@ -160,14 +160,14 @@ class EmbeddedChannelTest: XCTestCase { let channel = EmbeddedChannel() XCTAssertFalse(channel.isActive) let connectPromise = channel.eventLoop.makePromise(of: Void.self) - connectPromise.futureResult.whenComplete { + connectPromise.futureResult.whenComplete { (_: Result) in XCTAssertTrue(channel.isActive) } channel.connect(to: try SocketAddress(ipAddress: "127.0.0.1", port: 0), promise: connectPromise) try connectPromise.futureResult.wait() let closePromise = channel.eventLoop.makePromise(of: Void.self) - closePromise.futureResult.whenComplete { + closePromise.futureResult.whenComplete { (_: Result) in XCTAssertFalse(channel.isActive) } diff --git a/Tests/NIOTests/EmbeddedEventLoopTest.swift b/Tests/NIOTests/EmbeddedEventLoopTest.swift index 4602a1e7e..f9f99e4c2 100644 --- a/Tests/NIOTests/EmbeddedEventLoopTest.swift +++ b/Tests/NIOTests/EmbeddedEventLoopTest.swift @@ -215,7 +215,7 @@ public class EmbeddedEventLoopTest: XCTestCase { XCTFail("Scheduled future completed") }.mapIfError { caughtErr in XCTAssertTrue(err === caughtErr as? EmbeddedTestError) - }.whenComplete { + }.whenComplete { (_: Result) in fired = true } diff --git a/Tests/NIOTests/HappyEyeballsTest.swift b/Tests/NIOTests/HappyEyeballsTest.swift index 551835cfe..b30e1e91e 100644 --- a/Tests/NIOTests/HappyEyeballsTest.swift +++ b/Tests/NIOTests/HappyEyeballsTest.swift @@ -72,7 +72,7 @@ private class ConnectRecorder: ChannelOutboundHandler { public func close(ctx: ChannelHandlerContext, mode: CloseMode, promise: EventLoopPromise?) { let connectPromise = promise ?? ctx.eventLoop.makePromise() - connectPromise.futureResult.whenComplete { + connectPromise.futureResult.whenComplete { (_: Result) in self.state = .closed } ctx.close(promise: connectPromise) diff --git a/Tests/NIOTests/PendingDatagramWritesManagerTests.swift b/Tests/NIOTests/PendingDatagramWritesManagerTests.swift index 490dd4e4b..86e6d4ab9 100644 --- a/Tests/NIOTests/PendingDatagramWritesManagerTests.swift +++ b/Tests/NIOTests/PendingDatagramWritesManagerTests.swift @@ -50,11 +50,6 @@ private extension SocketAddress { } class PendingDatagramWritesManagerTests: XCTestCase { - private enum FakeWriteResult { - case ok(IOResult) - case error(Error) - } - private func withPendingDatagramWritesManager(_ body: (PendingDatagramWritesManager) throws -> Void) rethrows { try withExtendedLifetime(NSObject()) { o in var iovecs: [IOVector] = Array(repeating: iovec(), count: Socket.writevLimitIOVectors + 1) @@ -107,7 +102,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: [EventLoopPromise], expectedSingleWritabilities: [(Int, SocketAddress)]?, expectedVectorWritabilities: [[(Int, SocketAddress)]]?, - returns: [FakeWriteResult], + returns: [Result, Error>], promiseStates: [[Bool]], file: StaticString = #file, line: UInt = #line) throws -> OverallWriteResult { @@ -131,9 +126,9 @@ class PendingDatagramWritesManagerTests: XCTestCase { XCTAssertEqual(expected[singleState].1.expectedSize, len, "in single write \(singleState) (overall \(everythingState)), \(expected[singleState].1.expectedSize) socklen expected but \(len) received", file: file, line: line) switch returns[everythingState] { - case .ok(let r): + case .success(let r): return r - case .error(let e): + case .failure(let e): throw e } } else { @@ -165,9 +160,9 @@ class PendingDatagramWritesManagerTests: XCTestCase { file:file, line: line) switch returns[everythingState] { - case .ok(let r): + case .success(let r): return r - case .error(let e): + case .failure(let e): throw e } } else { @@ -248,7 +243,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: [(0, address)], expectedVectorWritabilities: nil, - returns: [.ok(.processed(0))], + returns: [.success(.processed(0))], promiseStates: [[true, false]]) XCTAssertFalse(pwm.isEmpty) @@ -269,7 +264,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: [(0, address)], expectedVectorWritabilities: nil, - returns: [.ok(.processed(0))], + returns: [.success(.processed(0))], promiseStates: [[true, true]]) XCTAssertEqual(.writtenCompletely, result) } @@ -296,7 +291,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: nil, expectedVectorWritabilities: [[(4, firstAddress), (4, secondAddress)]], - returns: [.ok(.processed(2))], + returns: [.success(.processed(2))], promiseStates: [[true, true, false]]) XCTAssertEqual(.writtenCompletely, result) @@ -306,7 +301,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: [(0, firstAddress)], expectedVectorWritabilities: nil, - returns: [.ok(.processed(0))], + returns: [.success(.processed(0))], promiseStates: [[true, true, true]]) XCTAssertEqual(.writtenCompletely, result) } @@ -336,7 +331,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { [(4, firstAddress), (4, secondAddress), (4, firstAddress), (4, secondAddress)], [(4, secondAddress), (4, firstAddress), (4, secondAddress)] ], - returns: [.ok(.processed(1)), .ok(.wouldBlock(0))], + returns: [.success(.processed(1)), .success(.wouldBlock(0))], promiseStates: [[true, false, false, false], [true, false, false, false]]) XCTAssertEqual(.couldNotWriteEverything, result) @@ -346,7 +341,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { expectedVectorWritabilities: [ [(4, secondAddress), (4, firstAddress), (4, secondAddress)], ], - returns: [.ok(.processed(2)), .ok(.wouldBlock(0))], + returns: [.success(.processed(2)), .success(.wouldBlock(0))], promiseStates: [[true, true, true, false], [true, true, true, false]] ) @@ -356,7 +351,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: [(4, secondAddress)], expectedVectorWritabilities: nil, - returns: [.ok(.processed(4))], + returns: [.success(.processed(4))], promiseStates: [[true, true, true, true]]) XCTAssertEqual(.writtenCompletely, result) } @@ -386,7 +381,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: nil, expectedVectorWritabilities: Array(actualVectorWritabilities), - returns: Array(repeating: .ok(.processed(1)), count: ps.count - 1), + returns: Array(repeating: .success(.processed(1)), count: ps.count - 1), promiseStates: actualPromiseStates) XCTAssertEqual(.couldNotWriteEverything, result) @@ -395,7 +390,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: [(12, address)], expectedVectorWritabilities: nil, - returns: [.ok(.processed(12))], + returns: [.success(.processed(12))], promiseStates: [Array(repeating: true, count: ps.count - 1) + [true]]) XCTAssertEqual(.writtenCompletely, result) } @@ -420,7 +415,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: nil, expectedVectorWritabilities: [[(4, address), (4, address)]], - returns: [.ok(.wouldBlock(0))], + returns: [.success(.wouldBlock(0))], promiseStates: [[false, false, false], [false, false, false]]) XCTAssertEqual(.couldNotWriteEverything, result) @@ -456,7 +451,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: [(halfTheWriteVLimit, address)], expectedVectorWritabilities: [[(halfTheWriteVLimit, address), (halfTheWriteVLimit, address)]], - returns: [.ok(.processed(2)), .ok(.processed(1))], + returns: [.success(.processed(2)), .success(.processed(1))], promiseStates: [[true, true, false], [true, true, true]]) XCTAssertEqual(.writtenCompletely, result) } @@ -496,9 +491,9 @@ class PendingDatagramWritesManagerTests: XCTestCase { (Socket.writevLimitBytes - 77, address)], expectedVectorWritabilities: [[(Socket.writevLimitBytes - 77, address)]], returns: [ - .error(IOError(errnoCode: EMSGSIZE, reason: "")), - .ok(.processed(1)), - .ok(.processed(1))], + .failure(IOError(errnoCode: EMSGSIZE, reason: "")), + .success(.processed(1)), + .success(.processed(1))], promiseStates: [[true, false, false], [true, true, false], [true, true, true]]) XCTAssertEqual(.writtenCompletely, result) @@ -534,7 +529,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: nil, expectedVectorWritabilities: [[(0, address), (0, address)]], - returns: [.ok(.processed(2))], + returns: [.success(.processed(2))], promiseStates: [[true, true, false]]) XCTAssertEqual(.writtenCompletely, result) @@ -544,7 +539,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: [(0, address)], expectedVectorWritabilities: nil, - returns: [.ok(.processed(0))], + returns: [.success(.processed(0))], promiseStates: [[true, true, true]]) XCTAssertEqual(.writtenCompletely, result) } @@ -564,7 +559,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { pwm.markFlushCheckpoint() _ = pwm.add(envelope: AddressedEnvelope(remoteAddress: address, data: buffer), promise: ps[2]) - ps[0].futureResult.whenComplete { + ps[0].futureResult.whenComplete { (_: Result) in pwm.failAll(error: ChannelError.inputClosed, close: true) } @@ -572,7 +567,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: nil, expectedVectorWritabilities: [[(4, address), (4, address)]], - returns: [.ok(.processed(1))], + returns: [.success(.processed(1))], promiseStates: [[true, true, true]]) XCTAssertEqual(.writtenCompletely, result) XCTAssertNoThrow(try ps[0].futureResult.wait()) @@ -599,7 +594,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: [(4, address)], expectedVectorWritabilities: [Array(repeating: (4, address), count: Socket.writevLimitIOVectors)], - returns: [.ok(.processed(Socket.writevLimitIOVectors)), .ok(.wouldBlock(0))], + returns: [.success(.processed(Socket.writevLimitIOVectors)), .success(.wouldBlock(0))], promiseStates: [Array(repeating: true, count: Socket.writevLimitIOVectors) + [false], Array(repeating: true, count: Socket.writevLimitIOVectors) + [false]]) XCTAssertEqual(.couldNotWriteEverything, result) @@ -607,7 +602,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { promises: ps, expectedSingleWritabilities: [(4, address)], expectedVectorWritabilities: nil, - returns: [.ok(.processed(4))], + returns: [.success(.processed(4))], promiseStates: [Array(repeating: true, count: Socket.writevLimitIOVectors + 1)]) XCTAssertEqual(.writtenCompletely, result) } diff --git a/Tests/NIOTests/SocketChannelTest.swift b/Tests/NIOTests/SocketChannelTest.swift index 6eba1495d..f2da6620d 100644 --- a/Tests/NIOTests/SocketChannelTest.swift +++ b/Tests/NIOTests/SocketChannelTest.swift @@ -445,10 +445,10 @@ public class SocketChannelTest : XCTestCase { let connectPromise = channel.eventLoop.makePromise(of: Void.self) let closePromise = channel.eventLoop.makePromise(of: Void.self) - closePromise.futureResult.whenComplete { + closePromise.futureResult.whenComplete { (_: Result) in XCTAssertTrue(connectPromise.futureResult.isFulfilled) } - connectPromise.futureResult.whenComplete { + connectPromise.futureResult.whenComplete { (_: Result) in XCTAssertFalse(closePromise.futureResult.isFulfilled) } @@ -510,7 +510,7 @@ public class SocketChannelTest : XCTestCase { XCTAssertEqual(.inactive, state) state = .removed - ctx.channel.closeFuture.whenComplete { + ctx.channel.closeFuture.whenComplete { (_: Result) in XCTAssertNil(ctx.localAddress) XCTAssertNil(ctx.remoteAddress) diff --git a/docs/public-api-changes-NIO1-to-NIO2.md b/docs/public-api-changes-NIO1-to-NIO2.md index 994026b8e..a1d800f1f 100644 --- a/docs/public-api-changes-NIO1-to-NIO2.md +++ b/docs/public-api-changes-NIO1-to-NIO2.md @@ -28,3 +28,4 @@ - `ByteBuffer.write(string:)` no longer returns an `Int?`, instead it returns `Int` and has had its return value made discardable. - removed ContiguousCollection +- EventLoopFuture.whenComplete now provides Result