runnerd-core 0.1.4

Config, domain logic and API types shared by runnerd and its clients
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
//! Stopping on the fakes (ADR 0091): a signal removes idle runners and leaves busy ones and the mirror to the next
//! start, which takes them over; a stop ends when Docker is gone or fails midway, and within `STOP_DEADLINE` when Docker hangs; a second
//! signal kills running jobs; a stop waits for a record being made but not for a reseed or a check of the caches.

use std::sync::atomic::AtomicBool;

use super::*;
use crate::api::Stopping;
use crate::scheduler::engine::STOP_DEADLINE;

/// Job 1 runs in slot 1, an idle runner started just now waits in slot 2, and the mirror is up.
fn busy_and_idle(
    github: &FakeGithub,
    docker: &FakeDocker,
    dir: &TestDir,
) -> (Engine<FakeGithub, FakeDocker>, Handle) {
    busy_and_idle_with(github, docker, dir, CONFIG)
}

/// `busy_and_idle` on `config`, with the mirror enabled.
fn busy_and_idle_with(
    github: &FakeGithub,
    docker: &FakeDocker,
    dir: &TestDir,
    config: &str,
) -> (Engine<FakeGithub, FakeDocker>, Handle) {
    running_runner(github, docker, "@@job 1 1\n");
    let idle = identity("ci", ID_A).runner_name("app", 2, 1);
    {
        let mut state = github.0.lock().unwrap();
        state.jobs.insert(1, details(1, "in_progress", None));
        state.runners.push(runner(8, &idle, false));
    }
    {
        let mut state = docker.0.lock().unwrap();
        state.containers.push(runner_container(2, 8, true));
        // Its log capture finds no job.
        state
            .container_captures
            .insert("container-2".to_owned(), String::new());
        state
            .started
            .insert("container-2".to_owned(), crate::time::now_ms());
        let mirror = Names::new("runnerd").mirror();
        state.containers.push(Container {
            id: mirror.clone(),
            name: mirror,
            running: true,
            labels: HashMap::from([("runnerd.mirror".to_owned(), String::new())]),
        });
    }
    let config = config.replace("mirror = { enabled = false", "mirror = { enabled = true");
    engine_with(github, docker, dir, &config)
}

fn container_names(docker: &FakeDocker) -> BTreeSet<String> {
    docker
        .0
        .lock()
        .unwrap()
        .containers
        .iter()
        .map(|container| container.name.clone())
        .collect()
}

/// The next start on the same history, Docker and GitHub: recovers and ticks once. Returns the runner in each slot.
async fn next_start(
    github: &FakeGithub,
    docker: &FakeDocker,
    dir: &TestDir,
) -> (Vec<Option<String>>, Handle) {
    let config = CONFIG.replace("mirror = { enabled = false", "mirror = { enabled = true");
    let (mut engine, handle) = engine_with(github, docker, dir, &config);
    engine.recover().await.unwrap();
    engine.tick(Shutdown::No).await;
    engine.publish();
    let slots = handle.status.borrow().profiles[0]
        .slots
        .iter()
        .map(|slot| slot.runner.as_ref().map(|runner| runner.name.clone()))
        .collect();
    (slots, handle)
}

#[tokio::test]
async fn a_signal_removes_idle_runners_and_leaves_busy_ones_and_the_mirror_to_the_next_start() {
    // The idle runner may take a job right before the signal, which GitHub knows before runnerd does.
    for took_job in [false, true] {
        let (github, docker, dir) = (
            FakeGithub::default(),
            FakeDocker::default(),
            TestDir::new("stop"),
        );
        let (engine, handle) = busy_and_idle(&github, &docker, &dir);
        let run = tokio::spawn(engine.run());
        until_state(&handle, 1, JobState::Running).await;
        github
            .until_calls(
                |calls| calls.iter().any(|call| call.starts_with("runners ")),
                "asked for its runners",
            )
            .await;
        if took_job {
            github.0.lock().unwrap().busy_deletes = 1;
        }
        let (github_calls, docker_calls) = (github.calls().len(), docker.calls().len());

        handle.control.shutdown(Shutdown::Detach);
        tokio::time::timeout(Duration::from_secs(10), run)
            .await
            .expect("a signal stops at once")
            .unwrap();
        let name = |slot| identity("ci", ID_A).runner_name("app", slot, 1);
        let mut kept = BTreeSet::from([name(1), Names::new("runnerd").mirror()]);
        if took_job {
            kept.insert(name(2));
        }
        assert_eq!(container_names(&docker), kept, "took a job: {took_job}");
        let deleted: Vec<String> = github.calls()[github_calls..]
            .iter()
            .filter(|call| call.starts_with("delete "))
            .cloned()
            .collect();
        assert_eq!(
            deleted,
            ["delete 8"],
            "only the idle runner is deregistered"
        );
        let removed: Vec<String> = docker.calls()[docker_calls..]
            .iter()
            .filter(|call| call.starts_with("remove "))
            .cloned()
            .collect();
        let expected: &[&str] = if took_job {
            &[]
        } else {
            &["remove container-2"]
        };
        assert_eq!(removed, expected, "took a job: {took_job}");

        let (slots, handle) = next_start(&github, &docker, &dir).await;
        assert_eq!(
            slots,
            [Some(name(1)), took_job.then(|| name(2))],
            "the next start adopts what runs (took a job: {took_job})"
        );
        assert_eq!(registered(&github.calls()), Vec::<&String>::new());
        handle.store.flush().await;
        let job = handle.store.job(1).await.unwrap().unwrap();
        assert_eq!(job.summary.state, JobState::Running);
    }
}

#[tokio::test]
async fn a_stop_asked_for_while_github_registers_a_runner_deletes_it_and_starts_no_container() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    {
        let mut state = github.0.lock().unwrap();
        state.strict = true;
        state.queued = vec![job(1)];
    }
    let (mut engine, handle) = engine_in(&github, &docker, &dir);
    engine.recover().await.unwrap();
    github.hold_registrations();
    let stops = async {
        github.until_waiting(1).await;
        handle.control.shutdown(Shutdown::Detach);
        github.release_registrations();
    };
    tokio::join!(engine.tick(Shutdown::No), stops);
    // The runner registered after the stop would take job 1 with no daemon to watch it.
    assert!(
        !docker.calls().iter().any(|call| call.starts_with("start ")),
        "no runner container starts: {:?}",
        docker.calls()
    );
    assert!(
        github.0.lock().unwrap().runners.is_empty(),
        "its registration is deleted: {:?}",
        github.calls()
    );
}

#[tokio::test]
async fn status_says_a_signal_leaves_running_jobs_while_it_removes_idle_runners() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let (engine, handle) = busy_and_idle(&github, &docker, &dir);
    let run = tokio::spawn(engine.run());
    until_state(&handle, 1, JobState::Running).await;
    github
        .until_calls(
            |calls| calls.iter().any(|call| call.starts_with("runners ")),
            "asked for its runners",
        )
        .await;
    // GitHub has not answered the idle runner's deregistration.
    github.hold_deletes();
    handle.control.shutdown(Shutdown::Detach);
    github.until_waiting(1).await;
    assert_eq!(
        handle.status.borrow().stopping,
        Some(Stopping::LeavesJobs),
        "status shows the stop while the idle runner is removed"
    );
    github.release_deletes();
    tokio::time::timeout(Duration::from_secs(10), run)
        .await
        .unwrap()
        .unwrap();
}

#[tokio::test]
async fn a_stop_ends_when_docker_is_gone_or_fails_midway_and_the_next_start_takes_over() {
    for gone in [true, false] {
        let (github, docker, dir) = (
            FakeGithub::default(),
            FakeDocker::default(),
            TestDir::new("stop"),
        );
        let (engine, handle) = busy_and_idle(&github, &docker, &dir);
        if gone {
            // Only job 1's runner, which no removal fails for: the stop must notice the failed inspects.
            docker
                .0
                .lock()
                .unwrap()
                .containers
                .retain(|container| container.id != "container-2");
            github
                .0
                .lock()
                .unwrap()
                .runners
                .retain(|runner| runner.id != 8);
        }
        let run = tokio::spawn(engine.run());
        until_state(&handle, 1, JobState::Running).await;
        github
            .until_calls(
                |calls| calls.iter().any(|call| call.starts_with("runners ")),
                "asked for its runners",
            )
            .await;
        if gone {
            docker.set_outage(Outage::Refused);
        } else {
            // It answers inspects, so the idle runner is deregistered, then goes before its removal.
            docker.0.lock().unwrap().remove_outage = Outage::Refused;
        }

        // `runnerd stop`, which would wait for job 1 if Docker answered.
        handle.control.shutdown(Shutdown::Graceful);
        tokio::time::timeout(Duration::from_secs(10), run)
            .await
            .unwrap_or_else(|_| panic!("the stop never ended (Docker gone: {gone})"))
            .unwrap();
        assert_eq!(
            handle.shutdown(Shutdown::Graceful),
            Some(Stopping::LeavesJobs),
            "a second `runnerd stop` says what the first does once Docker stopped answering (Docker gone: {gone})"
        );
        assert_eq!(
            handle.shutdown(Shutdown::Force),
            handle.status.borrow().stopping,
            "`runnerd stop --force` answers what status says, as Docker cannot kill anything (Docker gone: {gone})"
        );
        assert_eq!(handle.status.borrow().stopping, Some(Stopping::LeavesJobs));
        let calls = github.calls();
        for id in [7, 8] {
            let deletes = calls
                .iter()
                .filter(|call| **call == format!("delete {id}"))
                .count();
            assert!(deletes <= 1, "runner {id} deleted {deletes} times");
        }

        docker.set_outage(Outage::None);
        docker.0.lock().unwrap().remove_outage = Outage::None;
        let (slots, handle) = next_start(&github, &docker, &dir).await;
        assert_eq!(
            slots[0],
            Some(identity("ci", ID_A).runner_name("app", 1, 1)),
            "job 1's runner is adopted (Docker gone: {gone})"
        );
        assert_eq!(registered(&github.calls()), Vec::<&String>::new());
        handle.store.flush().await;
        let job = handle.store.job(1).await.unwrap().unwrap();
        assert_eq!(job.summary.state, JobState::Running);
    }
}

#[tokio::test]
async fn a_forced_stop_docker_is_gone_from_ends_as_a_detach() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let (engine, handle) = busy_and_idle(&github, &docker, &dir);
    let run = tokio::spawn(engine.run());
    until_state(&handle, 1, JobState::Running).await;
    github
        .until_calls(
            |calls| calls.iter().any(|call| call.starts_with("runners ")),
            "asked for its runners",
        )
        .await;
    docker.set_outage(Outage::Refused);
    handle.control.shutdown(Shutdown::Force);
    tokio::time::timeout(Duration::from_secs(10), run)
        .await
        .unwrap_or_else(|_| panic!("a forced stop with Docker gone must end at once"))
        .unwrap();
    assert_eq!(
        handle.status.borrow().stopping,
        Some(Stopping::LeavesJobs),
        "status says what the stop did"
    );
}

#[tokio::test]
async fn docker_gone_with_no_stop_asked_for_stops_nothing() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let (mut engine, handle) = busy_and_idle(&github, &docker, &dir);
    engine.recover().await.unwrap();
    engine.tick(Shutdown::No).await;
    docker.set_outage(Outage::Refused);
    engine.tick(Shutdown::No).await;
    docker.set_outage(Outage::None);
    assert_eq!(
        handle.control.mode(),
        Shutdown::No,
        "an outage with no stop asked for starts no stop"
    );
}

#[tokio::test]
async fn a_less_urgent_stop_does_not_undo_a_more_urgent_one() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let (_engine, handle) = busy_and_idle(&github, &docker, &dir);
    assert_eq!(handle.shutdown(Shutdown::Force), Some(Stopping::KillsJobs));
    assert_eq!(
        handle.shutdown(Shutdown::Graceful),
        Some(Stopping::KillsJobs),
        "a later `runnerd stop` keeps the force"
    );
}

#[tokio::test]
async fn runnerd_stop_tries_again_a_removal_docker_refused_and_leaves_nothing() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let (mut engine, handle) = busy_and_idle(&github, &docker, &dir);
    // Job 1 ended: both runners are idle, and the job's runner exits.
    github
        .0
        .lock()
        .unwrap()
        .runners
        .iter_mut()
        .for_each(|runner| runner.busy = false);
    engine.recover().await.unwrap();
    engine.tick(Shutdown::No).await;
    docker.0.lock().unwrap().containers[0].running = false;
    // Docker answers the first removal with an error, as when it is busy.
    docker.0.lock().unwrap().fail_removes = 1;
    handle.control.shutdown(Shutdown::Graceful);
    tokio::time::timeout(Duration::from_secs(30), engine.schedule())
        .await
        .expect("the stop ends once Docker removes them");
    assert_eq!(
        container_names(&docker),
        BTreeSet::new(),
        "both runners and the mirror are removed"
    );
}

#[tokio::test]
async fn a_stop_ends_within_its_deadline_while_docker_hangs_and_keeps_the_history() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let (engine, handle) = busy_and_idle(&github, &docker, &dir);
    let run = tokio::spawn(engine.run());
    until_state(&handle, 1, JobState::Running).await;
    tokio::time::pause();
    docker.set_outage(Outage::Hung);
    let asked = tokio::time::Instant::now();
    handle.control.shutdown(Shutdown::Force);
    // systemd kills a unit that has not stopped 90s after it asked.
    tokio::time::timeout(Duration::from_secs(89), run)
        .await
        .expect("the stop outlasted systemd's timeout")
        .unwrap();
    assert!(asked.elapsed() >= STOP_DEADLINE, "it gave Docker its time");
    // What the daemon does once the engine returns.
    handle.store.close();
    handle.store.flush().await;
    let reopened = Store::open(&dir.0.join("history.db")).unwrap();
    let job = reopened.job(1).await.unwrap().unwrap();
    assert_eq!(job.summary.state, JobState::Running);
}

#[tokio::test]
async fn a_second_signal_during_a_stop_kills_running_jobs() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let (engine, handle) = busy_and_idle(&github, &docker, &dir);
    let run = tokio::spawn(engine.run());
    until_state(&handle, 1, JobState::Running).await;
    // The signals come while a tick waits for GitHub.
    github.hold();
    handle.control.wake.notify_one();
    github.until_waiting(1).await;
    handle.control.shutdown(Shutdown::Detach);
    handle.control.shutdown(Shutdown::Force);
    github.release();
    tokio::time::timeout(Duration::from_secs(10), run)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(
        container_names(&docker),
        BTreeSet::new(),
        "both runners and the mirror are removed"
    );
    let calls = github.calls();
    assert!(
        calls.contains(&"delete 7".to_owned()),
        "job 1's runner is deregistered: {calls:?}"
    );
}

#[tokio::test]
async fn a_second_signal_while_a_signal_removes_idle_runners_kills_running_jobs() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let (engine, handle) = busy_and_idle(&github, &docker, &dir);
    let run = tokio::spawn(engine.run());
    until_state(&handle, 1, JobState::Running).await;
    github
        .until_calls(
            |calls| calls.iter().any(|call| call.starts_with("runners ")),
            "asked for its runners",
        )
        .await;
    // The second signal comes while GitHub has not answered the idle runner's deregistration.
    github.hold_deletes();
    handle.control.shutdown(Shutdown::Detach);
    github.until_waiting(1).await;
    handle.control.shutdown(Shutdown::Force);
    github.release_deletes();
    tokio::time::timeout(Duration::from_secs(10), run)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(
        container_names(&docker),
        BTreeSet::new(),
        "job 1's runner and the mirror are removed too"
    );
}

#[tokio::test]
async fn a_stop_leaves_a_reseed_and_a_check_of_the_caches_to_the_next_start() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let seed = Names::new("runnerd").seed("app");
    {
        let mut state = docker.0.lock().unwrap();
        // The seed container runs until the test ends, and measuring the volumes never finishes.
        state.containers.push(Container {
            id: "seed".to_owned(),
            name: seed.clone(),
            running: true,
            labels: HashMap::from([
                ("runnerd.seed".to_owned(), String::new()),
                ("runnerd.profile".to_owned(), "app".to_owned()),
            ]),
        });
        state.volume_sizes_held.send_replace(true);
    }
    let (mut engine, handle) = engine_in(&github, &docker, &dir);
    engine.recover().await.unwrap();
    engine.tick(Shutdown::No).await;
    assert!(engine.trim.is_some(), "the caches are being checked");
    assert!(!engine.no_reseeds(), "app is reseeding");

    handle.control.shutdown(Shutdown::Graceful);
    tokio::time::timeout(Duration::from_secs(10), engine.schedule())
        .await
        .expect("the stop waits for neither");
    assert!(container_names(&docker).contains(&seed), "the seed stays");

    let (mut engine, handle) = engine_in(&github, &docker, &dir);
    engine.recover().await.unwrap();
    engine.tick(Shutdown::No).await;
    engine.publish();
    assert!(
        handle.status.borrow().profiles[0].reseeding,
        "the next start waits for the reseed"
    );
    assert!(engine.trim.is_some(), "the next start checks the caches");
}

#[tokio::test]
async fn a_reload_asked_for_while_a_signal_stops_the_daemon_is_answered_as_it_went() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let (engine, handle) = engine_in(&github, &docker, &dir);
    let run = tokio::spawn(engine.run());
    github
        .until_calls(
            |calls| calls.iter().any(|call| call.starts_with("runners ")),
            "asked for its runners",
        )
        .await;
    github.hold();
    handle.control.wake.notify_one();
    github.until_waiting(1).await;
    handle.control.shutdown(Shutdown::Detach);
    let written = Arc::new(AtomicBool::new(false));
    let commit = {
        let written = written.clone();
        Commit::new(move || {
            written.store(true, Ordering::SeqCst);
            Ok(None)
        })
    };
    let control = handle.control.clone();
    let reload = tokio::spawn(async move {
        control
            .replace_config(
                Config::parse(&CONFIG.replace("slots = 2", "slots = 1")).unwrap(),
                None,
                None,
                commit,
                RunningSlots::Drain,
                Author::Owner,
            )
            .await
    });
    github.release();
    tokio::time::timeout(Duration::from_secs(10), run)
        .await
        .unwrap()
        .unwrap();
    let answer = tokio::time::timeout(Duration::from_secs(10), reload)
        .await
        .expect("the reload is answered")
        .unwrap();
    match answer {
        Ok(_) => assert!(written.load(Ordering::SeqCst), "applied, so written"),
        Err(ControlError::Stopped) => {
            assert!(!written.load(Ordering::SeqCst), "refused, so not written");
        }
        Err(other) => panic!("the reload failed: {other:?}"),
    }
}

#[tokio::test]
async fn the_first_tick_after_an_install_replaces_idle_runners_of_the_old_release() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let (mut engine, _handle) = busy_and_idle(&github, &docker, &dir);
    // Both runners were started by an earlier release, whose container spec may differ.
    for container in &mut docker.0.lock().unwrap().containers {
        container
            .labels
            .insert(super::super::runners::SPEC.to_owned(), "0.0.1".to_owned());
    }
    engine.recover().await.unwrap();
    engine.tick(Shutdown::No).await;
    let calls = docker.calls();
    assert!(
        calls.contains(&"remove container-2".to_owned()),
        "the idle runner goes: {calls:?}"
    );
    assert!(
        !calls.contains(&"remove container-1".to_owned()),
        "job 1's runner keeps running: {calls:?}"
    );
}

/// Job 1 runs in slot 1 and job 2 in slot 2, on a profile that keeps failed jobs' runners.
fn two_jobs_recording(
    github: &FakeGithub,
    docker: &FakeDocker,
    dir: &TestDir,
) -> (Engine<FakeGithub, FakeDocker>, Handle) {
    running_runner(github, docker, "@@job 1 1\n");
    let second = identity("ci", ID_A).runner_name("app", 2, 1);
    {
        let mut state = github.0.lock().unwrap();
        state.jobs.insert(1, details(1, "in_progress", None));
        state.jobs.insert(2, details(2, "in_progress", None));
        state.runners.push(runner(8, &second, true));
    }
    {
        let mut state = docker.0.lock().unwrap();
        state.containers.push(runner_container(2, 8, true));
        state
            .container_captures
            .insert("container-2".to_owned(), "@@job 2 2\n".to_owned());
    }
    engine_with(github, docker, dir, &records::recording())
}

/// Waits until the fake Docker was asked for `call`.
async fn until_docker_call(docker: &FakeDocker, call: &str) {
    for _ in 0..500 {
        if docker.calls().iter().any(|made| made == call) {
            return;
        }
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    panic!("Docker was never asked to {call}: {:?}", docker.calls());
}

#[test]
fn runnerd_stop_that_docker_turns_into_a_detach_ends_within_the_deadline_and_the_next_start_keeps_the_record()
 {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    // The daemon's own runtime, gone with the process once `run` returns: so is the commit it left waiting.
    let daemon = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    daemon.block_on(async {
        let (engine, handle) = two_jobs_recording(&github, &docker, &dir);
        let run = tokio::spawn(engine.run());
        until_state(&handle, 2, JobState::Running).await;
        // Job 2 fails, and its record's commit waits for Docker.
        {
            let mut state = docker.0.lock().unwrap();
            state.commits_held.send_replace(true);
            state
                .container_logs
                .insert("container-2".to_owned(), records::FAILED.to_owned());
            state.containers[1].running = false;
        }
        github
            .0
            .lock()
            .unwrap()
            .jobs
            .insert(2, details(2, "completed", Some("failure")));
        handle.control.wake.notify_one();
        until_docker_call(&docker, "commit container-2 runnerd-record:2").await;

        // `runnerd stop`, while Docker no longer answers the inspect of job 1's runner.
        tokio::time::pause();
        docker.set_outage(Outage::Refused);
        let asked = tokio::time::Instant::now();
        handle.control.shutdown(Shutdown::Graceful);
        tokio::time::timeout(STOP_DEADLINE + Duration::from_secs(10), run)
            .await
            .expect("the detach Docker turned the stop into has the deadline of any detach")
            .unwrap();
        assert!(
            asked.elapsed() >= STOP_DEADLINE,
            "it gave the record its time"
        );
        handle.store.close();
        handle.store.flush().await;
    });
    drop(daemon);

    let next = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    next.block_on(async {
        {
            let mut state = docker.0.lock().unwrap();
            state.outage = Outage::None;
            state.commits_held.send_replace(false);
        }
        let (mut engine, handle) = engine_with(&github, &docker, &dir, &records::recording());
        engine.recover().await.unwrap();
        records::until_recorded(&mut engine).await;
        let note = records::note(&handle, 2).await;
        assert!(
            note.is_some_and(|note| note.created_at.is_some()),
            "job 2's runner becomes its record at the next start"
        );
        engine.publish();
        let runner = handle.status.borrow().profiles[0].slots[0]
            .runner
            .as_ref()
            .map(|runner| runner.name.clone());
        assert_eq!(
            runner,
            Some(identity("ci", ID_A).runner_name("app", 1, 1)),
            "job 1's runner is adopted"
        );
    });
}

#[tokio::test]
async fn a_failed_job_an_idle_runner_took_while_detached_becomes_its_record_at_the_next_start() {
    let (github, docker, dir) = (
        FakeGithub::default(),
        FakeDocker::default(),
        TestDir::new("stop"),
    );
    let config = records::recording();
    let (engine, handle) = busy_and_idle_with(&github, &docker, &dir, &config);
    let run = tokio::spawn(engine.run());
    until_state(&handle, 1, JobState::Running).await;
    github
        .until_calls(
            |calls| calls.iter().any(|call| call.starts_with("runners ")),
            "asked for its runners",
        )
        .await;
    // The idle runner takes job 5 right before the signal, so GitHub keeps it, and runnerd never sees the job.
    github.0.lock().unwrap().busy_deletes = 1;
    handle.control.shutdown(Shutdown::Detach);
    tokio::time::timeout(Duration::from_secs(10), run)
        .await
        .unwrap()
        .unwrap();
    drop(handle);

    // Job 5 fails before the next start, and its runner exits; only its worker log names the job.
    github
        .0
        .lock()
        .unwrap()
        .runners
        .retain(|runner| runner.id != 8);
    {
        let mut state = docker.0.lock().unwrap();
        let idle = state
            .containers
            .iter_mut()
            .find(|container| container.id == "container-2")
            .unwrap();
        idle.running = false;
        state
            .container_logs
            .insert("container-2".to_owned(), records::FAILED.to_owned());
        state
            .diag
            .insert("container-2".to_owned(), docker::tests::diag_archive(5));
        // Docker is too busy to read it at first, then the read breaks off right after the archive's first entry.
        state.fail_files = 1;
        state.cut_files.push_back(512);
    }
    tokio::time::pause();
    let (mut engine, handle) = engine_with(&github, &docker, &dir, &config);
    engine.recover().await.unwrap();
    let left = identity("ci", ID_A).runner_name("app", 2, 1);
    assert!(
        container_names(&docker).contains(&left),
        "the runner Docker could not read yet stays"
    );
    // Each try comes after a backoff, which is at most 10 minutes.
    let backoff = Duration::from_secs(600);
    tokio::time::sleep(backoff).await;
    engine.tick(Shutdown::No).await;
    assert!(
        container_names(&docker).contains(&left),
        "the runner whose worker log was not read stays"
    );
    tokio::time::sleep(backoff).await;
    records::until_recorded(&mut engine).await;
    let kept: Vec<u64> = handle
        .records
        .borrow()
        .records
        .iter()
        .map(|record| record.job)
        .collect();
    assert_eq!(kept, [5], "job 5's runner becomes its record");
    let calls = docker.calls();
    let commit = calls
        .iter()
        .position(|call| call == "commit container-2 runnerd-record:5");
    let remove = calls.iter().position(|call| call == "remove container-2");
    assert!(
        commit.is_some() && commit < remove,
        "the stopped runner is committed, then goes: {calls:?}"
    );
}

#[tokio::test]
async fn a_graceful_stop_removes_only_a_runner_listed_idle_with_no_job() {
    let (github, docker) = (FakeGithub::default(), FakeDocker::default());
    let name = |slot| identity("ci", ID_A).runner_name("app", slot, 1);
    // Listed busy with its job named; and listed idle with its job named, as a capture can name it first.
    running_job(&github, &docker, "o/r", 1, 1);
    running_job(&github, &docker, "o/r", 2, 2);
    {
        let (mut containers, mut listed) = (docker.0.lock().unwrap(), github.0.lock().unwrap());
        // Listed busy, before any capture named its job.
        containers.containers.push(runner_container(3, 9, true));
        listed.runners.push(runner(9, &name(3), true));
        // Idle.
        containers.containers.push(runner_container(4, 10, true));
        listed.runners.push(runner(10, &name(4), false));
    }
    let dir = TestDir::new("stop-idle");
    let (mut engine, handle) = engine_with(
        &github,
        &docker,
        &dir,
        &CONFIG.replace("slots = 2", "slots = 4"),
    );
    engine.recover().await.unwrap();
    until_state(&handle, 1, JobState::Running).await;
    until_state(&handle, 2, JobState::Running).await;
    // A graceful stop goes by the last listing, which it does not repeat.
    for listed in &mut github.0.lock().unwrap().runners {
        listed.busy = listed.busy && listed.name != name(2);
    }
    engine.tick(Shutdown::No).await;

    engine.tick(Shutdown::Graceful).await;
    assert_eq!(
        removed(&docker.calls()),
        ["remove container-4"],
        "only the runner with no job, listed idle, goes"
    );
}