arty 0.6.0

Thread-aware async runtime that keeps each task on one worker thread.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Asynchronous worker execution loop.

use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::ControlFlow;
use std::time::{Duration, Instant};

use arty_executor::{CycleOutcome, Executor, TaskSet};
use performables::arc::Arc;
use performables::sync::channel;
use thread_aware::Thread;
use tick::SimpleClock;
use tick::runtime::{ClockDriver, InactiveClock};

use crate::io::{Io, RuntimeDriverHost};
use crate::runtime::config::MonitorOptions;
use crate::runtime::dispatch::DispatcherClient;
use crate::runtime::shutdown::{Shutdown, ShutdownState};
use crate::runtime::worker::asynchronous::{Command, LocalWorker, Signal};
#[cfg(test)]
use crate::runtime::worker::blocking;
use crate::task::Context;

const COMMANDS_PER_CYCLE: usize = 256;

fn suspend_wait(deadline: Instant, now: Instant) -> Duration {
    deadline.saturating_duration_since(now)
}

fn can_suspend(cycle_outcome: &CycleOutcome, batch_exhausted: bool) -> bool {
    matches!(cycle_outcome, CycleOutcome::Suspend) && !batch_exhausted
}

/// The async worker has exclusive use of one runtime thread. Multiple worker
/// threads can share a processor when the exact worker count exceeds available
/// processor capacity. The worker executes async tasks that are woken by local
/// or remote futures, incoming commands, and timers.
///
/// This type contains the internal worker state. Runtime components send work
/// and signals to it through channels.
///
/// # Ownership
///
/// This type is exclusively owned by the thread entry point.
///
/// # Lifecycle
///
/// The worker has these lifecycle stages:
///
/// 1. Startup: Bootstrap creates the thread and worker from the
///    [`RuntimeBuilder`](crate::runtime::RuntimeBuilder) configuration. Bootstrap
///    and the worker coordinate initialization until the worker is ready.
/// 2. Running: The worker processes tasks, advances timers, and waits for
///    commands.
/// 3. Shutting down: The worker rejects queued and new submissions, stops
///    blocking work, cancels async tasks, and runs final timer callbacks.
/// 4. Dead: The worker completed shutdown and its thread terminated.
///
/// A running worker stops after
/// [`RuntimeControl::request_stop`](crate::runtime::RuntimeControl::request_stop),
/// `Runtime::stop`, or `Runtime` destruction.
///
/// # Thread safety
///
/// This type is single-threaded. It only exists on its designated worker thread.
#[derive(Debug)]
pub(crate) struct Worker {
    // Only top-level worker logic uses this field.
    // Shutdown drops the receiver. Later submissions then fail and discard their tasks.
    command_rx: Option<channel::Receiver<Command>>,

    // Dropped when shutdown starts so no more tasks can be enqueued and worker-local
    // services can release resources needed by shutdown.
    context: Option<Context>,

    shutdown: Shutdown,

    // Becomes None during shutdown when the executor is dropped to cancel and release its tasks.
    executor: Option<Executor>,

    // Connected to the `executor`. Submissions after shutdown resolve to an executor-shutdown
    // JoinError rather than registering a task.
    tasks: TaskSet,

    // This drives moves the timers registered with the clock forward.
    clock_driver: ClockDriver,

    // Cycle-local timestamps and wait budgets use precise system time.
    cycle_clock: SimpleClock,
    signal: Arc<Signal>,
    monitor_options: MonitorOptions,
    _single_threaded: PhantomData<*const ()>,
}

impl Worker {
    #[expect(
        clippy::too_many_arguments,
        reason = "worker construction wires its independently owned runtime subsystems"
    )]
    pub(crate) fn new(
        command_rx: channel::Receiver<Command>,
        dispatcher: DispatcherClient,
        thread: Thread,
        io: Io,
        clock: InactiveClock,
        signal: Arc<Signal>,
        monitor_options: MonitorOptions,
        panic_policy: crate::runtime::PanicPolicy,
    ) -> Self {
        let worker_waker = Signal::waker(&signal);
        let shutdown = dispatcher.shutdown().clone();
        let (clock, clock_driver) = clock.activate(worker_waker.clone());
        let cycle_clock = SimpleClock::new_system();

        let executor = Executor::builder()
            .owner_waker(worker_waker)
            .thread(thread.clone())
            .panic_policy(panic_policy.into_executor_policy())
            .build();
        let tasks = executor.tasks();
        let executor_shutdown = executor.shutdown();

        let context = Context::new(dispatcher, clock, io, thread);
        LocalWorker::register_scheduler(context.clone(), tasks.clone(), executor_shutdown);

        Self {
            command_rx: Some(command_rx),
            context: Some(context),
            shutdown,
            executor: Some(executor),
            tasks,
            clock_driver,
            cycle_clock,
            signal,
            monitor_options,
            _single_threaded: PhantomData,
        }
    }

    /// Worker thread entry point, invoked by bootstrap after startup coordination completes.
    /// The configuration-facing `RuntimeBuilder` delegates construction to bootstrap.
    /// After this method returns, the thread will end.
    pub(crate) fn run(&mut self) {
        // Keep the worker-owned policy available at the boundary where monitoring
        // will be attached in a later stage.
        let _monitor_policy = self.monitor_options.policy();
        self.execute_phase();
    }

    #[cfg_attr(test, mutants::skip)] // Critical for code execution to occur in async contexts.
    fn execute_phase(&mut self) {
        while matches!(self.execute_step(), ControlFlow::Continue(())) {}
    }

    fn execute_step(&mut self) -> ControlFlow<()> {
        let action = self.prepare_cycle();
        let max_wait = match action {
            CycleAction::Shutdown => return self.finish_shutdown(),
            CycleAction::WaitForIo(deadline) => self.io_wait(deadline),
            CycleAction::Poll | CycleAction::WaitForSignal(_) => Duration::ZERO,
        };

        if RuntimeDriverHost::execute_cycle_current(max_wait).is_err() {
            return self.finish_shutdown();
        }

        if let CycleAction::WaitForSignal(deadline) = action {
            match deadline {
                Some(deadline) => self.signal.wait(self.wait_until(deadline)),
                None => self.signal.wait_for_notification(),
            }
        }

        ControlFlow::Continue(())
    }

    fn prepare_cycle(&mut self) -> CycleAction {
        let batch_exhausted = self.process_commands();

        if matches!(self.shutdown.state(), ShutdownState::Started) {
            return CycleAction::Shutdown;
        }

        let executor = self
            .executor
            .as_mut()
            .expect("executor removal publishes shutdown, which is handled above");

        let cycle_outcome = executor.execute_cycle();

        if let CycleOutcome::Shutdown(cause) = cycle_outcome {
            self.shutdown.begin(cause);
            return CycleAction::Shutdown;
        }

        let cycle_finished = self.cycle_clock.instant();
        let next_timer = self.clock_driver.advance_timers(cycle_finished).ok().flatten();
        if matches!(self.shutdown.state(), ShutdownState::Started) {
            return CycleAction::Shutdown;
        }

        if !can_suspend(&cycle_outcome, batch_exhausted) {
            CycleAction::Poll
        } else if RuntimeDriverHost::has_primary_current() {
            CycleAction::WaitForIo(next_timer)
        } else {
            CycleAction::WaitForSignal(next_timer)
        }
    }

    fn io_wait(&self, deadline: Option<Instant>) -> Duration {
        if self.signal.take_notification() {
            return Duration::ZERO;
        }

        deadline.map_or(Duration::MAX, |deadline| self.wait_until(deadline))
    }

    fn wait_until(&self, deadline: Instant) -> Duration {
        suspend_wait(deadline, self.cycle_clock.instant())
    }

    fn finish_shutdown(&mut self) -> ControlFlow<()> {
        self.begin_shutdown();
        // Final task cancellation can register a timer after the cycle sample.
        let now = self.cycle_clock.instant();
        _ = self.clock_driver.advance_timers(now);
        ControlFlow::Break(())
    }

    #[cfg_attr(test, mutants::skip)] // Mutation testing requires a fine level of control over what is in the channel, which is too bothersome just for mutation testing.
    fn process_commands(&mut self) -> bool {
        let Some(context) = self.context.as_ref() else {
            debug_assert!(self.command_rx.is_none());

            // We are shutting down and have dropped worker-local services.
            // The command channel is also closed, so no more commands can be received.
            return false;
        };

        let command_rx = self.command_rx.as_ref().expect("context and command channel are dropped together");

        for _ in 0..COMMANDS_PER_CYCLE {
            if matches!(self.shutdown.state(), ShutdownState::Started) {
                self.begin_shutdown();
                return false;
            }
            match command_rx.try_recv() {
                Ok(command) => match command {
                    Command::EnqueueTask { future_factory } => {
                        let _registered = future_factory.into_inner().register(context.clone(), &self.tasks);
                    }
                    Command::AccessIo { access } => {
                        access.into_inner()(context);
                    }
                    Command::Shutdown => {
                        self.begin_shutdown();

                        // Shutdown closes the command channel. No more commands can arrive.
                        return false;
                    }
                },
                Err(error) => {
                    // Every initialized worker retains its own dispatcher until shutdown.
                    assert!(error.is_empty(), "async worker command channel disconnected without shutdown");
                    return false;
                }
            }
        }
        // An exhausted batch can leave commands queued. Read the queue again before parking.
        true
    }

    #[cfg_attr(test, mutants::skip)] // If mutated, shutdown process will never finish - will hang.
    fn begin_shutdown(&mut self) {
        let Some(executor) = self.executor.take() else {
            return;
        };

        let cause = self
            .context
            .as_ref()
            .expect("a running worker retains its context until shutdown")
            .dispatcher()
            .stop(self.shutdown.cause().unwrap_or_default());

        LocalWorker::clear_scheduler();
        // Drop worker-local services first. Their futures and waiters can hold
        // resources that block executor shutdown.
        self.context = None;

        // The command channel is now closed. No more commands can arrive.
        self.command_rx = None;

        // Safe executor shutdown synchronously cancels and releases all remaining task storage.
        executor.shutdown().trigger(cause);
        drop(executor);
        let driver_host = RuntimeDriverHost::uninstall();
        drop(driver_host);
    }
}

impl Drop for Worker {
    #[cfg_attr(test, mutants::skip)] // Worker-owned services must retire before the thread can finish shutdown.
    fn drop(&mut self) {
        if self.executor.is_some() {
            self.begin_shutdown();
        }
    }
}

#[derive(Clone, Copy, Debug)]
enum CycleAction {
    Poll,
    WaitForIo(Option<Instant>),
    WaitForSignal(Option<Instant>),
    Shutdown,
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))] // Test scaffolding is not runtime behavior.
mod tests {
    use std::panic::{AssertUnwindSafe, catch_unwind};
    #[cfg(not(miri))]
    use std::pin::Pin;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
    use std::sync::mpsc;
    #[cfg(not(miri))]
    use std::task::Wake;
    use std::task::Waker;
    use std::thread;

    use arty_executor::{ShutdownCause, TaskCompletion, TaskOptions, TaskPanicPayload, discard_task_panic_payload};
    use arty_io_core::{Cycle, Driver, DriverError, DriverOptions, IoContext};
    #[cfg(not(miri))]
    use events_once::BoxedSender;
    use events_once::{Event, IntoValueError};
    use nonempty::NonEmpty;
    use testing_aids::{TEST_TIMEOUT, async_test};
    use thread_aware::{ThreadAware, ThreadBuilder};
    use tick::ClockControl;

    use super::*;
    use crate::runtime::config::MonitorPolicy;
    use crate::runtime::dispatch::{DispatcherCore, WorkerEndpoint};
    use crate::runtime::worker::asynchronous::{QueuedFactory, QueuedIoAccess};
    use crate::task::preparation::RemoteFutureFactory;

    fn test_blocking_worker() -> (Arc<blocking::Worker>, Shutdown) {
        let shutdown = Shutdown::default();
        (
            blocking::Worker::new(blocking::Pool::new(None), shutdown.clone(), blocking::PanicPolicy::Ignore),
            shutdown,
        )
    }

    trait TestTaskSetExt {
        fn add_test<F, R>(&self, future: F)
        where
            F: IntoFuture<Output = R> + 'static,
            R: 'static;
    }

    impl TestTaskSetExt for TaskSet {
        fn add_test<F, R>(&self, future: F)
        where
            F: IntoFuture<Output = R> + 'static,
            R: 'static,
        {
            self.add(future, TaskOptions::default(), DiscardCompletion);
        }
    }

    #[derive(Debug)]
    struct DiscardCompletion;

    impl<R: 'static> TaskCompletion<R> for DiscardCompletion {
        fn completed(self, result: R) {
            if let Err(panic) = catch_unwind(AssertUnwindSafe(|| drop(result))) {
                discard_task_panic_payload(panic);
            }
        }

        fn panicked(self, panic: TaskPanicPayload, _cause: Option<ShutdownCause>) {
            discard_task_panic_payload(panic);
        }

        fn cancelled(self, _cause: ShutdownCause) {}
    }

    fn enqueue(factory: RemoteFutureFactory) -> Command {
        Command::EnqueueTask {
            future_factory: QueuedFactory::new(factory),
        }
    }

    #[derive(Clone)]
    struct WaitRecordingContext(Arc<std::sync::Mutex<Vec<Duration>>>);

    impl ThreadAware for WaitRecordingContext {
        fn relocate(&mut self, _source: Option<&Thread>, _destination: &Thread) {}
    }

    impl IoContext for WaitRecordingContext {
        type ContextOptions = Arc<std::sync::Mutex<Vec<Duration>>>;
        type Driver = WaitRecordingDriver;

        fn context(options: Self::ContextOptions) -> Self {
            Self(options)
        }

        fn driver(&mut self, _options: DriverOptions) -> Result<Self::Driver, DriverError> {
            Ok(WaitRecordingDriver(Arc::clone(&self.0)))
        }
    }

    struct WaitRecordingDriver(Arc<std::sync::Mutex<Vec<Duration>>>);

    impl Driver for WaitRecordingDriver {
        fn waker(&self) -> Waker {
            Waker::noop().clone()
        }

        fn execute_cycle(&mut self, cycle: &mut Cycle) -> Result<(), DriverError> {
            self.0.lock().unwrap().push(cycle.max_wait());
            Ok(())
        }

        fn shutdown(self) -> Result<(), DriverError> {
            Ok(())
        }
    }

    fn test_worker(
        command_tx: channel::Sender<Command>,
        command_rx: channel::Receiver<Command>,
        blocking: (Arc<blocking::Worker>, Shutdown),
        clock: InactiveClock,
        signal: Arc<Signal>,
    ) -> Worker {
        test_worker_with_monitor_options(command_tx, command_rx, blocking, clock, signal, MonitorOptions::default())
    }

    fn test_worker_with_monitor_options(
        command_tx: channel::Sender<Command>,
        command_rx: channel::Receiver<Command>,
        blocking: (Arc<blocking::Worker>, Shutdown),
        clock: InactiveClock,
        signal: Arc<Signal>,
        monitor_options: MonitorOptions,
    ) -> Worker {
        let (blocking_worker, shutdown) = blocking;
        let thread = ThreadBuilder::default().build(thread::current().id());
        let endpoint = WorkerEndpoint {
            command_tx,
            waker: Signal::waker(&signal),
            thread: thread.clone(),
            blocking_worker: Arc::clone(&blocking_worker),
        };
        let dispatcher = DispatcherClient::new(DispatcherCore::new(NonEmpty::new(endpoint), shutdown));
        let io_runtime = arty_io_host::IoRuntime::new(observed::Sink::noop());
        let io = crate::io::Io::new(io_runtime.clone(), dispatcher.clone(), thread.clone());
        if !LocalWorker::is_current() {
            LocalWorker::attach();
        }
        let driver_waker = crate::io::RuntimeDriverHost::install(&io_runtime, dispatcher.clone(), &thread, Signal::waker(&signal));
        signal.set_driver_waker(driver_waker);

        Worker::new(
            command_rx,
            dispatcher,
            thread,
            io,
            clock,
            signal,
            monitor_options,
            crate::runtime::PanicPolicy::default(),
        )
    }

    fn replenishing_command(sender: channel::Sender<Command>, processed: Arc<AtomicUsize>) -> Command {
        enqueue(RemoteFutureFactory::new(move |_, _| {
            processed.fetch_add(1, Ordering::Relaxed);
            sender.send(replenishing_command(sender.clone(), Arc::clone(&processed))).unwrap();
        }))
    }

    #[cfg(not(miri))]
    struct GeneratedWorkerScenario {
        command_tx: channel::Sender<Command>,
        ready_task: Option<BoxedSender<()>>,
        control: ClockControl,
        task_done: Arc<AtomicBool>,
        timer_done: Arc<AtomicBool>,
        factory_invocations: Arc<AtomicUsize>,
        forbidden_factory_invocations: Arc<AtomicUsize>,
        signal: Arc<Signal>,
        worker: Worker,
        timer_ready: bool,
        timer_advanced_by_step: bool,
        queued_shutdown: bool,
        queued_factories: usize,
        sent_factories: usize,
        progress_failure: Option<&'static str>,
    }

    #[cfg(not(miri))]
    impl GeneratedWorkerScenario {
        const MAX_OPERATIONS: usize = 12;
        const MAX_CLEANUP_STEPS: usize = Self::MAX_OPERATIONS + 8;

        fn new() -> Self {
            let (command_tx, command_rx) = channel::unbounded();
            let (ready_task, task_ready) = Event::boxed();
            let control = ClockControl::new();
            let task_done = Arc::new(AtomicBool::new(false));
            let timer_done = Arc::new(AtomicBool::new(false));
            let factory_invocations = Arc::new(AtomicUsize::new(0));
            let forbidden_factory_invocations = Arc::new(AtomicUsize::new(0));
            let signal = Arc::new(Signal::default());
            let task_observer = Arc::clone(&task_done);
            let timer_observer = Arc::clone(&timer_done);

            let worker = test_worker(
                command_tx.clone(),
                command_rx,
                test_blocking_worker(),
                control.clone().into(),
                Arc::clone(&signal),
            );
            worker.tasks.add_test(async move {
                task_ready.await.unwrap();
                task_observer.store(true, Ordering::Relaxed);
            });
            let clock = worker.context.as_ref().unwrap().clock().clone();
            worker.tasks.add_test(async move {
                clock.delay(Duration::from_secs(1)).await;
                timer_observer.store(true, Ordering::Relaxed);
            });
            let mut scenario = Self {
                command_tx,
                ready_task: Some(ready_task),
                control,
                task_done,
                timer_done,
                factory_invocations,
                forbidden_factory_invocations,
                signal,
                worker,
                timer_ready: false,
                timer_advanced_by_step: false,
                queued_shutdown: false,
                queued_factories: 0,
                sent_factories: 0,
                progress_failure: None,
            };
            scenario.setup_step("worker stopped while polling controlled work");
            scenario
        }

        fn setup_step(&mut self, failure: &'static str) {
            Signal::waker(&self.signal).wake_by_ref();
            if self.worker.execute_step().is_break() {
                self.progress_failure = Some(failure);
            }
        }

        fn apply(&mut self, operation: u8) {
            match operation % 5 {
                0 => self.queue_factory_batch(operation),
                1 => {
                    if let Some(ready_task) = self.ready_task.take() {
                        ready_task.send(());
                    }
                }
                2 => self.ready_timer(),
                3 => self.step(),
                _ => self.queue_shutdown(),
            }
        }

        fn queue_factory_batch(&mut self, operation: u8) {
            let batch_size = match (operation / 6) % 4 {
                0 => 1,
                1 => COMMANDS_PER_CYCLE - 1,
                2 => COMMANDS_PER_CYCLE,
                _ => COMMANDS_PER_CYCLE + 1,
            };
            for _ in 0..batch_size {
                let invocations = Arc::clone(&self.factory_invocations);
                let forbidden = Arc::clone(&self.forbidden_factory_invocations);
                let forbidden_after_queued_shutdown = self.queued_shutdown;
                if self
                    .command_tx
                    .send(enqueue(RemoteFutureFactory::new(move |_, _| {
                        invocations.fetch_add(1, Ordering::Relaxed);
                        if forbidden_after_queued_shutdown {
                            forbidden.fetch_add(1, Ordering::Relaxed);
                        }
                    })))
                    .is_err()
                {
                    break;
                }
                self.queued_factories += 1;
                self.sent_factories += 1;
            }
        }

        fn ready_timer(&mut self) {
            if !self.timer_ready {
                self.control.advance(Duration::from_secs(1));
                self.timer_ready = true;
            }
        }

        fn step(&mut self) {
            if self.worker.executor.is_none() {
                return;
            }
            Signal::waker(&self.signal).wake_by_ref();
            let full_batch = self.queued_factories >= COMMANDS_PER_CYCLE && !self.queued_shutdown;
            let task_should_progress =
                full_batch && self.ready_task.is_none() && !self.task_done.load(Ordering::Relaxed) && self.progress_failure.is_none();
            let timer_should_progress =
                full_batch && self.timer_advanced_by_step && !self.timer_done.load(Ordering::Relaxed) && self.progress_failure.is_none();
            let running = self.worker.execute_step().is_continue();
            if running && task_should_progress && !self.task_done.load(Ordering::Relaxed) {
                self.progress_failure = Some("ready task must progress between full command batches");
            }
            if running && timer_should_progress && !self.timer_done.load(Ordering::Relaxed) {
                self.progress_failure = Some("ready timer must progress between full command batches");
            }
            if full_batch {
                self.queued_factories -= COMMANDS_PER_CYCLE;
            } else if !self.queued_shutdown {
                self.queued_factories = 0;
            }
            self.timer_advanced_by_step |= self.timer_ready;
        }

        fn queue_shutdown(&mut self) {
            if !self.queued_shutdown {
                self.worker.shutdown.begin(ShutdownCause::default());
            }
            if !self.queued_shutdown && self.command_tx.send(Command::Shutdown).is_ok() {
                self.queued_shutdown = true;
            }
        }

        fn finish(mut self) {
            if let Some(ready_task) = self.ready_task.take() {
                ready_task.send(());
            }
            self.ready_timer();
            self.queue_shutdown();

            let mut stopped = self.worker.executor.is_none();
            for _ in 0..Self::MAX_CLEANUP_STEPS {
                if stopped {
                    break;
                }
                Signal::waker(&self.signal).wake_by_ref();
                stopped = self.worker.execute_step().is_break();
            }
            if !stopped {
                std::mem::forget(self.worker);
                panic!("bounded cleanup must complete worker shutdown");
            }
            assert!(self.progress_failure.is_none(), "{}", self.progress_failure.unwrap_or_default());
            assert_eq!(
                self.forbidden_factory_invocations.load(Ordering::Relaxed),
                0,
                "factories queued after shutdown or pending at shutdown publication must not be invoked"
            );
            assert!(self.factory_invocations.load(Ordering::Relaxed) <= self.sent_factories);
        }
    }

    #[test]
    fn suspend_budget_requires_suspend_outcome_and_non_exhausted_batch() {
        assert!(can_suspend(&CycleOutcome::Suspend, false));
        assert!(!can_suspend(&CycleOutcome::Suspend, true));
        assert!(!can_suspend(&CycleOutcome::Continue, false));
        assert!(!can_suspend(&CycleOutcome::Continue, true));
        assert!(!can_suspend(&CycleOutcome::Shutdown(ShutdownCause::default()), false));
        assert!(!can_suspend(&CycleOutcome::Shutdown(ShutdownCause::default()), true));
    }

    #[test]
    fn private_suspend_wait_uses_current_time() {
        // The exact wait calculation is private worker-loop policy; public timer wake and
        // completion behavior is covered by integration tests in `tests/time.rs`.
        let now = Instant::now();

        assert_eq!(
            suspend_wait(now + Duration::from_secs(2), now + Duration::from_secs(1)),
            Duration::from_secs(1)
        );
        assert_eq!(suspend_wait(now + Duration::from_millis(20), now), Duration::from_millis(20));
        assert_eq!(
            suspend_wait(
                now.checked_sub(Duration::from_millis(1))
                    .expect("the current instant supports a 1 ms subtraction"),
                now
            ),
            Duration::ZERO
        );
    }

    #[test]
    fn worker_preserves_monitor_options() {
        let (command_tx, command_rx) = channel::unbounded();
        let signal = Arc::new(Signal::default());
        let worker = test_worker_with_monitor_options(
            command_tx,
            command_rx,
            test_blocking_worker(),
            InactiveClock::new_isolated(),
            signal,
            MonitorPolicy::disabled().into(),
        );

        assert!(!worker.monitor_options.policy().is_enabled());
    }

    #[cfg(not(miri))]
    #[test]
    fn worker_without_primary_waits_for_timer_or_notification() {
        let (worker_ready, worker_setup) = mpsc::channel();
        let (step_done, step_completion) = mpsc::channel();
        let worker_thread = thread::spawn(move || {
            let (command_tx, command_rx) = channel::unbounded();
            let signal = Arc::new(Signal::default());
            let mut worker = test_worker(
                command_tx.clone(),
                command_rx,
                test_blocking_worker(),
                InactiveClock::new_isolated(),
                Arc::clone(&signal),
            );
            let clock = worker.context.as_ref().unwrap().clock().clone();
            let mut delay = Box::pin(clock.delay(Duration::from_mins(1)));
            assert!(delay.as_mut().poll(&mut std::task::Context::from_waker(Waker::noop())).is_pending());
            signal.wait(Duration::ZERO);
            worker_ready.send(Signal::waker(&signal)).unwrap();

            assert!(worker.execute_step().is_continue());
            step_done.send(()).unwrap();

            drop(delay);
            drop(clock);
            command_tx.send(Command::Shutdown).unwrap();
            worker.run();
        });
        let worker_waker = worker_setup.recv_timeout(TEST_TIMEOUT).unwrap();

        assert_eq!(
            step_completion.recv_timeout(Duration::from_millis(50)),
            Err(mpsc::RecvTimeoutError::Timeout)
        );
        worker_waker.wake_by_ref();
        step_completion.recv_timeout(TEST_TIMEOUT).unwrap();
        worker_thread.join().unwrap();
    }

    #[cfg(not(miri))]
    #[test]
    fn first_cross_thread_timer_interrupts_established_idle_wait() {
        struct TimerWake(mpsc::Sender<()>);

        impl Wake for TimerWake {
            fn wake(self: std::sync::Arc<Self>) {
                _ = self.0.send(());
            }
        }

        let (wait_entered, wait_entry) = mpsc::channel();
        let (worker_ready, worker_setup) = mpsc::channel();
        let worker_thread = thread::spawn(move || {
            let (command_tx, command_rx) = channel::unbounded();
            let signal = Arc::new(Signal::with_wait_observer(wait_entered));
            let worker_waker = Signal::waker(&signal);
            let mut worker = test_worker(
                command_tx.clone(),
                command_rx,
                test_blocking_worker(),
                InactiveClock::new_isolated(),
                signal,
            );
            let clock = worker.context.as_ref().unwrap().clock().clone();
            worker_ready.send((command_tx, worker_waker, clock)).unwrap();
            worker.run();
        });
        let (command_tx, worker_waker, clock) = worker_setup.recv_timeout(TEST_TIMEOUT).unwrap();
        wait_entry.recv_timeout(TEST_TIMEOUT).unwrap();
        let (timer_fired, timer_completion) = mpsc::channel();
        let timer_waker = Waker::from(std::sync::Arc::new(TimerWake(timer_fired)));
        let mut delay = Box::pin(clock.delay(Duration::from_millis(1)));

        assert!(delay.as_mut().poll(&mut std::task::Context::from_waker(&timer_waker)).is_pending());
        timer_completion.recv_timeout(TEST_TIMEOUT).unwrap();

        drop(delay);
        drop(clock);
        command_tx.send(Command::Shutdown).unwrap();
        worker_waker.wake_by_ref();
        worker_thread.join().unwrap();
    }

    #[cfg(not(miri))]
    #[test]
    fn timer_callback_direct_submission_notifies_worker() {
        struct SpawnOnWake {
            scheduler: crate::task::Scheduler,
            invoked: Arc<AtomicBool>,
        }

        impl Wake for SpawnOnWake {
            fn wake(self: std::sync::Arc<Self>) {
                let invoked = Arc::clone(&self.invoked);
                drop(self.scheduler.spawn(move |_| async move {
                    invoked.store(true, Ordering::Release);
                }));
            }
        }

        let control = ClockControl::new();
        let (command_tx, command_rx) = channel::unbounded();
        let signal = Arc::new(Signal::default());
        let mut worker = test_worker(
            command_tx,
            command_rx,
            test_blocking_worker(),
            control.clone().into(),
            Arc::clone(&signal),
        );
        let clock = worker.context.as_ref().unwrap().clock().clone();
        let invoked = Arc::new(AtomicBool::new(false));
        let callback_waker = Waker::from(std::sync::Arc::new(SpawnOnWake {
            scheduler: worker.context.as_ref().unwrap().scheduler().clone(),
            invoked: Arc::clone(&invoked),
        }));
        let mut delay = Box::pin(clock.delay(Duration::from_secs(1)));
        assert!(
            delay
                .as_mut()
                .poll(&mut std::task::Context::from_waker(&callback_waker))
                .is_pending()
        );
        while signal.take_notification() {}

        control.advance(Duration::from_secs(1));

        assert!(signal.is_notified());
        assert!(worker.execute_step().is_continue());
        assert!(invoked.load(Ordering::Acquire));
    }

    #[cfg(not(miri))]
    #[test]
    fn generated_command_sequences_preserve_progress_and_close_admission() {
        bolero::check!().for_each(|input: &[u8]| {
            let mut scenario = GeneratedWorkerScenario::new();
            for operation in input.iter().copied().take(GeneratedWorkerScenario::MAX_OPERATIONS) {
                scenario.apply(operation);
            }
            scenario.finish();
        });
    }

    #[test]
    fn a_never_empty_command_queue_allows_registered_tasks_and_timers_to_progress() {
        let (command_tx, command_rx) = channel::unbounded();
        let (proceed, progress) = Event::boxed();
        let control = ClockControl::new();
        let task_done = Arc::new(AtomicBool::new(false));
        let timer_done = Arc::new(AtomicBool::new(false));
        let processed = Arc::new(AtomicUsize::new(0));
        let task_observer = Arc::clone(&task_done);
        let timer_observer = Arc::clone(&timer_done);
        let shutdown = command_tx.clone();

        let mut worker = test_worker(
            command_tx.clone(),
            command_rx,
            test_blocking_worker(),
            control.clone().into(),
            Arc::new(Signal::default()),
        );
        worker.tasks.add_test(async move {
            progress.await.unwrap();
            task_observer.store(true, Ordering::Relaxed);
        });
        let clock = worker.context.as_ref().unwrap().clock().clone();
        worker.tasks.add_test(async move {
            clock.delay(Duration::from_secs(1)).await;
            timer_observer.store(true, Ordering::Relaxed);
            shutdown.send(Command::Shutdown).unwrap();
        });
        _ = worker.executor.as_mut().unwrap().execute_cycle();
        assert!(!task_done.load(Ordering::Relaxed));
        assert!(!timer_done.load(Ordering::Relaxed));

        command_tx
            .send(replenishing_command(command_tx.clone(), Arc::clone(&processed)))
            .unwrap();
        proceed.send(());
        control.advance(Duration::from_secs(1));
        worker.run();

        assert!(task_done.load(Ordering::Relaxed));
        assert!(timer_done.load(Ordering::Relaxed));
        assert!((COMMANDS_PER_CYCLE..=2 * COMMANDS_PER_CYCLE + 1).contains(&processed.load(Ordering::Relaxed)));
    }

    #[test]
    fn a_full_command_batch_skips_waiting_for_the_worker_signal() {
        let (command_tx, command_rx) = channel::unbounded();
        let signal = Arc::new(Signal::default());

        let mut worker = test_worker(
            command_tx.clone(),
            command_rx,
            test_blocking_worker(),
            InactiveClock::new_isolated(),
            Arc::clone(&signal),
        );
        Signal::waker(&signal).wake_by_ref();
        assert!(worker.execute_step().is_continue());

        Signal::waker(&signal).wake_by_ref();
        assert!(signal.is_notified());
        assert!(worker.execute_step().is_continue());
        assert!(!signal.is_notified());

        for _ in 0..COMMANDS_PER_CYCLE {
            command_tx.send(enqueue(RemoteFutureFactory::new(|_, _| {}))).unwrap();
        }
        Signal::waker(&signal).wake_by_ref();
        assert!(signal.is_notified());
        assert!(worker.execute_step().is_continue());
        assert!(signal.is_notified());

        command_tx.send(Command::Shutdown).unwrap();
        worker.run();
    }

    #[test]
    fn a_full_command_batch_gives_the_primary_driver_no_wait_budget_and_skips_signal_wait() {
        let (command_tx, command_rx) = channel::unbounded();
        let signal = Arc::new(Signal::default());
        let mut worker = test_worker(
            command_tx.clone(),
            command_rx,
            test_blocking_worker(),
            InactiveClock::new_isolated(),
            Arc::clone(&signal),
        );
        let waits = Arc::new(std::sync::Mutex::new(Vec::new()));
        let _context = worker
            .context
            .as_ref()
            .unwrap()
            .io()
            .context::<WaitRecordingContext>(&Arc::clone(&waits));
        for _ in 0..COMMANDS_PER_CYCLE {
            command_tx.send(enqueue(RemoteFutureFactory::new(|_, _| {}))).unwrap();
        }
        Signal::waker(&signal).wake_by_ref();
        assert!(signal.is_notified());

        assert!(worker.execute_step().is_continue());
        assert_eq!(*waits.lock().unwrap(), vec![Duration::ZERO]);
        assert!(signal.is_notified());

        command_tx.send(Command::Shutdown).unwrap();
        worker.run();
    }

    #[cfg(not(miri))]
    #[test]
    fn final_timer_panic_occurs_after_executor_drop() {
        struct PanicWake;

        impl Wake for PanicWake {
            fn wake(self: std::sync::Arc<Self>) {
                panic!("final timer callback");
            }
        }

        let (command_tx, command_rx) = channel::unbounded();
        let mut worker = test_worker(
            command_tx.clone(),
            command_rx,
            test_blocking_worker(),
            InactiveClock::new_isolated(),
            Arc::new(Signal::default()),
        );
        let clock = worker.context.as_ref().unwrap().clock().clone();
        let mut timer = clock.delay(Duration::from_millis(1));
        let waker = Waker::from(Arc::into_std_arc(Arc::new(PanicWake)));
        assert!(Pin::new(&mut timer).poll(&mut std::task::Context::from_waker(&waker)).is_pending());
        // Forgetting a timer is legal and avoids a second panic from the poisoned timer lock.
        std::mem::forget(timer);
        thread::sleep(Duration::from_millis(2));
        command_tx.send(Command::Shutdown).unwrap();

        let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| worker.run()));
        assert_eq!(*outcome.unwrap_err().downcast::<&str>().unwrap(), "final timer callback");
    }

    #[test]
    fn smoke_test() {
        async_test(async || {
            // Run worker, execute one remote task which awaits a nested task, then shut down.
            let (command_tx, command_rx) = channel::unbounded();
            let worker_command_tx = command_tx.clone();
            let signal = Arc::new(Signal::default());
            let worker_signal = Arc::clone(&signal);

            let async_worker_thread = thread::spawn(move || {
                let blocking_worker = test_blocking_worker();

                let mut worker = test_worker(
                    worker_command_tx,
                    command_rx,
                    (Arc::clone(&blocking_worker.0), blocking_worker.1.clone()),
                    InactiveClock::new_isolated(),
                    worker_signal,
                );
                worker.run();

                assert!(blocking_worker.0.is_shutting_down());
            });

            // Signals that the task waiting on the nested task has completed.
            let (outer_completed_tx, outer_completed_rx) = Event::boxed();

            // Signals that the nested task has completed.
            let (inner_completed_tx, inner_completed_rx) = Event::boxed();

            command_tx
                .send(enqueue(RemoteFutureFactory::new({
                    move |_: Context, tasks: &TaskSet| {
                        tasks.add_test(async move {
                            inner_completed_tx.send(());
                        });
                        tasks.add_test(async move {
                            inner_completed_rx.await.unwrap();
                            outer_completed_tx.send(());
                        });
                    }
                })))
                .unwrap();
            Signal::waker(&signal).wake_by_ref();

            // Wait for the command to complete.
            outer_completed_rx.await.unwrap();

            // Shut down after the test body completes.
            command_tx.send(Command::Shutdown).unwrap();
            Signal::waker(&signal).wake_by_ref();

            // Join the worker so this test reports shutdown errors.
            async_worker_thread.join().unwrap();
        });
    }

    #[test]
    fn worker_setup_can_register_new_tasks() {
        async_test(async || {
            // Observe both tasks.
            let (initial_completed_tx, initial_completed_rx) = Event::boxed();
            let (spawned_completed_tx, spawned_completed_rx) = Event::boxed();

            let (command_tx, command_rx) = channel::unbounded();
            let worker_command_tx = command_tx.clone();
            let signal = Arc::new(Signal::default());
            let worker_signal = Arc::clone(&signal);
            let worker_thread = thread::spawn(move || {
                let mut worker = test_worker(
                    worker_command_tx,
                    command_rx,
                    test_blocking_worker(),
                    InactiveClock::new_isolated(),
                    worker_signal,
                );
                worker.tasks.add_test(async move { initial_completed_tx.send(()) });

                worker.run();
            });

            command_tx
                .send(enqueue(RemoteFutureFactory::new({
                    move |_: Context, tasks: &TaskSet| {
                        tasks.add_test(async move {
                            spawned_completed_tx.send(());
                        });
                    }
                })))
                .unwrap();
            Signal::waker(&signal).wake_by_ref();

            spawned_completed_rx.await.unwrap();
            initial_completed_rx.into_value().unwrap();

            // Shut down after both tasks complete.
            command_tx.send(Command::Shutdown).unwrap();
            Signal::waker(&signal).wake_by_ref();

            // Join the worker so this test reports shutdown errors.
            worker_thread.join().unwrap();
        });
    }

    #[test]
    fn task_after_shutdown_is_ignored() {
        // The asynchronous submission path can enqueue a new-task command after
        // shutdown starts.
        //
        // The worker drops these tasks so their remote join handles report
        // shutdown and release all task resources.

        let (command_tx, command_rx) = channel::unbounded();
        let worker_command_tx = command_tx.clone();
        let signal = Arc::new(Signal::default());
        let worker_signal = Arc::clone(&signal);

        let async_worker_thread = thread::spawn(move || {
            let mut worker = test_worker(
                worker_command_tx,
                command_rx,
                test_blocking_worker(),
                InactiveClock::new_isolated(),
                worker_signal,
            );
            worker.run();
        });

        command_tx.send(Command::Shutdown).unwrap();
        Signal::waker(&signal).wake_by_ref();

        let (completed_tx, completed_rx) = Event::boxed();

        // The channel can already be closed because the worker is shutting down.
        let _ = command_tx.send(enqueue(RemoteFutureFactory::new({
            move |_: Context, tasks: &TaskSet| {
                tasks.add_test(async move {
                    completed_tx.send(14);
                });
            }
        })));

        // This waits for the worker to shut down.
        async_worker_thread.join().unwrap();

        let completed_result = completed_rx.into_value();
        assert!(matches!(completed_result, Err(IntoValueError::Disconnected)));
    }

    #[test]
    fn shutdown_command_cancels_registered_tasks_before_polling() {
        let (command_tx, command_rx) = channel::unbounded();
        let invoked = Arc::new(AtomicBool::new(false));
        let invoked_by_task = Arc::clone(&invoked);

        let mut worker = test_worker(
            command_tx.clone(),
            command_rx,
            test_blocking_worker(),
            InactiveClock::new_isolated(),
            Arc::new(Signal::default()),
        );

        command_tx
            .send(enqueue(RemoteFutureFactory::new(move |_, tasks| {
                tasks.add_test(async move {
                    invoked_by_task.store(true, Ordering::Relaxed);
                });
            })))
            .unwrap();
        command_tx.send(Command::Shutdown).unwrap();

        worker.run();

        assert!(!invoked.load(Ordering::Relaxed));
    }

    #[test]
    fn same_worker_stop_skips_a_later_initialized_task() {
        let (command_tx, command_rx) = channel::unbounded();
        let mut worker = test_worker(
            command_tx,
            command_rx,
            test_blocking_worker(),
            InactiveClock::new_isolated(),
            Arc::new(Signal::default()),
        );
        let control = worker.context.as_ref().unwrap().control().clone();
        let invoked = Arc::new(AtomicBool::new(false));
        let invoked_by_task = Arc::clone(&invoked);
        worker.tasks.add_test(async move {
            control.request_stop();
        });
        worker.tasks.add_test(async move {
            invoked_by_task.store(true, Ordering::Release);
        });
        let executor = worker.executor.as_mut().unwrap();

        assert!(matches!(executor.execute_cycle(), CycleOutcome::Shutdown(_)));
        assert!(!invoked.load(Ordering::Acquire));
    }

    #[test]
    fn repeated_shutdown_cleanup_is_idempotent() {
        let (command_tx, command_rx) = channel::unbounded();
        let mut worker = test_worker(
            command_tx,
            command_rx,
            test_blocking_worker(),
            InactiveClock::new_isolated(),
            Arc::new(Signal::default()),
        );

        worker.begin_shutdown();
        worker.begin_shutdown();
    }

    #[test]
    fn published_shutdown_discards_queued_io_access_without_invoking_it() {
        struct DropSignal(mpsc::Sender<()>);

        impl Drop for DropSignal {
            fn drop(&mut self) {
                _ = self.0.send(());
            }
        }

        let (command_tx, command_rx) = channel::unbounded();
        let mut worker = test_worker(
            command_tx.clone(),
            command_rx,
            test_blocking_worker(),
            InactiveClock::new_isolated(),
            Arc::new(Signal::default()),
        );
        let invoked = Arc::new(AtomicBool::new(false));
        let (dropped_tx, dropped_rx) = mpsc::channel();
        let drop_signal = DropSignal(dropped_tx);
        command_tx
            .send(Command::AccessIo {
                access: QueuedIoAccess::new(Box::new({
                    let invoked = Arc::clone(&invoked);
                    move |_| {
                        let _ = &drop_signal;
                        invoked.store(true, Ordering::Release);
                    }
                })),
            })
            .unwrap();
        worker.shutdown.begin(ShutdownCause::default());

        worker.run();

        dropped_rx.recv_timeout(TEST_TIMEOUT).unwrap();
        assert!(!invoked.load(Ordering::Acquire));
    }

    #[test]
    fn shutdown_published_during_final_batch_registration_cancels_before_polling() {
        let (command_tx, command_rx) = channel::unbounded();
        let invoked = Arc::new(AtomicUsize::new(0));
        let mut worker = test_worker(
            command_tx.clone(),
            command_rx,
            test_blocking_worker(),
            InactiveClock::new_isolated(),
            Arc::new(Signal::default()),
        );
        let shutdown = worker.shutdown.clone();

        for index in 0..COMMANDS_PER_CYCLE {
            let invoked = Arc::clone(&invoked);
            let shutdown = shutdown.clone();
            command_tx
                .send(enqueue(RemoteFutureFactory::new(move |_, tasks| {
                    if index + 1 == COMMANDS_PER_CYCLE {
                        shutdown.begin(ShutdownCause::default());
                    }
                    tasks.add_test(async move {
                        invoked.fetch_add(1, Ordering::Relaxed);
                    });
                })))
                .unwrap();
        }

        worker.run();

        assert_eq!(invoked.load(Ordering::Relaxed), 0);
    }

    #[test]
    fn dropping_worker_without_run_cleans_up_safely() {
        let (commands, receiver) = channel::unbounded();
        let blocking_worker = test_blocking_worker();
        let worker = test_worker(
            commands,
            receiver,
            (Arc::clone(&blocking_worker.0), blocking_worker.1.clone()),
            InactiveClock::new_isolated(),
            Arc::new(Signal::default()),
        );

        drop(worker);

        assert!(blocking_worker.0.is_shutting_down());
    }

    #[test]
    fn explicit_executor_shutdown_completes_worker_shutdown() {
        let (commands, receiver) = channel::unbounded();
        let blocking_worker = test_blocking_worker();
        let signal = Arc::new(Signal::default());
        let mut worker = test_worker(
            commands.clone(),
            receiver,
            (Arc::clone(&blocking_worker.0), blocking_worker.1.clone()),
            InactiveClock::new_isolated(),
            Arc::clone(&signal),
        );
        Signal::waker(&signal).wake_by_ref();
        assert!(worker.execute_step().is_continue());
        worker.executor.as_ref().unwrap().shutdown().trigger(ShutdownCause::default());

        let outcome = worker.execute_step();

        assert!(outcome.is_break());
        assert!(worker.executor.is_none());
        assert!(blocking_worker.0.is_shutting_down());
        assert!(!worker.process_commands());
        drop(commands);
    }
}