Better align shutdown semantics of testing event loops (#2800)

### Motivation:

The two testing event loops—`EmbeddedEventLoop` and `NIOAsyncTestingEventLoop`—have different semantics for outstanding work during shutdown, which are both different from the production `SelectableEventLoop`, specifically with newly scheduled tasks that result from running existing scheduled work at the time of shutdown.

There are three axes to consider:

1. Scheduled tasks that are at or past their deadline.
2. Scheduled tasks with a deadline in the future.
3. Newly scheduled work that result from either running or cancelling (1) and (2).

| | (1) | (2) | (3) |
|-|-|-|-|
| `SelectableEventLoop` | Run | Cancel | Quiesce over 1000 ticks, repeatedly run resulting (1) and cancel resulting (2) |
| `EmbeddedEventLoop` | Run | Cancel | Cancel resulting (1) and resulting (2) |
| `NIOAsyncTestingEventLoop` | Run | Run | Run resulting (1) and resulting (2) |

Note that `NIOAsyncTestingEventLoop` may never terminate because of this.

### Modifications:

This PR aligns `EmbeddedEventLoop` and `NIOTestingEventLoop` and makes them more similar to `SelectableEventLoop` and less surprising semantics.

### Result:

| | (1) | (2) | (3) |
|-|-|-|-|
| `SelectableEventLoop` | Run | Cancel | Quiesce over 1000 ticks, repeatedly run resulting (1) and cancel resulting (2) |
| `EmbeddedEventLoop` | Run | Cancel | Cancel resulting (1) and resulting (2) |
| `NIOAsyncTestingEventLoop` | Run | Cancel | Cancel resulting (1) and resulting (2) |

### Future work:

Given both the `EmbeddedEventLoop` and `NIOAsyncTestingEventLoop` are used in tests of NIO applications that run with `SelectableEventLoop` in production, it might be worth extending both to also support quiescing to give a more representative shutdown behaviour in tests.
This commit is contained in:
Si Beaumont authored and GitHub committed 2024-07-25 14:38:36 +00:00
1 parent 2ef87d45e6
commit 55d4c49334
16 files changed
+468 -357

No files matched your search

+65 -54
View File
@@ -95,6 +95,9 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable {
/// The queue on which we run all our operations.
private let queue = DispatchQueue(label: "io.swiftnio.AsyncEmbeddedEventLoop")
private enum State: Int, AtomicValue { case open, closing, closed }
private let state = ManagedAtomic(State.open)
// This function must only be called on queue.
private func nextTaskNumber() -> UInt64 {
dispatchPrecondition(condition: .onQueue(self.queue))
@@ -150,6 +153,15 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable {
let promise: EventLoopPromise<T> = self.makePromise()
let taskID = self.scheduledTaskCounter.loadThenWrappingIncrement(ordering: .relaxed)
switch self.state.load(ordering: .acquiring) {
case .open:
break
case .closing, .closed:
// If the event loop is shut down, or shutting down, immediately cancel the task.
promise.fail(EventLoopError.cancelled)
return Scheduled(promise: promise, cancellationTask: {})
}
let scheduled = Scheduled(
promise: promise,
cancellationTask: {
@@ -187,27 +199,7 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable {
} else {
self.queue.async {
self.scheduleTask(deadline: self.now, task)
var tasks = CircularBuffer<EmbeddedScheduledTask>()
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)
}
self._run()
}
}
}
@@ -233,43 +225,52 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable {
///
/// - Note: If `deadline` is before the current time, the current time will not be advanced.
public func advanceTime(to deadline: NIODeadline) async {
await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in
await withCheckedContinuation { continuation in
self.queue.async {
let newTime = max(deadline, self.now)
var tasks = CircularBuffer<EmbeddedScheduledTask>()
while let nextTask = self.scheduledTasks.peek() {
guard nextTask.readyTime <= newTime 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()
}
// Set the time correctly before we call into user code, then
// call in for all tasks.
self._now.store(nextTask.readyTime.uptimeNanoseconds, ordering: .relaxed)
for task in tasks {
task.task()
}
tasks.removeAll(keepingCapacity: true)
}
// Finally ensure we got the time right.
self._now.store(newTime.uptimeNanoseconds, ordering: .relaxed)
self._advanceTime(to: deadline)
continuation.resume()
}
}
}
internal func _advanceTime(to deadline: NIODeadline) {
dispatchPrecondition(condition: .onQueue(self.queue))
let newTime = max(deadline, self.now)
var tasks = CircularBuffer<EmbeddedScheduledTask>()
while let nextTask = self.scheduledTasks.peek() {
guard nextTask.readyTime <= newTime 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()
}
// Set the time correctly before we call into user code, then
// call in for all tasks.
self._now.store(nextTask.readyTime.uptimeNanoseconds, ordering: .relaxed)
for task in tasks {
task.task()
}
tasks.removeAll(keepingCapacity: true)
}
// Finally ensure we got the time right.
self._now.store(newTime.uptimeNanoseconds, ordering: .relaxed)
}
internal func _run() {
dispatchPrecondition(condition: .onQueue(self.queue))
self._advanceTime(to: self.now)
}
/// Executes the given function in the context of this event loop. This is useful when it's necessary to be confident that an operation
/// is "blocking" the event loop. As long as you are executing, nothing else can execute in this loop.
///
@@ -293,6 +294,13 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable {
}
}
internal func _cancelRemainingScheduledTasks() {
dispatchPrecondition(condition: .onQueue(self.queue))
while let task = self.scheduledTasks.pop() {
task.fail(EventLoopError.cancelled)
}
}
internal func drainScheduledTasksByRunningAllCurrentlyScheduledTasks() {
var currentlyScheduledTasks = self.scheduledTasks
while let nextTask = currentlyScheduledTasks.pop() {
@@ -309,7 +317,8 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable {
private func _shutdownGracefully() {
dispatchPrecondition(condition: .onQueue(self.queue))
self.drainScheduledTasksByRunningAllCurrentlyScheduledTasks()
self._run()
self._cancelRemainingScheduledTasks()
}
/// - see: `EventLoop.shutdownGracefully`
@@ -324,9 +333,11 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable {
/// The concurrency-aware equivalent of `shutdownGracefully(queue:_:)`.
public func shutdownGracefully() async {
await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in
await withCheckedContinuation { continuation in
self.state.store(.closing, ordering: .releasing)
self.queue.async {
self._shutdownGracefully()
self.state.store(.closed, ordering: .releasing)
continuation.resume()
}
}