Files
swift-nio/Tests/NIOCoreTests/NIOIsolatedEventLoopTests.swift
George Barnett 6b00972ad9 Add make*Future methods to isolated event loop (#3152)
Motivation:

Creating a completed future of a non-sendable value when on the current
event loop is currently a little tedious. It requires you to create a
promise, succeed it using the isolated view, or fail it using the
non-isolated view. This is more tedious than it needs to be.

Modifications:

- Add `make{Succeeded,Failed,Completed}Future` methods to the isolated
event loop. These don't require the `Success` value to be `Sendable`.

Result:

It's easier to create completed futures of non sendable values from an
isolated event loop.
2025-03-20 16:00:04 +00:00

62 lines
2.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOCore
import NIOEmbedded
import XCTest
final class NIOIsolatedEventLoopTests: XCTestCase {
func withEmbeddedEventLoop(_ body: (EmbeddedEventLoop) throws -> Void) rethrows {
let loop = EmbeddedEventLoop()
defer { try! loop.syncShutdownGracefully() }
try body(loop)
}
func testMakeSucceededFuture() throws {
try self.withEmbeddedEventLoop { loop in
let future = loop.assumeIsolated().makeSucceededFuture(NotSendable())
XCTAssertNoThrow(try future.map { _ in }.wait())
}
}
func testMakeFailedFuture() throws {
try self.withEmbeddedEventLoop { loop in
let future: EventLoopFuture<NotSendable> = loop.assumeIsolated().makeFailedFuture(
ChannelError.alreadyClosed
)
XCTAssertThrowsError(try future.map { _ in }.wait())
}
}
func testMakeCompletedFuture() throws {
try self.withEmbeddedEventLoop { loop in
let result = Result<NotSendable, any Error>.success(NotSendable())
let future: EventLoopFuture<NotSendable> = loop.assumeIsolated().makeCompletedFuture(result)
XCTAssertNoThrow(try future.map { _ in }.wait())
}
}
func testMakeCompletedFutureWithResultOfClosure() throws {
try self.withEmbeddedEventLoop { loop in
let future = loop.assumeIsolated().makeCompletedFuture { NotSendable() }
XCTAssertNoThrow(try future.map { _ in }.wait())
}
}
}
private struct NotSendable {}
@available(*, unavailable)
extension NotSendable: Sendable {}