diff --git a/Sources/NIOEmbedded/AsyncEmbeddedEventLoop.swift b/Sources/NIOEmbedded/AsyncTestingEventLoop.swift similarity index 74% rename from Sources/NIOEmbedded/AsyncEmbeddedEventLoop.swift rename to Sources/NIOEmbedded/AsyncTestingEventLoop.swift index d562a243e..723b1d22c 100644 --- a/Sources/NIOEmbedded/AsyncEmbeddedEventLoop.swift +++ b/Sources/NIOEmbedded/AsyncTestingEventLoop.swift @@ -22,23 +22,23 @@ import NIOConcurrencyHelpers /// An `EventLoop` that is thread safe and whose execution is fully controlled /// by the user. /// -/// Unlike more complex `EventLoop`s, such as `SelectableEventLoop`, the `NIOAsyncEmbeddedEventLoop` +/// Unlike more complex `EventLoop`s, such as `SelectableEventLoop`, the `NIOAsyncTestingEventLoop` /// has no proper eventing mechanism. Instead, reads and writes are fully controlled by the -/// entity that instantiates the `NIOAsyncEmbeddedEventLoop`. This property makes `NIOAsyncEmbeddedEventLoop` +/// entity that instantiates the `NIOAsyncTestingEventLoop`. This property makes `NIOAsyncTestingEventLoop` /// of limited use for many application purposes, but highly valuable for testing and other -/// kinds of mocking. Unlike `EmbeddedEventLoop`, `NIOAsyncEmbeddedEventLoop` is fully thread-safe and +/// kinds of mocking. Unlike `EmbeddedEventLoop`, `NIOAsyncTestingEventLoop` is fully thread-safe and /// safe to use from within a Swift concurrency context. /// -/// Unlike `EmbeddedEventLoop`, `NIOAsyncEmbeddedEventLoop` does require that user tests appropriately +/// Unlike `EmbeddedEventLoop`, `NIOAsyncTestingEventLoop` does require that user tests appropriately /// enforce thread safety. Used carefully it is possible to safely operate the event loop without /// explicit synchronization, but it is recommended to use `executeInContext` in any case where it's /// necessary to ensure that the event loop is not making progress. /// -/// Time is controllable on an `NIOAsyncEmbeddedEventLoop`. It begins at `NIODeadline.uptimeNanoseconds(0)` +/// Time is controllable on an `NIOAsyncTestingEventLoop`. It begins at `NIODeadline.uptimeNanoseconds(0)` /// and may be advanced by a fixed amount by using `advanceTime(by:)`, or advanced to a point in /// time with `advanceTime(to:)`. /// -/// If users wish to perform multiple tasks at once on an `NIOAsyncEmbeddedEventLoop`, it is recommended that they +/// If users wish to perform multiple tasks at once on an `NIOAsyncTestingEventLoop`, it is recommended that they /// use `executeInContext` to perform the operations. For example: /// /// ``` @@ -54,10 +54,9 @@ import NIOConcurrencyHelpers /// There is a tricky requirement around waiting for `EventLoopFuture`s when working with this /// event loop. Simply calling `.wait()` from the test thread will never complete. This is because /// `wait` calls `loop.execute` under the hood, and that callback cannot execute without calling -/// `loop.run()`. As a result, if you need to await an `EventLoopFuture` created on this loop you -/// should use `awaitFuture`. +/// `loop.run()`. @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) -public final class NIOAsyncEmbeddedEventLoop: EventLoop, @unchecked Sendable { +public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { // This type is `@unchecked Sendable` because of the use of `taskNumber`. This // variable is only used from within `queue`, but the compiler cannot see that. @@ -111,7 +110,7 @@ public final class NIOAsyncEmbeddedEventLoop: EventLoop, @unchecked Sendable { return DispatchQueue.getSpecific(key: Self.inQueueKey) == self.thisLoopID } - /// Initialize a new `NIOAsyncEmbeddedEventLoop`. + /// Initialize a new `NIOAsyncTestingEventLoop`. public init() { self.queue.setSpecific(key: Self.inQueueKey, value: self.thisLoopID) } @@ -172,17 +171,43 @@ public final class NIOAsyncEmbeddedEventLoop: EventLoop, @unchecked Sendable { return self.scheduleTask(deadline: self.now + `in`, task) } - /// On an `NIOAsyncEmbeddedEventLoop`, `execute` will simply use `scheduleTask` with a deadline of _now_. This means that - /// `task` will be run the next time you call `AsyncEmbeddedEventLoop.run`. + /// On an `NIOAsyncTestingEventLoop`, `execute` will simply use `scheduleTask` with a deadline of _now_. Unlike with the other operations, this will + /// immediately execute, to eliminate a common class of bugs. public func execute(_ task: @escaping () -> Void) { - self.scheduleTask(deadline: self.now, task) + if self.inEventLoop { + self.scheduleTask(deadline: self.now, task) + } else { + self.queue.async { + self.scheduleTask(deadline: self.now, task) + + var tasks = CircularBuffer() + while let nextTask = self.scheduledTasks.peek() { + guard nextTask.readyTime <= self.now else { + break + } + + // Now we want to grab all tasks that are ready to execute at the same + // time as the first. + while let candidateTask = self.scheduledTasks.peek(), candidateTask.readyTime == nextTask.readyTime { + tasks.append(candidateTask) + self.scheduledTasks.pop() + } + + for task in tasks { + task.task() + } + + tasks.removeAll(keepingCapacity: true) + } + } + } } - /// Run all tasks that have previously been submitted to this `NIOAsyncEmbeddedEventLoop`, either by calling `execute` or + /// Run all tasks that have previously been submitted to this `NIOAsyncTestingEventLoop`, either by calling `execute` or /// events that have been enqueued using `scheduleTask`/`scheduleRepeatedTask`/`scheduleRepeatedAsyncTask` and whose /// deadlines have expired. /// - /// - seealso: `NIOAsyncEmbeddedEventLoop.advanceTime`. + /// - seealso: `NIOAsyncTestingEventLoop.advanceTime`. public func run() async { // Execute all tasks that are currently enqueued to be executed *now*. await self.advanceTime(to: self.now) @@ -194,59 +219,6 @@ public final class NIOAsyncEmbeddedEventLoop: EventLoop, @unchecked Sendable { await self.advanceTime(to: self.now + increment) } - /// Unwrap a future result from this event loop. - /// - /// This replaces `EventLoopFuture.get()` for use with `NIOAsyncEmbeddedEventLoop`. This is necessary because attaching - /// a callback to an `EventLoopFuture` (which is what `EventLoopFuture.get` does) requires scheduling a work item onto - /// the event loop, and running that work item requires spinning the loop. This is a non-trivial dance to get right due - /// to timing issues, so this function provides a helper to ensure that this will work. - public func awaitFuture(_ future: EventLoopFuture, timeout: TimeAmount) async throws -> ResultType { - // We need a task group to wait for the scheduled future result, because the future result callback - // can only complete when we actually run the loop, which we need to do in another Task. - return try await withThrowingTaskGroup(of: ResultType.self, returning: ResultType.self) { group in - // We need an innner promise to allow cancellation of the wait. - let promise = self.makePromise(of: ResultType.self) - future.cascade(to: promise) - - group.addTask { - try await promise.futureResult.get() - } - - group.addTask { - while true { - await self.run() - try Task.checkCancellation() - await Task.yield() - } - } - - group.addTask { - try await Task.sleep(nanoseconds: UInt64(timeout.nanoseconds)) - promise.fail(NIOAsyncEmbeddedEventLoopError.timeoutAwaitingFuture) - - // This self.run() is _very important_. We're about to throw out of this function, which will - // cancel the entire TaskGroup. Depending on how things get scheduled it is possible for that - // cancellation to cancel the runner task above _before_ it spins again, meaning that the - // promise failure never actually happens (as it has to establish event loop context). So - // before we cancel this we make sure that we get a chance to spin the loop. - await self.run() - throw NIOAsyncEmbeddedEventLoopError.timeoutAwaitingFuture - } - - do { - // This force-unwrap is safe: there are only three tasks and only one of them can ever return a value, - // the rest will error. The one that does return a value can never return `nil`. In essence, this is an - // `AsyncSequenceOfOneOrError`. - let result = try await group.next()! - group.cancelAll() - return result - } catch { - group.cancelAll() - throw error - } - } - } - /// Runs the event loop and moves "time" forward to the given point in time, running any scheduled /// tasks that need to be run. /// @@ -296,7 +268,7 @@ public final class NIOAsyncEmbeddedEventLoop: EventLoop, @unchecked Sendable { /// accessed from the event loop thread and want to be 100% sure of the thread-safety of accessing them. /// /// Be careful not to try to spin the event loop again from within this callback, however. As long as this function is on the call - /// stack the `NIOAsyncEmbeddedEventLoop` cannot progress, and so any attempt to progress it will block until this function returns. + /// stack the `NIOAsyncTestingEventLoop` cannot progress, and so any attempt to progress it will block until this function returns. public func executeInContext(_ task: @escaping @Sendable () throws -> ReturnType) async throws -> ReturnType { try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in self.queue.async { @@ -373,13 +345,13 @@ public final class NIOAsyncEmbeddedEventLoop: EventLoop, @unchecked Sendable { } deinit { - precondition(scheduledTasks.isEmpty, "NIOAsyncEmbeddedEventLoop freed with unexecuted scheduled tasks!") + precondition(scheduledTasks.isEmpty, "NIOAsyncTestingEventLoop freed with unexecuted scheduled tasks!") } } /// This is a thread-safe promise creation store. /// -/// We use this to keep track of where promises come from in the `NIOAsyncEmbeddedEventLoop`. +/// We use this to keep track of where promises come from in the `NIOAsyncTestingEventLoop`. private class PromiseCreationStore { private let lock = Lock() private var promiseCreationStore: [_NIOEventLoopFutureIdentifier: (file: StaticString, line: UInt)] = [:] @@ -400,24 +372,8 @@ private class PromiseCreationStore { deinit { // We no longer need the lock here. - precondition(self.promiseCreationStore.isEmpty, "NIOAsyncEmbeddedEventLoop freed with uncompleted promises!") + precondition(self.promiseCreationStore.isEmpty, "NIOAsyncTestingEventLoop freed with uncompleted promises!") } } -/// Errors that can happen on a `NIOAsyncEmbeddedEventLoop`. -public struct NIOAsyncEmbeddedEventLoopError: Error, Hashable { - private enum Backing { - case timeoutAwaitingFuture - } - - private var backing: Backing - - private init(_ backing: Backing) { - self.backing = backing - } - - /// A timeout occurred while waiting for a future to complete. - public static let timeoutAwaitingFuture = Self(.timeoutAwaitingFuture) -} - #endif diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index e5bb617fe..c0688d47a 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -105,7 +105,7 @@ class LinuxMainRunnerImpl: LinuxMainRunner { testCase(MessageToByteHandlerTest.allTests), testCase(MulticastTest.allTests), testCase(NIOAnyDebugTest.allTests), - testCase(NIOAsyncEmbeddedEventLoopTests.allTests), + testCase(NIOAsyncTestingEventLoopTests.allTests), testCase(NIOCloseOnErrorHandlerTest.allTests), testCase(NIOConcurrencyHelpersTests.allTests), testCase(NIOHTTP1TestServerTest.allTests), diff --git a/Tests/NIOEmbeddedTests/AsyncEmbeddedEventLoopTests+XCTest.swift b/Tests/NIOEmbeddedTests/AsyncTestingEventLoopTests+XCTest.swift similarity index 86% rename from Tests/NIOEmbeddedTests/AsyncEmbeddedEventLoopTests+XCTest.swift rename to Tests/NIOEmbeddedTests/AsyncTestingEventLoopTests+XCTest.swift index 2fc783afd..2d61c790c 100644 --- a/Tests/NIOEmbeddedTests/AsyncEmbeddedEventLoopTests+XCTest.swift +++ b/Tests/NIOEmbeddedTests/AsyncTestingEventLoopTests+XCTest.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// // -// AsyncEmbeddedEventLoopTests+XCTest.swift +// AsyncTestingEventLoopTests+XCTest.swift // import XCTest @@ -22,15 +22,17 @@ import XCTest /// Do NOT edit this file directly as it will be regenerated automatically when needed. /// -extension NIOAsyncEmbeddedEventLoopTests { +extension NIOAsyncTestingEventLoopTests { @available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings") - static var allTests : [(String, (NIOAsyncEmbeddedEventLoopTests) -> () throws -> Void)] { + static var allTests : [(String, (NIOAsyncTestingEventLoopTests) -> () throws -> Void)] { return [ ("testExecuteDoesNotImmediatelyRunTasks", testExecuteDoesNotImmediatelyRunTasks), ("testExecuteWillRunAllTasks", testExecuteWillRunAllTasks), ("testExecuteWillRunTasksAddedRecursively", testExecuteWillRunTasksAddedRecursively), - ("testTasksSubmittedAfterRunDontRun", testTasksSubmittedAfterRunDontRun), + ("testExecuteRunsImmediately", testExecuteRunsImmediately), + ("testTasksScheduledAfterRunDontRun", testTasksScheduledAfterRunDontRun), + ("testSubmitRunsImmediately", testSubmitRunsImmediately), ("testSyncShutdownGracefullyRunsTasks", testSyncShutdownGracefullyRunsTasks), ("testShutdownGracefullyRunsTasks", testShutdownGracefullyRunsTasks), ("testCanControlTime", testCanControlTime), @@ -43,7 +45,6 @@ extension NIOAsyncEmbeddedEventLoopTests { ("testScheduledTasksFuturesError", testScheduledTasksFuturesError), ("testTaskOrdering", testTaskOrdering), ("testCancelledScheduledTasksDoNotHoldOnToRunClosure", testCancelledScheduledTasksDoNotHoldOnToRunClosure), - ("testWaitingForFutureCanTimeOut", testWaitingForFutureCanTimeOut), ("testDrainScheduledTasks", testDrainScheduledTasks), ("testDrainScheduledTasksDoesNotRunNewlyScheduledTasks", testDrainScheduledTasksDoesNotRunNewlyScheduledTasks), ("testAdvanceTimeToDeadline", testAdvanceTimeToDeadline), diff --git a/Tests/NIOEmbeddedTests/AsyncEmbeddedEventLoopTests.swift b/Tests/NIOEmbeddedTests/AsyncTestingEventLoopTests.swift similarity index 87% rename from Tests/NIOEmbeddedTests/AsyncEmbeddedEventLoopTests.swift rename to Tests/NIOEmbeddedTests/AsyncTestingEventLoopTests.swift index d74371732..a65b506b3 100644 --- a/Tests/NIOEmbeddedTests/AsyncEmbeddedEventLoopTests.swift +++ b/Tests/NIOEmbeddedTests/AsyncTestingEventLoopTests.swift @@ -23,13 +23,13 @@ import Atomics private class EmbeddedTestError: Error { } -final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { +final class NIOAsyncTestingEventLoopTests: XCTestCase { func testExecuteDoesNotImmediatelyRunTasks() throws { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let callbackRan = ManagedAtomic(false) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() try await loop.executeInContext { loop.execute { callbackRan.store(true, ordering: .relaxed) } XCTAssertFalse(callbackRan.load(ordering: .relaxed)) @@ -47,13 +47,13 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let runCount = ManagedAtomic(0) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() loop.execute { runCount.wrappingIncrement(ordering: .relaxed) } loop.execute { runCount.wrappingIncrement(ordering: .relaxed) } loop.execute { runCount.wrappingIncrement(ordering: .relaxed) } try await loop.executeInContext { - XCTAssertEqual(runCount.load(ordering: .relaxed), 0) + XCTAssertEqual(runCount.load(ordering: .relaxed), 3) } await loop.run() try await loop.executeInContext { @@ -70,26 +70,26 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let sentinel = ManagedAtomic(0) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() loop.execute { // This should execute first. XCTAssertEqual(sentinel.load(ordering: .relaxed), 0) sentinel.store(1, ordering: .relaxed) loop.execute { - // This should execute third. - XCTAssertEqual(sentinel.load(ordering: .relaxed), 2) - sentinel.store(3, ordering: .relaxed) + // This should execute second + let loaded = sentinel.loadThenWrappingIncrement(ordering: .relaxed) + XCTAssertEqual(loaded, 1) } } loop.execute { - // This should execute second. - XCTAssertEqual(sentinel.load(ordering: .relaxed), 1) - sentinel.store(2, ordering: .relaxed) + // This should execute third + let loaded = sentinel.loadThenWrappingIncrement(ordering: .relaxed) + XCTAssertEqual(loaded, 2) } try await loop.executeInContext { - XCTAssertEqual(sentinel.load(ordering: .relaxed), 0) + XCTAssertEqual(sentinel.load(ordering: .relaxed), 3) } await loop.run() try await loop.executeInContext { @@ -101,19 +101,44 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #endif } - func testTasksSubmittedAfterRunDontRun() throws { + func testExecuteRunsImmediately() throws { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let callbackRan = ManagedAtomic(false) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() loop.execute { callbackRan.store(true, ordering: .relaxed) } + try await loop.executeInContext { + XCTAssertTrue(callbackRan.load(ordering: .relaxed)) + } + loop.execute { callbackRan.store(false, ordering: .relaxed) } + + try await loop.executeInContext { + XCTAssertFalse(callbackRan.load(ordering: .relaxed)) + } + try await loop.executeInContext { + XCTAssertFalse(callbackRan.load(ordering: .relaxed)) + } + } + #else + throw XCTSkip() + #endif + } + + func testTasksScheduledAfterRunDontRun() throws { + #if compiler(>=5.5.2) && canImport(_Concurrency) + guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } + XCTAsyncTest { + let callbackRan = ManagedAtomic(false) + let loop = NIOAsyncTestingEventLoop() + loop.scheduleTask(deadline: loop.now) { callbackRan.store(true, ordering: .relaxed) } + try await loop.executeInContext { XCTAssertFalse(callbackRan.load(ordering: .relaxed)) } await loop.run() - loop.execute { callbackRan.store(false, ordering: .relaxed) } + loop.scheduleTask(deadline: loop.now) { callbackRan.store(false, ordering: .relaxed) } try await loop.executeInContext { XCTAssertTrue(callbackRan.load(ordering: .relaxed)) @@ -128,13 +153,38 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #endif } + func testSubmitRunsImmediately() throws { + #if compiler(>=5.5.2) && canImport(_Concurrency) + guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } + XCTAsyncTest { + let callbackRan = ManagedAtomic(false) + let loop = NIOAsyncTestingEventLoop() + _ = loop.submit { callbackRan.store(true, ordering: .relaxed) } + + try await loop.executeInContext { + XCTAssertTrue(callbackRan.load(ordering: .relaxed)) + } + _ = loop.submit { callbackRan.store(false, ordering: .relaxed) } + + try await loop.executeInContext { + XCTAssertFalse(callbackRan.load(ordering: .relaxed)) + } + try await loop.executeInContext { + XCTAssertFalse(callbackRan.load(ordering: .relaxed)) + } + } + #else + throw XCTSkip() + #endif + } + func testSyncShutdownGracefullyRunsTasks() throws { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let callbackRan = ManagedAtomic(false) - let loop = NIOAsyncEmbeddedEventLoop() - loop.execute { callbackRan.store(true, ordering: .relaxed) } + let loop = NIOAsyncTestingEventLoop() + loop.scheduleTask(deadline: loop.now) { callbackRan.store(true, ordering: .relaxed) } try await loop.executeInContext { XCTAssertFalse(callbackRan.load(ordering: .relaxed)) @@ -154,8 +204,8 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let callbackRan = ManagedAtomic(false) - let loop = NIOAsyncEmbeddedEventLoop() - loop.execute { callbackRan.store(true, ordering: .relaxed) } + let loop = NIOAsyncTestingEventLoop() + loop.scheduleTask(deadline: loop.now) { callbackRan.store(true, ordering: .relaxed) } try await loop.executeInContext { XCTAssertFalse(callbackRan.load(ordering: .relaxed)) @@ -175,7 +225,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let callbackCount = ManagedAtomic(0) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() _ = loop.scheduleTask(in: .nanoseconds(5)) { callbackCount.loadThenWrappingIncrement(ordering: .relaxed) } @@ -210,7 +260,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let sentinel = ManagedAtomic(0) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() for index in 1...10 { _ = loop.scheduleTask(in: .nanoseconds(Int64(index))) { sentinel.store(index, ordering: .relaxed) @@ -237,7 +287,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let sentinel = ManagedAtomic(0) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() _ = loop.scheduleTask(in: .nanoseconds(5)) { sentinel.store(1, ordering: .relaxed) loop.execute { @@ -264,7 +314,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let sentinel = ManagedAtomic(0) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() _ = loop.scheduleTask(in: .nanoseconds(5)) { sentinel.store(1, ordering: .relaxed) _ = loop.scheduleTask(in: .nanoseconds(3)) { @@ -310,7 +360,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let sentinel = ManagedAtomic(0) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() loop.execute { XCTAssertEqual(sentinel.load(ordering: .relaxed), 0) _ = loop.scheduleTask(in: .nanoseconds(5)) { @@ -334,7 +384,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() let task = loop.scheduleTask(in: .nanoseconds(10), { XCTFail("Cancelled task ran") }) _ = loop.scheduleTask(in: .nanoseconds(5)) { task.cancel() @@ -352,7 +402,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { let fired = ManagedAtomic(false) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() let task = loop.scheduleTask(in: .nanoseconds(5)) { true } task.futureResult.whenSuccess { fired.store($0, ordering: .relaxed) } @@ -372,7 +422,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { XCTAsyncTest { let err = EmbeddedTestError() let fired = ManagedAtomic(false) - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() let task = loop.scheduleTask(in: .nanoseconds(5)) { throw err } @@ -398,11 +448,11 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { - // This test validates that the ordering of task firing on NIOAsyncEmbeddedEventLoop via + // This test validates that the ordering of task firing on NIOAsyncTestingEventLoop via // advanceTime(by:) is the same as on MultiThreadedEventLoopGroup: specifically, that tasks run via // schedule that expire "now" all run at the same time, and that any work they schedule is run // after all such tasks expire. - let loop = NIOAsyncEmbeddedEventLoop() + let loop = NIOAsyncTestingEventLoop() let lock = Lock() var firstScheduled: Scheduled? = nil var secondScheduled: Scheduled? = nil @@ -485,7 +535,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { - let eventLoop = NIOAsyncEmbeddedEventLoop() + let eventLoop = NIOAsyncTestingEventLoop() defer { XCTAssertNoThrow(try eventLoop.syncShutdownGracefully()) } @@ -505,7 +555,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { let scheduled = make() scheduled.cancel() assert(weakThing == nil, within: .seconds(1)) - await XCTAssertThrowsError(try await eventLoop.awaitFuture(scheduled.futureResult, timeout: .seconds(1))) { error in + await XCTAssertThrowsError(try await scheduled.futureResult.get()) { error in XCTAssertEqual(EventLoopError.cancelled, error as? EventLoopError) } } @@ -514,30 +564,11 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #endif } - func testWaitingForFutureCanTimeOut() throws { - #if compiler(>=5.5.2) && canImport(_Concurrency) - guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } - XCTAsyncTest { - let eventLoop = NIOAsyncEmbeddedEventLoop() - defer { - XCTAssertNoThrow(try eventLoop.syncShutdownGracefully()) - } - let promise = eventLoop.makePromise(of: Void.self) - - await XCTAssertThrowsError(try await eventLoop.awaitFuture(promise.futureResult, timeout: .milliseconds(1))) { error in - XCTAssertEqual(NIOAsyncEmbeddedEventLoopError.timeoutAwaitingFuture, error as? NIOAsyncEmbeddedEventLoopError) - } - } - #else - throw XCTSkip() - #endif - } - func testDrainScheduledTasks() throws { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { - let eventLoop = NIOAsyncEmbeddedEventLoop() + let eventLoop = NIOAsyncTestingEventLoop() let tasksRun = ManagedAtomic(0) let startTime = eventLoop.now @@ -563,7 +594,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { - let eventLoop = NIOAsyncEmbeddedEventLoop() + let eventLoop = NIOAsyncTestingEventLoop() let tasksRun = ManagedAtomic(0) func scheduleNowAndIncrement() { @@ -586,7 +617,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { - let eventLoop = NIOAsyncEmbeddedEventLoop() + let eventLoop = NIOAsyncTestingEventLoop() let deadline = NIODeadline.uptimeNanoseconds(0) + .seconds(42) let tasksRun = ManagedAtomic(0) @@ -606,7 +637,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { - let eventLoop = NIOAsyncEmbeddedEventLoop() + let eventLoop = NIOAsyncTestingEventLoop() let tasksRun = ManagedAtomic(0) eventLoop.scheduleTask(deadline: .uptimeNanoseconds(0) + .seconds(42)) { @@ -634,7 +665,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { - let eventLoop = NIOAsyncEmbeddedEventLoop() + let eventLoop = NIOAsyncTestingEventLoop() let counter = ManagedAtomic(0) eventLoop.execute { @@ -664,7 +695,7 @@ final class NIOAsyncEmbeddedEventLoopTests: XCTestCase { #if compiler(>=5.5.2) && canImport(_Concurrency) guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() } XCTAsyncTest { - let eventLoop = NIOAsyncEmbeddedEventLoop() + let eventLoop = NIOAsyncTestingEventLoop() let counter = ManagedAtomic(0) eventLoop.scheduleTask(in: .seconds(1)) {