From 9b9dcebecfabf9a3f341d9a54ff4bfd832cbce7a Mon Sep 17 00:00:00 2001 From: Nathan Harris Date: Mon, 4 Feb 2019 09:43:02 -0800 Subject: [PATCH] add `ELF.whenAllSucceed` method to act as "fail fast" counter-part to `ELF.whenAllComplete` (#804) Motivation: `ELF.whenAllComplete` is a "fail slow" method that returns an `EventLoopFuture` that always succeeds after all provided futures complete - fulfilling with all the results of the futures. `ELF.whenAllSucceed` is a "fail fast" method that returns an `EventLoopFuture` that succeeds if all of the futures succeed - and returning the fulfilled values. This new method acts as a counterpart to both `ELF.whenAllComplete` and `andAll` Modifications: Add `ELF.whenAllSucceed` that returns an EventLoopFuture with all of the values provided from the futures being awaited. Rename `ELF.whenAllComplete(_:eventLoop:)` to `ELF.whenAllComplete(_:on:)` to match new `whenAllSucceed` and Swift API guidelines Result: New method for flattening multiple futures into a single EventLoopFuture that fulfills with an array of their success values if they all succeed. --- Sources/NIO/EventLoopFuture.swift | 20 ++++++++++++++++++-- Tests/NIOTests/EventLoopFutureTest.swift | 6 +++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Sources/NIO/EventLoopFuture.swift b/Sources/NIO/EventLoopFuture.swift index a47b5778c..3bd7f2779 100644 --- a/Sources/NIO/EventLoopFuture.swift +++ b/Sources/NIO/EventLoopFuture.swift @@ -986,7 +986,21 @@ extension EventLoopFuture { } } +// MARK: whenAll + extension EventLoopFuture { + /// Returns a new `EventLoopFuture` that succeeds only if all of the provided futures succeed. + /// The new `EventLoopFuture` will contain all of the values fulfilled by the futures. + /// + /// The returned `EventLoopFuture` will fail as soon as any of the futures fails. + /// - Parameters: + /// - futures: An array of homogenous `EventLoopFuture`s to wait on for fulfilled values. + /// - on: The `EventLoop` on which the new `EventLoopFuture` callbacks will fire. + /// - Returns: A new `EventLoopFuture` with all of the values fulfilled by the provided futures. + public static func whenAllSucceed(_ futures: [EventLoopFuture], on eventLoop: EventLoop) -> EventLoopFuture<[Value]> { + return .reduce(into: [], futures, eventLoop: eventLoop) { (results, value) in results.append(value) } + } + /// Returns a new `EventLoopFuture` that succeeds when all of the provided `EventLoopFuture`s complete. /// The new `EventLoopFuture` will contain an array of results, maintaining ordering for each of the `EventLoopFuture`s. /// @@ -994,10 +1008,12 @@ extension EventLoopFuture { /// /// If it is desired to flatten them into a single `EventLoopFuture` that fails on the first `EventLoopFuture` failure, /// use one of the `reduce` methods instead. - /// - Parameter futures: An array of homogenous `EventLoopFuture`s to gather results from. + /// - Parameters: + /// - futures: An array of homogenous `EventLoopFuture`s to gather results from. + /// - on: The `EventLoop` on which the new `EventLoopFuture` callbacks will fire. /// - Returns: A new `EventLoopFuture` with all the results of the provided futures. public static func whenAllComplete(_ futures: [EventLoopFuture], - eventLoop: EventLoop) -> EventLoopFuture<[Result]> { + on eventLoop: EventLoop) -> EventLoopFuture<[Result]> { let promise = eventLoop.makePromise(of: [Result].self) if eventLoop.inEventLoop { diff --git a/Tests/NIOTests/EventLoopFutureTest.swift b/Tests/NIOTests/EventLoopFutureTest.swift index 5dfbf6149..edbc7dbe4 100644 --- a/Tests/NIOTests/EventLoopFutureTest.swift +++ b/Tests/NIOTests/EventLoopFutureTest.swift @@ -869,7 +869,7 @@ class EventLoopFutureTest : XCTestCase { let future = EventLoopFuture.whenAllComplete([ group.next().makeFailedFuture(EventLoopFutureTestError.example), group.next().makeSucceededFuture(true) - ], eventLoop: group.next()) + ], on: group.next()) XCTAssertNoThrow(try future.wait()) } @@ -885,7 +885,7 @@ class EventLoopFutureTest : XCTestCase { group.next().makeSucceededFuture(10), group.next().makeFailedFuture(EventLoopFutureTestError.example), group.next().makeSucceededFuture(5) - ], eventLoop: group.next()).wait() + ], on: group.next()).wait() XCTAssertEqual(try results[0].get(), 3) XCTAssertThrowsError(try results[1].get()) @@ -906,7 +906,7 @@ class EventLoopFutureTest : XCTestCase { var succeeded = false var completedPromises = false - let mainFuture = EventLoopFuture.whenAllComplete(futures, eventLoop: group.next()) + let mainFuture = EventLoopFuture.whenAllComplete(futures, on: group.next()) mainFuture.whenSuccess { _ in XCTAssertTrue(completedPromises) XCTAssertFalse(succeeded)