snare 3.3.0

Ergonomic in-process network testing on top of snare-interpose: write the code under test against std, describe the peer with a tester builder
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
//! Deterministic scheduling (`Sim::builder().deterministic()`): one thread runs at a time and
//! control passes only where a thread waits, always to the next thread in a fixed order — so every
//! interleaving below comes out the same on every run, however the OS would have scheduled it.

use std::collections::hash_map::RandomState;
use std::hash::BuildHasher;
use std::io::{BufRead, BufReader, Write};
use std::net::{TcpStream, UdpSocket};
use std::sync::{Arc, Condvar, Mutex, mpsc};
use std::time::{Duration, Instant};

use snare::{Line, Sim, TesterAction, connect_tester, run_testers};

fn dsim(seed: u64) -> Sim {
    Sim::builder().deterministic().seed(seed).build()
}

/// Runs `scenario` twice on fresh deterministic sims and checks both runs saw the same thing.
fn replays<T: PartialEq + std::fmt::Debug>(scenario: impl Fn() -> T) -> T {
    let first = dsim(1).run(&scenario);
    let second = dsim(1).run(&scenario);
    assert_eq!(first, second, "the same seed must replay the same run");
    first
}

#[test]
fn yields_interleave_threads_in_a_fixed_order() {
    let log = replays(|| {
        let log = Arc::new(Mutex::new(Vec::new()));
        let workers: Vec<_> = (0..4)
            .map(|w| {
                let log = log.clone();
                std::thread::spawn(move || {
                    for step in 0..5 {
                        log.lock().unwrap().push((w, step));
                        std::thread::yield_now();
                    }
                })
            })
            .collect();
        for worker in workers {
            worker.join().unwrap();
        }
        Arc::try_unwrap(log).unwrap().into_inner().unwrap()
    });
    assert_eq!(log.len(), 20);
    let first_five: Vec<_> = log.iter().take(5).map(|(w, _)| *w).collect();
    assert!(
        first_five.windows(2).any(|w| w[0] != w[1]),
        "yields hand the baton around rather than running each thread to completion: {log:?}"
    );
}

#[test]
fn mutex_contention_resolves_the_same_way_every_run() {
    let order = replays(|| {
        let lock = Arc::new(Mutex::new(Vec::new()));
        let workers: Vec<_> = (0..5)
            .map(|w| {
                let lock = lock.clone();
                std::thread::spawn(move || {
                    for _ in 0..3 {
                        let mut held = lock.lock().unwrap();
                        held.push(w);
                        // Hold the lock across a wait, so the others pile up behind it.
                        std::thread::sleep(Duration::from_millis(1));
                    }
                })
            })
            .collect();
        for worker in workers {
            worker.join().unwrap();
        }
        Arc::try_unwrap(lock).unwrap().into_inner().unwrap()
    });
    assert_eq!(order.len(), 15);
}

#[test]
fn channel_messages_from_many_producers_arrive_in_a_fixed_order() {
    let received = replays(|| {
        let (tx, rx) = mpsc::channel();
        let producers: Vec<_> = (0..4)
            .map(|p| {
                let tx = tx.clone();
                std::thread::spawn(move || {
                    for n in 0..5 {
                        tx.send((p, n)).unwrap();
                        std::thread::sleep(Duration::from_micros(500 * (p + 1)));
                    }
                })
            })
            .collect();
        drop(tx);
        let received: Vec<_> = rx.iter().collect();
        for producer in producers {
            producer.join().unwrap();
        }
        received
    });
    assert_eq!(received.len(), 20);
}

#[test]
fn a_condvar_ping_pong_completes_and_replays() {
    let trace = replays(|| {
        let state = Arc::new((Mutex::new((0u32, Vec::new())), Condvar::new()));
        let other = state.clone();
        let pong = std::thread::spawn(move || {
            let (lock, cv) = &*other;
            for _ in 0..5 {
                let mut s = lock.lock().unwrap();
                while s.0 % 2 == 0 {
                    s = cv.wait(s).unwrap();
                }
                s.0 += 1;
                s.1.push("pong");
                cv.notify_one();
            }
        });
        let (lock, cv) = &*state;
        for _ in 0..5 {
            let mut s = lock.lock().unwrap();
            while s.0 % 2 == 1 {
                s = cv.wait(s).unwrap();
            }
            s.0 += 1;
            s.1.push("ping");
            cv.notify_one();
        }
        pong.join().unwrap();
        let s = lock.lock().unwrap();
        s.1.clone()
    });
    assert_eq!(trace, ["ping", "pong"].repeat(5));
}

#[test]
fn sleepers_wake_in_time_order_with_ties_in_a_fixed_order() {
    let wakes = replays(|| {
        let log = Arc::new(Mutex::new(Vec::new()));
        let sleepers: Vec<_> = [30u64, 10, 20, 10, 30]
            .into_iter()
            .enumerate()
            .map(|(i, ms)| {
                let log = log.clone();
                std::thread::spawn(move || {
                    std::thread::sleep(Duration::from_millis(ms));
                    log.lock().unwrap().push((ms, i));
                })
            })
            .collect();
        for sleeper in sleepers {
            sleeper.join().unwrap();
        }
        Arc::try_unwrap(log).unwrap().into_inner().unwrap()
    });
    let times: Vec<u64> = wakes.iter().map(|(ms, _)| *ms).collect();
    assert_eq!(times, [10, 10, 20, 30, 30], "woken in virtual-time order");
}

#[test]
fn scoped_threads_and_joins_work() {
    let total = replays(|| {
        let mut parts = [0u64; 4];
        std::thread::scope(|scope| {
            for (i, part) in parts.iter_mut().enumerate() {
                scope.spawn(move || {
                    std::thread::sleep(Duration::from_millis(i as u64));
                    *part = (i as u64 + 1) * 10;
                });
            }
        });
        parts.iter().sum::<u64>()
    });
    assert_eq!(total, 100);
}

#[test]
fn randomness_across_threads_replays() {
    let hashes = replays(|| {
        let workers: Vec<_> = (0..3)
            .map(|_| std::thread::spawn(|| RandomState::new().hash_one(7u64)))
            .collect();
        workers
            .into_iter()
            .map(|w| w.join().unwrap())
            .collect::<Vec<_>>()
    });
    assert_eq!(hashes.len(), 3);
}

/// std draws a thread's hash keys at its first `RandomState` and keeps them, so building the sim
/// must not draw them on the thread that then runs it: the code under test's `HashMap` order there
/// comes from the seed, as on a thread the sim was not built on.
#[test]
fn hash_order_on_the_thread_that_built_the_sim_comes_from_the_seed() {
    let order = || {
        let map: std::collections::HashMap<u32, ()> = (0..32).map(|i| (i, ())).collect();
        map.into_keys().collect::<Vec<_>>()
    };
    let built_elsewhere = dsim(1);
    let seeded = std::thread::spawn(move || built_elsewhere.run(order))
        .join()
        .unwrap();
    let built_here = std::thread::spawn(move || dsim(1).run(order))
        .join()
        .unwrap();
    assert_eq!(built_here, seeded);
}

/// A lock in static data, held most of the time by a thread outside every sim.
#[cfg(target_os = "macos")]
static HELD_OUTSIDE: Mutex<()> = Mutex::new(());

/// Scheduled threads take a lock a thread outside the sim keeps busy, while the threads they spawn
/// start and exit beside the baton: each contended take waits for the outside holder in real time
/// without letting another scheduled thread run, so the order replays however long those waits
/// and the children's startups take. macOS only: there a pthread mutex names its owner, while a
/// Linux futex word or Windows address word does not and gets only a short grace for an outside
/// holder (OPEN_BUGS.md, "Locks held outside the sim").
#[cfg(target_os = "macos")]
#[test]
fn a_lock_held_outside_the_sim_does_not_reorder_threads_starting_beside_it() {
    let stop = Arc::new(std::sync::atomic::AtomicBool::new(false));
    let holder = {
        let stop = stop.clone();
        std::thread::spawn(move || {
            let spin = |span| {
                let until = Instant::now() + span;
                while Instant::now() < until {}
            };
            while !stop.load(std::sync::atomic::Ordering::Relaxed) {
                let held = HELD_OUTSIDE.lock().unwrap();
                spin(Duration::from_micros(50));
                drop(held);
                // macOS mutexes are not fair: a holder that relocks at once starves the waiters.
                std::thread::sleep(Duration::from_micros(50));
            }
        })
    };
    let scenario = || {
        let log = Arc::new(Mutex::new(Vec::new()));
        let workers: Vec<_> = (0..8)
            .map(|w| {
                let log = log.clone();
                std::thread::spawn(move || {
                    for step in 0..20 {
                        let children: Vec<_> = (0..4)
                            .map(|_| {
                                let child = std::thread::spawn(|| {});
                                drop(HELD_OUTSIDE.lock().unwrap());
                                child
                            })
                            .collect();
                        log.lock().unwrap().push((w, step));
                        for child in children {
                            child.join().unwrap();
                        }
                    }
                })
            })
            .collect();
        for worker in workers {
            worker.join().unwrap();
        }
        Arc::try_unwrap(log).unwrap().into_inner().unwrap()
    };
    let first = dsim(1).run(scenario);
    for _ in 0..30 {
        assert_eq!(
            dsim(1).run(scenario),
            first,
            "the same seed must replay the same run"
        );
    }
    stop.store(true, std::sync::atomic::Ordering::Relaxed);
    holder.join().unwrap();
    assert_eq!(first.len(), 160);
}

#[test]
fn testers_run_under_the_schedule() {
    let line = replays(|| {
        let server = connect_tester::<Line>("127.0.0.8:9500")
            .then_action(|msg, _| TesterAction::Send(Line(format!("echo:{}", msg.0))))
            .until_after(Duration::from_millis(100));
        let client = std::thread::spawn(|| {
            let stream = TcpStream::connect("127.0.0.8:9500").unwrap();
            (&stream).write_all(b"hi\n").unwrap();
            let mut line = String::new();
            BufReader::new(&stream).read_line(&mut line).unwrap();
            line
        });
        run_testers!(server);
        client.join().unwrap()
    });
    assert_eq!(line, "echo:hi\n");
}

#[test]
fn a_deadlocked_receive_still_gives_up() {
    dsim(0).run(|| {
        let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
        let mut buf = [0u8; 4];
        assert!(sock.recv_from(&mut buf).is_err());
    });
}

#[test]
fn timeouts_run_on_virtual_time() {
    let real = snare::real(Instant::now);
    dsim(0).run(|| {
        let (_tx, rx) = mpsc::channel::<u8>();
        let start = Instant::now();
        assert_eq!(
            rx.recv_timeout(Duration::from_secs(30)),
            Err(mpsc::RecvTimeoutError::Timeout)
        );
        assert!(start.elapsed() >= Duration::from_secs(30));
    });
    assert!(snare::real(|| real.elapsed()) < Duration::from_secs(30));
}

#[cfg(target_os = "linux")]
#[test]
fn a_simhost_run_replays_under_the_schedule() {
    let run = || {
        let host = snare::HostProfile::new().build();
        Sim::builder()
            .host(host)
            .deterministic()
            .seed(3)
            .build()
            .run(|| {
                let rx = UdpSocket::bind("127.0.0.1:9600").unwrap();
                let senders: Vec<_> = (0..3u8)
                    .map(|s| {
                        std::thread::spawn(move || {
                            let tx = UdpSocket::bind(("127.0.0.1", 9610 + u16::from(s))).unwrap();
                            for n in 0..4u8 {
                                tx.send_to(&[s, n], "127.0.0.1:9600").unwrap();
                                std::thread::sleep(Duration::from_micros(300 * (u64::from(s) + 1)));
                            }
                        })
                    })
                    .collect();
                let start = Instant::now();
                let mut got = Vec::new();
                let mut buf = [0u8; 2];
                for _ in 0..12 {
                    rx.recv_from(&mut buf).unwrap();
                    got.push((buf, start.elapsed()));
                }
                for sender in senders {
                    sender.join().unwrap();
                }
                got
            })
    };
    let first = run();
    assert_eq!(first.len(), 12);
    assert_eq!(first, run(), "the same seed must replay the same run");
}

#[test]
#[should_panic(expected = "deterministic Sim cannot run its clock at a real-time rate")]
fn finite_rate_rejected() {
    let _ = Sim::builder().deterministic().time_rate(2.0).build();
}

#[test]
#[should_panic(expected = "deterministic Sim cannot run its clock at a real-time rate")]
fn finite_rate_rejected_after_build() {
    dsim(0).set_time_rate(1.0);
}

#[test]
fn clock_controls_work_under_the_schedule() {
    let sim = dsim(0);
    sim.pause_time();
    sim.advance_time(Duration::from_secs(1));
    sim.set_time_value(Duration::from_secs(2));
    sim.set_time_rate(f64::INFINITY);
    assert_eq!(sim.time_rate(), f64::INFINITY);
    assert_eq!(sim.time_value(), Duration::from_secs(2));
}

#[test]
fn paused_det_parks_and_unmanaged_advance_releases_in_lineage_order() {
    let run = || {
        let sim = dsim(5);
        sim.pause_time();
        let time = sim.time();
        sim.run(|| {
            let real = snare::real(Instant::now);
            let started = Arc::new(std::sync::atomic::AtomicUsize::new(0));
            let controller = snare::real(|| {
                let started = started.clone();
                std::thread::spawn(move || {
                    // A sleeper's thread can take longer than the 100 ms below to start on a
                    // loaded machine; one that read the clock only after the advance would sleep
                    // past it.
                    while started.load(std::sync::atomic::Ordering::Acquire) < 4 {
                        std::thread::sleep(Duration::from_millis(1));
                    }
                    std::thread::sleep(Duration::from_millis(100));
                    time.advance(Duration::from_secs(1));
                })
            });
            let log = Arc::new(Mutex::new(Vec::new()));
            let sleepers: Vec<_> = (0..4u64)
                .map(|i| {
                    let log = log.clone();
                    let started = started.clone();
                    std::thread::spawn(move || {
                        let start = Instant::now();
                        started.fetch_add(1, std::sync::atomic::Ordering::Release);
                        std::thread::sleep(Duration::from_millis(10 * (4 - i)));
                        log.lock().unwrap().push((i, start.elapsed()));
                    })
                })
                .collect();
            for sleeper in sleepers {
                sleeper.join().unwrap();
            }
            snare::real(|| controller.join().unwrap());
            assert!(
                snare::real(|| real.elapsed()) >= Duration::from_millis(80),
                "the sleepers parked until the outside advance"
            );
            Arc::try_unwrap(log).unwrap().into_inner().unwrap()
        })
    };
    let first = run();
    assert_eq!(first.len(), 4);
    assert!(
        first
            .iter()
            .all(|(_, slept)| *slept == Duration::from_secs(1)),
        "{first:?}"
    );
    assert_eq!(first, run(), "released in the same order on every run");
}

#[test]
fn exiting_thread_releases_a_native_join_with_a_paused_sibling() {
    let sim = dsim(7);
    sim.pause_time();
    sim.run(|| {
        snare::sched::mark_driver_thread();
        let exec = snare::sched::attach(Default::default()).unwrap();
        let receiver = UdpSocket::bind("127.0.0.1:0").unwrap();
        let address = receiver.local_addr().unwrap();
        let idle = std::thread::spawn(move || {
            receiver.recv_from(&mut [0; 4]).unwrap();
        });
        let sleeper = std::thread::spawn(|| std::thread::sleep(Duration::from_secs(10)));
        let start = snare::real(Instant::now);
        loop {
            let state = exec.quiescence();
            if state.quiescent && state.blocked == 2 {
                break;
            }
            assert!(snare::real(|| start.elapsed()) < Duration::from_secs(2));
            snare::real(std::thread::yield_now);
        }
        let (joined, completed) = mpsc::channel();
        let observer = snare::real(|| {
            std::thread::spawn(move || {
                idle.join().unwrap();
                joined.send(()).unwrap();
            })
        });
        UdpSocket::bind("127.0.0.1:0")
            .unwrap()
            .send_to(b"stop", address)
            .unwrap();
        let joined_before_advance =
            snare::real(|| completed.recv_timeout(Duration::from_secs(1))).is_ok();
        exec.enter_timestamp(Duration::from_secs(10));
        exec.leave_timestamp(Duration::from_secs(10));
        sleeper.join().unwrap();
        snare::real(|| observer.join().unwrap());
        assert!(
            joined_before_advance,
            "native join waited for a sibling's clock"
        );
    });
}

#[test]
fn set_value_under_det_from_managed_thread() {
    let run = || {
        let sim = dsim(2);
        sim.pause_time();
        sim.run(|| {
            let log = Arc::new(Mutex::new(Vec::new()));
            let theirs = log.clone();
            let sleeper = std::thread::spawn(move || {
                let start = Instant::now();
                std::thread::sleep(Duration::from_secs(5));
                theirs.lock().unwrap().push(("woke", start.elapsed()));
            });
            std::thread::yield_now();
            let time = snare::time();
            log.lock().unwrap().push(("set", time.value()));
            time.set_value(time.value() + Duration::from_secs(5));
            sleeper.join().unwrap();
            Arc::try_unwrap(log).unwrap().into_inner().unwrap()
        })
    };
    let first = run();
    assert_eq!(
        first,
        [("set", Duration::ZERO), ("woke", Duration::from_secs(5))]
    );
    assert_eq!(first, run());
}

/// Three TCP clients and two UDP senders on a lossy, jittery, duplicating link, all racing one
/// another; returns the order the threads did their work in.
fn busy_edge() -> Vec<String> {
    let trace = Arc::new(Mutex::new(Vec::new()));
    snare::set_udp_policy("127.0.0.8:9611", |p| {
        p.loss_rate = 0.3;
        p.duplicate_rate = 0.3;
        p.jitter = Duration::from_millis(5);
    });
    let server = connect_tester::<Line>("127.0.0.8:9610")
        .then_test(|msg, _| (msg.0 != "skip").then_some(msg))
        .then_action(|msg, _| TesterAction::Send(Line(format!("echo:{}", msg.0))))
        .until_after(Duration::from_millis(100));
    let device = snare::udp_tester::<snare::Bytes>("127.0.0.8:9611")
        .then_action(|msg, _| TesterAction::Send(msg))
        .until_after(Duration::from_millis(100));
    let tcp = (0..3).map(|c| {
        let trace = trace.clone();
        std::thread::spawn(move || {
            let stream = TcpStream::connect("127.0.0.8:9610").unwrap();
            let mut reader = BufReader::new(&stream);
            for n in 0..3 {
                (&stream)
                    .write_all(format!("skip\n{c}.{n}\n").as_bytes())
                    .unwrap();
                let mut line = String::new();
                reader.read_line(&mut line).unwrap();
                trace.lock().unwrap().push(line);
            }
        })
    });
    let udp = (0..2).map(|c| {
        let trace = trace.clone();
        std::thread::spawn(move || {
            let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
            for n in 0..10u8 {
                sock.send_to(&[c, n], "127.0.0.8:9611").unwrap();
                trace.lock().unwrap().push(format!("udp {c}.{n}"));
                std::thread::sleep(Duration::from_millis(1));
            }
        })
    });
    let workers: Vec<_> = tcp.chain(udp).collect();
    run_testers!(server, device);
    for worker in workers {
        worker.join().unwrap();
    }
    Arc::try_unwrap(trace).unwrap().into_inner().unwrap()
}

#[test]
fn the_event_log_replays_under_the_schedule() {
    let log = replays(|| {
        busy_edge();
        snare::recorded_events()
    });
    assert!(
        log.iter()
            .any(|e| matches!(e.event, snare::RecordedEvent::Link { .. })),
        "the lossy link logged faults: {log:?}"
    );
    assert!(
        log.windows(2)
            .all(|w| w[0].seq < w[1].seq && w[0].at <= w[1].at)
    );
}

#[test]
fn recording_does_not_perturb_the_schedule() {
    let run = |record| {
        let sim = Sim::builder()
            .deterministic()
            .seed(7)
            .record_events(record)
            .build();
        let trace = sim.run(busy_edge);
        (trace, sim.recorded_events().len())
    };
    let (recorded, logged) = run(true);
    let (unrecorded, nothing) = run(false);
    assert!(logged > 0);
    assert_eq!(nothing, 0);
    assert_eq!(recorded, unrecorded, "recording must not change the run");
}

fn flapping_link() -> Sim {
    let policy = snare::NicPolicy {
        latency: Duration::from_millis(2),
        jitter: Duration::from_millis(3),
        loss_rate: 0.2,
        duplicate_rate: 0.1,
    };
    Sim::builder()
        .deterministic()
        .seed(3)
        .nic(
            snare::NicSpec::new("eth0")
                .index(4)
                .address("10.0.0.1/24".parse::<snare::IpNet>().unwrap())
                .station("10.0.0.2".parse::<std::net::IpAddr>().unwrap())
                .policy(policy),
        )
        .build()
}

fn flap_run() -> (Vec<(u8, Duration)>, Vec<snare::RecordedEvent>) {
    let sim = flapping_link();
    let got = sim.run(|| {
        let rx = UdpSocket::bind("10.0.0.2:7000").unwrap();
        rx.set_read_timeout(Some(Duration::from_millis(50)))
            .unwrap();
        let start = Instant::now();
        let sender = std::thread::spawn(|| {
            let tx = UdpSocket::bind("10.0.0.1:0").unwrap();
            for i in 0..30u8 {
                let sent = tx.send_to(&[i], "10.0.0.2:7000");
                // Windows media sense withdraws the interface's routes with its carrier.
                if !cfg!(windows) {
                    sent.unwrap();
                }
                std::thread::sleep(Duration::from_millis(1));
            }
        });
        let flapper = std::thread::spawn(|| {
            for _ in 0..3 {
                std::thread::sleep(Duration::from_millis(5));
                snare::set_link("eth0", false).unwrap();
                std::thread::sleep(Duration::from_millis(2));
                snare::set_link("eth0", true).unwrap();
            }
            snare::schedule_link("eth0", Duration::from_millis(3), false).unwrap();
            snare::schedule_link("eth0", Duration::from_millis(6), true).unwrap();
        });
        let mut got = Vec::new();
        let mut buf = [0u8; 4];
        while let Ok((n, _)) = rx.recv_from(&mut buf) {
            assert_eq!(n, 1);
            got.push((buf[0], start.elapsed()));
        }
        sender.join().unwrap();
        flapper.join().unwrap();
        got
    });
    let events = sim.recorded_events().into_iter().map(|e| e.event).collect();
    (got, events)
}

#[test]
fn link_flap_replays() {
    let (got, events) = flap_run();
    assert_eq!((got.clone(), events.clone()), flap_run());
    assert!(got.len() > 5 && got.len() < 30, "{got:?}");
    let flaps = events
        .iter()
        .filter(|e| matches!(e, snare::RecordedEvent::NicChanged { carrier: false, .. }))
        .count();
    assert_eq!(flaps, 4, "{events:?}");
}

#[test]
fn scheduled_link_up_time_skips_under_baton() {
    let real = Instant::now();
    let waited = replays(|| {
        let sim_nic = snare::NicSpec::new("eth9")
            .address("10.9.0.1/24".parse::<snare::IpNet>().unwrap())
            .station("10.9.0.2".parse::<std::net::IpAddr>().unwrap());
        snare::add_nic(sim_nic).unwrap();
        let listener = std::net::TcpListener::bind("10.9.0.2:9500").unwrap();
        let mut client = TcpStream::connect("10.9.0.2:9500").unwrap();
        let (mut server, _) = listener.accept().unwrap();
        snare::set_link("eth9", false).unwrap();
        client.write_all(b"later").unwrap();
        snare::schedule_link("eth9", Duration::from_secs(10), true).unwrap();
        let t0 = Instant::now();
        let mut buf = [0u8; 8];
        let n = std::io::Read::read(&mut server, &mut buf).unwrap();
        assert_eq!(&buf[..n], b"later");
        t0.elapsed()
    });
    assert!(waited >= Duration::from_secs(10), "{waited:?}");
    assert!(
        real.elapsed() < Duration::from_secs(2),
        "{:?}",
        real.elapsed()
    );
}

#[test]
fn det_connect_timeout_replays() {
    let log = replays(|| {
        let delayed: std::net::SocketAddr = "127.0.0.7:7400".parse().unwrap();
        let listener = std::net::TcpListener::bind(delayed).unwrap();
        let until = Instant::now() + Duration::from_millis(1500);
        snare::set_listener_behavior(delayed, snare::ListenerBehavior::DelayingUntil(until));
        let start = Instant::now();
        let log = Arc::new(Mutex::new(Vec::new()));
        let workers: Vec<_> = [
            (0, "10.255.0.1:80", 5000),
            (1, "127.0.0.7:7400", 5000),
            (2, "10.255.0.2:80", 2500),
        ]
        .into_iter()
        .map(|(w, dest, ms)| {
            let log = log.clone();
            std::thread::spawn(move || {
                let dest = dest.parse().unwrap();
                let result = TcpStream::connect_timeout(&dest, Duration::from_millis(ms))
                    .map(drop)
                    .map_err(|e| e.kind());
                log.lock().unwrap().push((w, result, start.elapsed()));
            })
        })
        .collect();
        for worker in workers {
            worker.join().unwrap();
        }
        listener.accept().unwrap();
        Arc::try_unwrap(log).unwrap().into_inner().unwrap()
    });
    assert_eq!(log.len(), 3);
    let outcome = |w| log.iter().find(|e| e.0 == w).unwrap().1;
    assert_eq!(outcome(0), Err(std::io::ErrorKind::TimedOut));
    assert_eq!(outcome(1), Ok(()));
    assert_eq!(outcome(2), Err(std::io::ErrorKind::TimedOut));
}

#[test]
fn det_backpressure_replays() {
    use std::io::Read;
    let log = replays(|| {
        let listener = std::net::TcpListener::bind("127.0.0.1:9300").unwrap();
        snare::set_tcp_policy("127.0.0.1:9300", |p| {
            p.recv_window = Some(16 * 1024);
            p.latency = Duration::from_millis(5);
            p.jitter = Duration::from_millis(3);
        });
        let mut client = TcpStream::connect("127.0.0.1:9300").unwrap();
        let (mut server, _) = listener.accept().unwrap();
        let start = Instant::now();
        let writer = std::thread::spawn(move || {
            let data: Vec<u8> = (0..2048 * 1024).map(|i| (i % 251) as u8).collect();
            let mut writes = Vec::new();
            let mut sent = 0;
            while sent < data.len() {
                let end = data.len().min(sent + 8192);
                let n = client.write(&data[sent..end]).unwrap();
                writes.push((n, start.elapsed()));
                sent += n;
            }
            writes
        });
        let mut reads = Vec::new();
        let mut got = 0;
        let mut buf = [0u8; 5000];
        while got < 2048 * 1024 {
            let n = server.read(&mut buf).unwrap();
            reads.push((n, start.elapsed()));
            got += n;
        }
        (writer.join().unwrap(), reads)
    });
    let (writes, _) = log;
    assert!(
        writes.last().unwrap().1 >= Duration::from_millis(5 * 8),
        "the window held the writer back for round trips: {writes:?}"
    );
}

#[test]
fn det_icmp_and_stall_replays() {
    let log = replays(|| {
        let start = Instant::now();
        let mut log = Vec::new();
        snare::set_udp_policy("127.0.0.1:9", |p| p.latency = Duration::from_millis(7));
        let a = UdpSocket::bind("127.0.0.1:9400").unwrap();
        let b = UdpSocket::bind("127.0.0.1:9401").unwrap();
        a.connect("127.0.0.1:9").unwrap();
        a.send(b"x").unwrap();
        let mut buf = [0u8; 4];
        log.push(format!(
            "{:?} {:?}",
            a.recv(&mut buf).map_err(|e| e.kind()),
            start.elapsed()
        ));
        snare::quiesce(
            "127.0.0.1:9401",
            Duration::from_millis(30),
            snare::Direction::Receive,
        );
        let sender = std::thread::spawn(|| {
            let c = UdpSocket::bind("127.0.0.1:9402").unwrap();
            for i in 0..3u8 {
                c.send_to(&[i], "127.0.0.1:9401").unwrap();
                std::thread::sleep(Duration::from_millis(4));
            }
        });
        for _ in 0..3 {
            let (n, from) = b.recv_from(&mut buf).unwrap();
            log.push(format!("{} {:?} {from} {:?}", n, buf[0], start.elapsed()));
        }
        sender.join().unwrap();
        log
    });
    assert_eq!(log.len(), 4);
}