robot-control 0.1.9

A Rust library for controlling DC motor robots via PCA9685 PWM boards over I2C
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
use std::sync::{
    Condvar, Mutex as StdMutex,
    atomic::{AtomicBool, Ordering},
};

use tokio::sync::Notify;

use super::*;

const MOTORS: [Motor; 4] = [Motor::Motor1, Motor::Motor2, Motor::Motor3, Motor::Motor4];

#[derive(Debug, Clone, PartialEq, Eq)]
enum Event {
    Speed(Motor, u16, bool),
    Cleanup,
    StopAttempt(Motor),
}

// A synchronous driver call really stays in flight until the test releases it.
// Notify is the async entry barrier; the condvar holds the executor's I/O thread.
#[derive(Default)]
struct Gate {
    claimed: AtomicBool,
    entered: Notify,
    released: StdMutex<bool>,
    wake: Condvar,
}

impl Gate {
    fn block_once(&self) {
        if self.claimed.swap(true, Ordering::SeqCst) {
            return;
        }
        self.entered.notify_one();
        let mut released = self.released.lock().unwrap();
        while !*released {
            released = self.wake.wait(released).unwrap();
        }
    }

    fn release(&self) {
        *self.released.lock().unwrap() = true;
        self.wake.notify_all();
    }
}

// Release blocked synchronous I/O even if an assertion or deadlock guard fails.
struct ReleaseOnDrop(Arc<Gate>);

impl Drop for ReleaseOnDrop {
    fn drop(&mut self) {
        self.0.release();
    }
}

#[derive(Default)]
struct TestDriver {
    events: Arc<StdMutex<Vec<Event>>>,
    write_gate: Option<Arc<Gate>>,
    cleanup_gate: Option<Arc<Gate>>,
    cleanup_finished: Arc<Notify>,
    fail_stop: bool,
    fail_write: Option<Motor>,
    fail_zero_stop: bool,
    failure_detail: Option<String>,
    panic_write: bool,
}

impl MotorDriver for TestDriver {
    fn set_motor_speed(&self, motor: Motor, speed: u16, forward: bool) -> Result<(), MotorError> {
        if let Some(gate) = &self.write_gate {
            gate.block_once();
        }
        self.events
            .lock()
            .unwrap()
            .push(Event::Speed(motor, speed, forward));
        assert!(!self.panic_write, "injected adjustment panic");
        if self.fail_write == Some(motor) {
            return Err(MotorError::I2c(
                self.failure_detail
                    .clone()
                    .unwrap_or_else(|| "injected write failure".into()),
            ));
        }
        Ok(())
    }

    fn stop_motor(&self, motor: Motor) -> Result<(), MotorError> {
        self.events.lock().unwrap().push(Event::StopAttempt(motor));
        if (self.fail_stop && motor == Motor::Motor2)
            || (self.fail_zero_stop && motor == Motor::Motor4)
        {
            Err(MotorError::I2c(
                self.failure_detail
                    .clone()
                    .unwrap_or_else(|| "injected stop failure".into()),
            ))
        } else {
            Ok(())
        }
    }

    fn stop_all(&self) -> Result<(), MotorError> {
        self.events.lock().unwrap().push(Event::Cleanup);
        if let Some(gate) = &self.cleanup_gate {
            gate.block_once();
        }
        let mut failure = None;
        for motor in MOTORS {
            if let Err(error) = self.stop_motor(motor) {
                failure = Some(error);
            }
        }
        self.cleanup_finished.notify_one();
        failure.map_or(Ok(()), Err)
    }
}

fn create_robot(driver: TestDriver) -> (Arc<Robot>, Arc<Mutex<TestDriver>>) {
    let driver = Arc::new(Mutex::new(driver));
    let robot = Robot::new(
        driver.clone(),
        RobotConfig {
            left_motor: Motor::Motor4,
            right_motor: Motor::Motor1,
            ..RobotConfig::default()
        },
    )
    .unwrap();
    (robot, driver)
}

async fn assert_pending<F: std::future::Future>(mut future: std::pin::Pin<&mut F>) {
    std::future::poll_fn(|cx| {
        assert!(future.as_mut().poll(cx).is_pending());
        std::task::Poll::Ready(())
    })
    .await;
}

fn assert_final_cleanup(events: &[Event]) {
    assert_eq!(
        events
            .iter()
            .filter(|event| **event == Event::Cleanup)
            .count(),
        1
    );
    let cleanup = events
        .iter()
        .position(|event| *event == Event::Cleanup)
        .unwrap();
    assert_eq!(&events[cleanup + 1..], MOTORS.map(Event::StopAttempt));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn shutdown_joins_in_flight_write_before_final_stop() {
    tokio::time::timeout(Duration::from_secs(5), async {
        let gate = Arc::new(Gate::default());
        let _release = ReleaseOnDrop(gate.clone());
        let driver = TestDriver {
            write_gate: Some(gate.clone()),
            ..TestDriver::default()
        };
        let events = driver.events.clone();
        let (robot, _driver) = create_robot(driver);
        robot.drive(0.25, -0.25).await.unwrap();
        gate.entered.notified().await;

        let mut first = Box::pin(robot.shutdown());
        let mut second = Box::pin(robot.shutdown());
        assert_pending(first.as_mut()).await;
        assert_pending(second.as_mut()).await;
        assert!(matches!(
            robot.drive(-0.5, 0.5).await,
            Err(RobotError::Shutdown)
        ));
        assert!(matches!(robot.stop().await, Err(RobotError::Shutdown)));
        assert!(!events.lock().unwrap().contains(&Event::Cleanup));

        gate.release();
        tokio::join!(first, second);
        let completed = events.lock().unwrap().clone();
        assert!(
            completed
                .iter()
                .any(|event| matches!(event, Event::Speed(_, 1.., _)))
        );
        assert_final_cleanup(&completed);
        // Both callers joined the only writer before the final stop. A repeated
        // shutdown cannot restart adjustment or run a second cleanup sequence.
        robot.shutdown().await;
        tokio::task::yield_now().await;
        assert_eq!(*events.lock().unwrap(), completed);
        assert!(robot.cleanup.lock().await.is_none());
    })
    .await
    .expect("shutdown did not join the in-flight write");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn cancelled_shutdown_waiter_preserves_cleanup_for_other_callers() {
    tokio::time::timeout(Duration::from_secs(5), async {
        let gate = Arc::new(Gate::default());
        let _release = ReleaseOnDrop(gate.clone());
        let driver = TestDriver {
            cleanup_gate: Some(gate.clone()),
            ..TestDriver::default()
        };
        let events = driver.events.clone();
        let (robot, _driver) = create_robot(driver);
        let mut first = Box::pin(robot.shutdown());
        assert_pending(first.as_mut()).await;
        // Drop the actual pinned future, not just a Pin<&mut _> reference.
        drop(first);
        gate.entered.notified().await;

        let mut second = Box::pin(robot.shutdown());
        let mut third = Box::pin(robot.shutdown());
        assert_pending(second.as_mut()).await;
        assert_pending(third.as_mut()).await;
        assert!(matches!(
            robot.drive(0.5, 0.5).await,
            Err(RobotError::Shutdown)
        ));
        gate.release();
        tokio::join!(second, third);
        assert_final_cleanup(&events.lock().unwrap());
    })
    .await
    .expect("a cancelled waiter lost cleanup");
}

#[tokio::test(start_paused = true)]
async fn last_handle_drop_cleans_up_with_external_driver_reference() {
    tokio::time::timeout(Duration::from_secs(5), async {
        let driver = TestDriver::default();
        let events = driver.events.clone();
        let finished = driver.cleanup_finished.clone();
        let (robot, external_driver) = create_robot(driver);
        let public = Arc::downgrade(&robot);
        let state = Arc::downgrade(&robot.state);
        let last = robot.clone();
        drop(robot);
        assert!(public.upgrade().is_some());
        last.drive(0.25, 0.25).await.unwrap();
        tokio::task::yield_now().await;
        tokio::time::advance(Duration::from_millis(10)).await;
        tokio::task::yield_now().await;
        assert!(
            events
                .lock()
                .unwrap()
                .iter()
                .any(|event| matches!(event, Event::Speed(_, 1.., _)))
        );

        drop(last);
        assert!(public.upgrade().is_none(), "task retained the public Robot");
        finished.notified().await;
        // On this single-thread runtime, cleanup finishes its synchronous stop
        // and drops its state before this notified test can run again.
        assert!(
            state.upgrade().is_none(),
            "cleanup retained its internal state"
        );
        assert_eq!(Arc::strong_count(&external_driver), 1);
        let completed = events.lock().unwrap().clone();
        assert_final_cleanup(&completed);
        tokio::time::advance(Duration::from_secs(1)).await;
        tokio::task::yield_now().await;
        assert_eq!(*events.lock().unwrap(), completed);
    })
    .await
    .expect("last-handle drop retained the adjustment task");
}

#[tokio::test]
async fn cancelled_shutdown_and_last_handle_drop_still_finish_cleanup() {
    tokio::time::timeout(Duration::from_secs(5), async {
        let driver = TestDriver::default();
        let events = driver.events.clone();
        let finished = driver.cleanup_finished.clone();
        let (robot, external_driver) = create_robot(driver);
        let board = external_driver.lock().await;
        let mut shutdown = Box::pin(robot.shutdown());
        assert_pending(shutdown.as_mut()).await;
        drop(shutdown);
        let public = Arc::downgrade(&robot);
        drop(robot);
        assert!(public.upgrade().is_none());
        drop(board);
        finished.notified().await;
        assert_final_cleanup(&events.lock().unwrap());
    })
    .await
    .expect("cleanup depended on a surviving shutdown caller");
}

#[tokio::test(start_paused = true)]
async fn shutdown_discards_adjustment_waiting_for_board() {
    tokio::time::timeout(Duration::from_secs(5), async {
        let driver = TestDriver::default();
        let events = driver.events.clone();
        let (robot, external_driver) = create_robot(driver);
        let board = external_driver.lock().await;
        robot.drive(0.25, 0.25).await.unwrap();
        tokio::task::yield_now().await;
        tokio::time::advance(Duration::from_millis(10)).await;
        tokio::task::yield_now().await;
        let mut shutdown = Box::pin(robot.shutdown());
        assert_pending(shutdown.as_mut()).await;
        drop(board);
        shutdown.await;
        let completed = events.lock().unwrap().clone();
        assert_final_cleanup(&completed);
        assert!(
            !completed
                .iter()
                .any(|event| matches!(event, Event::Speed(..)))
        );
        tokio::time::advance(Duration::from_secs(1)).await;
        tokio::task::yield_now().await;
        assert_eq!(*events.lock().unwrap(), completed);
    })
    .await
    .expect("shutdown did not finish after the board was released");
}

#[tokio::test]
async fn stop_failure_does_not_prevent_cleanup_completion() {
    tokio::time::timeout(Duration::from_secs(5), async {
        let driver = TestDriver {
            fail_stop: true,
            ..TestDriver::default()
        };
        let events = driver.events.clone();
        let (robot, _driver) = create_robot(driver);
        tokio::join!(robot.shutdown(), robot.shutdown());
        robot.shutdown().await;
        assert_final_cleanup(&events.lock().unwrap());
        assert!(matches!(
            robot.drive(0.5, 0.5).await,
            Err(RobotError::Motor(MotorError::Pwm(message))) if message.contains("injected stop failure")
        ));
        let status = robot.status().await;
        assert_eq!(status.phase, RobotPhase::Faulted);
        assert_eq!(status.output, RobotOutputState::Unknown);
        assert!(status.cleanup_complete);
        assert!(status.fault.is_none());
        assert!(status.cleanup_error.unwrap().contains("injected stop failure"));
    })
    .await
    .expect("stop failure prevented shutdown completion");
}

#[tokio::test(start_paused = true)]
async fn left_and_right_failures_latch_and_stop_without_later_drive_writes() {
    for (motor, side, attempted) in [(Motor::Motor4, "left", 1), (Motor::Motor1, "right", 2)] {
        for fail_stop in [false, true] {
            let driver = TestDriver {
                fail_write: Some(motor),
                fail_stop,
                ..TestDriver::default()
            };
            let events = driver.events.clone();
            let finished = driver.cleanup_finished.clone();
            let (robot, _driver) = create_robot(driver);
            assert_eq!(robot.status().await.phase, RobotPhase::Running);
            robot.drive(0.25, -0.25).await.unwrap();
            // Acceptance has not performed any hardware I/O or confirmed zero.
            assert!(events.lock().unwrap().is_empty());
            assert_eq!(robot.status().await.output, RobotOutputState::Unknown);
            tokio::time::timeout(Duration::from_secs(5), finished.notified())
                .await
                .unwrap();
            robot.shutdown().await;
            let status = robot.status().await;
            assert_eq!(status.phase, RobotPhase::Faulted);
            assert!(status.cleanup_complete);
            assert!(
                status
                    .fault
                    .as_ref()
                    .unwrap()
                    .contains(&format!("{side} motor {motor:?}"))
            );
            assert!(
                status
                    .fault
                    .as_ref()
                    .unwrap()
                    .contains("injected write failure")
            );
            assert_eq!(status.cleanup_error.is_some(), fail_stop);
            assert_eq!(
                status.output,
                if fail_stop {
                    RobotOutputState::Unknown
                } else {
                    RobotOutputState::Stopped
                }
            );
            assert_eq!(robot.state.control.lock().await.targets, (0.0, 0.0));
            for _ in 0..3 {
                assert!(matches!(robot.drive(0.5, 0.5).await,
                    Err(RobotError::Motor(MotorError::Pwm(message))) if message.contains("injected write failure")));
                assert!(matches!(
                    robot.stop().await,
                    Err(RobotError::Motor(MotorError::Pwm(_)))
                ));
                robot.shutdown().await;
                assert_eq!(robot.status().await, status);
            }
            let completed = events.lock().unwrap().clone();
            assert_eq!(
                completed
                    .iter()
                    .filter(|event| matches!(event, Event::Speed(..)))
                    .count(),
                attempted
            );
            assert_final_cleanup(&completed);
            tokio::time::advance(Duration::from_secs(1)).await;
            tokio::task::yield_now().await;
            assert_eq!(*events.lock().unwrap(), completed);
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fault_clears_pending_targets_and_rejects_waiters_while_cleanup_is_blocked() {
    tokio::time::timeout(Duration::from_secs(5), async {
        let write = Arc::new(Gate::default());
        let cleanup = Arc::new(Gate::default());
        let _release_write = ReleaseOnDrop(write.clone());
        let _release_cleanup = ReleaseOnDrop(cleanup.clone());
        let driver = TestDriver {
            write_gate: Some(write.clone()),
            cleanup_gate: Some(cleanup.clone()),
            fail_write: Some(Motor::Motor4),
            ..TestDriver::default()
        };
        let events = driver.events.clone();
        let (robot, _driver) = create_robot(driver);
        robot.drive(0.25, 0.25).await.unwrap();
        write.entered.notified().await;
        // Accepted while the preceding output is in flight; must not survive
        // that output's failure or get applied after fault cleanup.
        robot.drive(-0.75, 0.5).await.unwrap();
        write.release();
        cleanup.entered.notified().await;
        let status = robot.status().await;
        assert_eq!(status.phase, RobotPhase::Faulted);
        assert_eq!(status.output, RobotOutputState::Unknown);
        assert!(!status.cleanup_complete);
        assert!(
            status
                .fault
                .as_ref()
                .unwrap()
                .contains("injected write failure")
        );

        let control = robot.state.control.lock().await;
        assert_eq!(control.targets, (0.0, 0.0));
        let mut drive = Box::pin(robot.drive(1.0, -1.0));
        let mut stop = Box::pin(robot.stop());
        assert_pending(drive.as_mut()).await;
        assert_pending(stop.as_mut()).await;
        drop(control);
        assert!(matches!(
            drive.await,
            Err(RobotError::Motor(MotorError::Pwm(_)))
        ));
        assert!(matches!(
            stop.await,
            Err(RobotError::Motor(MotorError::Pwm(_)))
        ));
        let mut shutdown = Box::pin(robot.shutdown());
        assert_pending(shutdown.as_mut()).await;
        drop(shutdown);
        assert_eq!(robot.status().await, status);
        cleanup.release();
        robot.shutdown().await;
        let finished = robot.status().await;
        assert_eq!(finished.fault, status.fault);
        assert_eq!(finished.phase, RobotPhase::Faulted);
        assert_eq!(finished.output, RobotOutputState::Stopped);
        assert!(finished.cleanup_complete);
        let events = events.lock().unwrap();
        assert_eq!(
            events
                .iter()
                .filter(|event| matches!(event, Event::Speed(..)))
                .count(),
            1
        );
        assert_final_cleanup(&events);
    })
    .await
    .expect("fault handling or cleanup deadlocked");
}

#[tokio::test(start_paused = true)]
async fn healthy_status_distinguishes_admission_shutdown_and_successful_stop() {
    let (robot, driver) = create_robot(TestDriver::default());
    let initial = robot.status().await;
    assert_eq!(initial.phase, RobotPhase::Running);
    assert_eq!(initial.output, RobotOutputState::Unknown);
    assert!(!initial.cleanup_complete);
    robot.drive(0.25, 0.25).await.unwrap();
    let board = driver.lock().await;
    let mut shutdown = Box::pin(robot.shutdown());
    assert_pending(shutdown.as_mut()).await;
    let stopping = robot.status().await;
    assert_eq!(stopping.phase, RobotPhase::ShuttingDown);
    assert_eq!(stopping.output, RobotOutputState::Unknown);
    assert!(!stopping.cleanup_complete);
    drop(board);
    shutdown.await;
    let stopped = robot.status().await;
    assert_eq!(stopped.phase, RobotPhase::Shutdown);
    assert_eq!(stopped.output, RobotOutputState::Stopped);
    assert!(stopped.cleanup_complete);
    assert!(stopped.fault.is_none() && stopped.cleanup_error.is_none());
}

#[tokio::test(start_paused = true)]
async fn fault_diagnostics_are_bounded_and_survive_repeated_commands() {
    let driver = TestDriver {
        fail_write: Some(Motor::Motor4),
        fail_stop: true,
        failure_detail: Some("故障".repeat(5000)),
        ..TestDriver::default()
    };
    let finished = driver.cleanup_finished.clone();
    let (robot, _driver) = create_robot(driver);
    robot.drive(0.25, 0.25).await.unwrap();
    tokio::time::timeout(Duration::from_secs(5), finished.notified())
        .await
        .unwrap();
    robot.shutdown().await;
    let status = robot.status().await;
    for diagnostic in [&status.fault, &status.cleanup_error] {
        let message = diagnostic.as_ref().unwrap();
        assert!(message.len() <= 4096);
        assert!(message.ends_with(" [truncated]"));
    }
    for _ in 0..10 {
        assert!(robot.drive(0.5, 0.5).await.is_err());
        robot.shutdown().await;
        assert_eq!(robot.status().await, status);
    }
}

#[tokio::test(start_paused = true)]
async fn adjustment_panic_is_observable_and_cleanup_still_runs() {
    let driver = TestDriver {
        panic_write: true,
        ..TestDriver::default()
    };
    let finished = driver.cleanup_finished.clone();
    let events = driver.events.clone();
    let (robot, _driver) = create_robot(driver);
    robot.drive(0.25, 0.25).await.unwrap();
    tokio::time::timeout(Duration::from_secs(5), finished.notified())
        .await
        .unwrap();
    robot.shutdown().await;
    let status = robot.status().await;
    assert_eq!(status.phase, RobotPhase::Faulted);
    assert!(status.fault.unwrap().contains("injected adjustment panic"));
    assert_eq!(status.output, RobotOutputState::Stopped);
    assert!(status.cleanup_complete);
    assert_final_cleanup(&events.lock().unwrap());
}

#[tokio::test(start_paused = true)]
async fn background_zero_output_failure_also_latches() {
    let driver = TestDriver {
        fail_zero_stop: true,
        ..TestDriver::default()
    };
    let finished = driver.cleanup_finished.clone();
    let events = driver.events.clone();
    let (robot, _driver) = create_robot(driver);
    // No nonzero command: the adjustment loop still writes stopped outputs.
    tokio::time::timeout(Duration::from_secs(5), finished.notified())
        .await
        .unwrap();
    robot.shutdown().await;
    let status = robot.status().await;
    assert_eq!(status.phase, RobotPhase::Faulted);
    assert_eq!(status.output, RobotOutputState::Unknown);
    assert!(status.fault.unwrap().contains("left motor Motor4"));
    assert!(
        status
            .cleanup_error
            .unwrap()
            .contains("injected stop failure")
    );
    let events = events.lock().unwrap();
    assert_eq!(events[0], Event::StopAttempt(Motor::Motor4));
    assert_final_cleanup(&events);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn in_flight_failure_is_retained_when_shutdown_already_started() {
    tokio::time::timeout(Duration::from_secs(5), async {
        let gate = Arc::new(Gate::default());
        let _release = ReleaseOnDrop(gate.clone());
        let driver = TestDriver {
            write_gate: Some(gate.clone()),
            fail_write: Some(Motor::Motor4),
            ..TestDriver::default()
        };
        let (robot, _driver) = create_robot(driver);
        robot.drive(0.25, 0.25).await.unwrap();
        gate.entered.notified().await;
        let mut shutdown = Box::pin(robot.shutdown());
        assert_pending(shutdown.as_mut()).await;
        assert_eq!(robot.status().await.phase, RobotPhase::ShuttingDown);
        gate.release();
        shutdown.await;
        let status = robot.status().await;
        assert_eq!(status.phase, RobotPhase::Faulted);
        assert_eq!(status.output, RobotOutputState::Stopped);
        assert!(status.cleanup_complete);
        assert!(status.fault.unwrap().contains("injected write failure"));
    })
    .await
    .expect("shutdown lost an in-flight fault");
}