Skip to content

Commit

Permalink
add easier async to future conversion (#2334)
Browse files Browse the repository at this point in the history
  • Loading branch information
weissi authored Dec 19, 2022
1 parent 0760383 commit 86e8b5f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/NIOCore/AsyncAwaitSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,13 @@ struct AsyncSequenceFromIterator<AsyncIterator: AsyncIteratorProtocol>: AsyncSeq
self.iterator
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension EventLoop {
@inlinable
public func makeFutureWithTask<Return>(_ body: @Sendable @escaping () async throws -> Return) -> EventLoopFuture<Return> {
let promise = self.makePromise(of: Return.self)
promise.completeWithTask(body)
return promise.futureResult
}
}
2 changes: 2 additions & 0 deletions Tests/NIOPosixTests/EventLoopTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ extension EventLoopTest {
("testEventLoopGroupsWithoutAnyImplementationAreValid", testEventLoopGroupsWithoutAnyImplementationAreValid),
("testCallingAnyOnAnMTELGThatIsNotSelfDoesNotReturnItself", testCallingAnyOnAnMTELGThatIsNotSelfDoesNotReturnItself),
("testMultiThreadedEventLoopGroupSupportsStickyAnyImplementation", testMultiThreadedEventLoopGroupSupportsStickyAnyImplementation),
("testAsyncToFutureConversionSuccess", testAsyncToFutureConversionSuccess),
("testAsyncToFutureConversionFailure", testAsyncToFutureConversionFailure),
]
}
}
Expand Down
31 changes: 31 additions & 0 deletions Tests/NIOPosixTests/EventLoopTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,7 @@ public final class EventLoopTest : XCTestCase {
}.wait())
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func testMultiThreadedEventLoopGroupSupportsStickyAnyImplementation() {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 3)
defer {
Expand All @@ -1492,6 +1493,36 @@ public final class EventLoopTest : XCTestCase {
XCTAssert(el1 === MultiThreadedEventLoopGroup.currentEventLoop!)
}.wait())
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func testAsyncToFutureConversionSuccess() {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}

XCTAssertEqual("hello from async",
try group.next().makeFutureWithTask {
try await Task.sleep(nanoseconds: 37)
return "hello from async"
}.wait())
}

func testAsyncToFutureConversionFailure() {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}

struct DummyError: Error {}

XCTAssertThrowsError(try group.next().makeFutureWithTask {
try await Task.sleep(nanoseconds: 37)
throw DummyError()
}.wait()) { error in
XCTAssert(error is DummyError)
}
}
}

fileprivate class EventLoopWithPreSucceededFuture: EventLoop {
Expand Down

0 comments on commit 86e8b5f

Please sign in to comment.