Skip to content

Commit

Permalink
Add flatSubmit method to EventLoop (#426) (#1174)
Browse files Browse the repository at this point in the history
Motivation:

Calling EventLoop's submit method with a task that returns an EventLoopFuture results in a double future. flatSubmit provides an alternative which does not return a double future.

Modifications:

Added flatSubmit() to the EventLoop protocol and provided a default implementation that returns the result of calling flatMap { $0 } on the EventLoopFuture returned from task.

Result:

Additive change only. flatSubmit method on EventLoop will be available for use.
  • Loading branch information
2bjake authored and Lukasa committed Oct 22, 2019
1 parent 58001da commit 4761f96
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/NIO/EventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,18 @@ extension EventLoop {
return promise.futureResult
}

/// Submit `task` to be run on this `EventLoop`.
///
/// The returned `EventLoopFuture` will be completed when `task` has finished running. It will be identical to
/// the `EventLoopFuture` returned by `task`.
///
/// - parameters:
/// - task: The synchronous task to run. As everything that runs on the `EventLoop`, it must not block.
/// - returns: An `EventLoopFuture` identical to the `EventLooopFuture` returned from `task`.
public func flatSubmit<T>(_ task: @escaping () -> EventLoopFuture<T>) -> EventLoopFuture<T> {
return submit(task).flatMap { $0 }
}

/// Creates and returns a new `EventLoopPromise` that will be notified using this `EventLoop` as execution `NIOThread`.
public func makePromise<T>(of type: T.Type = T.self, file: StaticString = #file, line: UInt = #line) -> EventLoopPromise<T> {
return EventLoopPromise<T>(eventLoop: self, file: file, line: line)
Expand Down
2 changes: 2 additions & 0 deletions Tests/NIOTests/EventLoopTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ extension EventLoopTest {
("testEdgeCasesNIODeadlineMinusNIODeadline", testEdgeCasesNIODeadlineMinusNIODeadline),
("testEdgeCasesNIODeadlinePlusTimeAmount", testEdgeCasesNIODeadlinePlusTimeAmount),
("testEdgeCasesNIODeadlineMinusTimeAmount", testEdgeCasesNIODeadlineMinusTimeAmount),
("testSuccessfulFlatSubmit", testSuccessfulFlatSubmit),
("testFailingFlatSubmit", testFailingFlatSubmit),
]
}
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/NIOTests/EventLoopTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -928,4 +928,26 @@ public final class EventLoopTest : XCTestCase {
}
}
}

func testSuccessfulFlatSubmit() {
let eventLoop = EmbeddedEventLoop()
let future = eventLoop.flatSubmit {
eventLoop.makeSucceededFuture(1)
}
eventLoop.run()
XCTAssertNoThrow(XCTAssertEqual(1, try future.wait()))
}

func testFailingFlatSubmit() {
enum TestError: Error { case failed }

let eventLoop = EmbeddedEventLoop()
let future = eventLoop.flatSubmit { () -> EventLoopFuture<Int> in
eventLoop.makeFailedFuture(TestError.failed)
}
eventLoop.run()
XCTAssertThrowsError(try future.wait()) { error in
XCTAssertEqual(.failed, error as? TestError)
}
}
}

0 comments on commit 4761f96

Please sign in to comment.