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
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Test utilities for dealing with time.

use alloc::{
    collections::{BinaryHeap, HashMap},
    format,
    string::String,
    vec::Vec,
};
use core::{
    fmt::{self, Debug, Formatter},
    hash::Hash,
    ops,
    time::Duration,
};

use assert_matches::assert_matches;

use crate::{
    ref_counted_hash_map::{RefCountedHashSet, RemoveResult},
    time::{
        Instant, InstantBindingsTypes, InstantContext, TimerBindingsTypes, TimerContext,
        TimerHandler,
    },
};

/// A fake implementation of `Instant` for use in testing.
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct FakeInstant {
    /// A FakeInstant is just an offset from some arbitrary epoch.
    pub offset: Duration,
}

impl crate::inspect::InspectableValue for FakeInstant {
    fn record<I: crate::inspect::Inspector>(&self, _name: &str, _inspector: &mut I) {
        unimplemented!()
    }
}

impl FakeInstant {
    /// The maximum value represented by a fake instant.
    pub const LATEST: FakeInstant = FakeInstant { offset: Duration::MAX };

    /// Adds to this fake instant, saturating at [`LATEST`].
    pub fn saturating_add(self, dur: Duration) -> FakeInstant {
        FakeInstant { offset: self.offset.saturating_add(dur) }
    }
}

impl From<Duration> for FakeInstant {
    fn from(offset: Duration) -> FakeInstant {
        FakeInstant { offset }
    }
}

impl Instant for FakeInstant {
    fn duration_since(&self, earlier: FakeInstant) -> Duration {
        self.offset.checked_sub(earlier.offset).unwrap()
    }

    fn saturating_duration_since(&self, earlier: FakeInstant) -> Duration {
        self.offset.saturating_sub(earlier.offset)
    }

    fn checked_add(&self, duration: Duration) -> Option<FakeInstant> {
        self.offset.checked_add(duration).map(|offset| FakeInstant { offset })
    }

    fn checked_sub(&self, duration: Duration) -> Option<FakeInstant> {
        self.offset.checked_sub(duration).map(|offset| FakeInstant { offset })
    }
}

impl ops::Add<Duration> for FakeInstant {
    type Output = FakeInstant;

    fn add(self, dur: Duration) -> FakeInstant {
        FakeInstant { offset: self.offset + dur }
    }
}

impl ops::Sub<FakeInstant> for FakeInstant {
    type Output = Duration;

    fn sub(self, other: FakeInstant) -> Duration {
        self.offset - other.offset
    }
}

impl ops::Sub<Duration> for FakeInstant {
    type Output = FakeInstant;

    fn sub(self, dur: Duration) -> FakeInstant {
        FakeInstant { offset: self.offset - dur }
    }
}

impl Debug for FakeInstant {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.offset)
    }
}

/// A fake [`InstantContext`] which stores the current time as a
/// [`FakeInstant`].
#[derive(Default)]
pub struct FakeInstantCtx {
    /// The fake instant held by this fake context.
    pub time: FakeInstant,
}

impl FakeInstantCtx {
    /// Advance the current time by the given duration.
    pub fn sleep(&mut self, dur: Duration) {
        self.time.offset += dur;
    }
}

impl InstantBindingsTypes for FakeInstantCtx {
    type Instant = FakeInstant;
}

impl InstantContext for FakeInstantCtx {
    fn now(&self) -> FakeInstant {
        self.time
    }
}

impl<T: AsRef<FakeInstantCtx>> InstantBindingsTypes for T {
    type Instant = FakeInstant;
}

impl<T: AsRef<FakeInstantCtx>> InstantContext for T {
    fn now(&self) -> FakeInstant {
        self.as_ref().now()
    }
}

/// Arbitrary data of type `D` attached to a `FakeInstant`.
///
/// `InstantAndData` implements `Ord` and `Eq` to be used in a `BinaryHeap`
/// and ordered by `FakeInstant`.
#[derive(Clone, Debug)]
pub struct InstantAndData<D>(pub FakeInstant, pub D);

impl<D> InstantAndData<D> {
    /// Creates a new `InstantAndData`.
    pub fn new(time: FakeInstant, data: D) -> Self {
        Self(time, data)
    }
}

impl<D> Eq for InstantAndData<D> {}

impl<D> PartialEq for InstantAndData<D> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<D> Ord for InstantAndData<D> {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        other.0.cmp(&self.0)
    }
}

impl<D> PartialOrd for InstantAndData<D> {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

/// A fake timer vended by [`FakeTimerCtx`].
#[derive(Debug, Clone)]
pub struct FakeTimer<Id> {
    timer_id: usize,
    pub dispatch_id: Id,
}

/// A fake [`TimerContext`] which stores time as a [`FakeInstantCtx`].
pub struct FakeTimerCtx<Id> {
    /// The instant context within this fake timer context.
    pub instant: FakeInstantCtx,
    /// The timer heap kept by the fake implementation.
    pub timers: BinaryHeap<InstantAndData<FakeTimer<Id>>>,
    /// Used to issue new [`FakeTimer`] ids.
    next_timer_id: usize,
}

impl<Id> Default for FakeTimerCtx<Id> {
    fn default() -> FakeTimerCtx<Id> {
        FakeTimerCtx {
            instant: FakeInstantCtx::default(),
            timers: BinaryHeap::default(),
            next_timer_id: 0,
        }
    }
}

impl<Id: Clone> FakeTimerCtx<Id> {
    /// Get an ordered list of all currently-scheduled timers.
    pub fn timers(&self) -> Vec<(FakeInstant, Id)> {
        self.timers
            .clone()
            .into_sorted_vec()
            .into_iter()
            .map(|InstantAndData(i, FakeTimer { timer_id: _, dispatch_id })| (i, dispatch_id))
            .collect()
    }
}

impl<Id: Debug + Clone + Hash + Eq> FakeTimerCtx<Id> {
    /// Asserts that `self` contains exactly the timers in `timers`.
    ///
    /// # Panics
    ///
    /// Panics if `timers` contains the same ID more than once or if `self`
    /// does not contain exactly the timers in `timers`.
    ///
    /// [`RangeBounds<FakeInstant>`]: core::ops::RangeBounds
    #[track_caller]
    pub fn assert_timers_installed<I: IntoIterator<Item = (Id, FakeInstant)>>(&self, timers: I) {
        self.assert_timers_installed_range(
            timers.into_iter().map(|(id, instant)| (id, instant..=instant)),
        );
    }

    /// Like [`assert_timers_installed`] but receives a range instants to
    /// match.
    ///
    /// Each timer must be present, and its deadline must fall into the
    /// specified range.
    #[track_caller]
    pub fn assert_timers_installed_range<
        R: ops::RangeBounds<FakeInstant> + Debug,
        I: IntoIterator<Item = (Id, R)>,
    >(
        &self,
        timers: I,
    ) {
        self.assert_timers_installed_inner(timers, true);
    }

    /// Asserts that `self` contains at least the timers in `timers`.
    ///
    /// Like [`assert_timers_installed`], but only asserts that `timers` is
    /// a subset of the timers installed; other timers may be installed in
    /// addition to those in `timers`.
    #[track_caller]
    pub fn assert_some_timers_installed<I: IntoIterator<Item = (Id, FakeInstant)>>(
        &self,
        timers: I,
    ) {
        self.assert_some_timers_installed_range(
            timers.into_iter().map(|(id, instant)| (id, instant..=instant)),
        );
    }

    /// Like [`assert_some_timers_installed`] but receives instant ranges
    /// to match like [`assert_timers_installed_range`].
    #[track_caller]
    pub fn assert_some_timers_installed_range<
        R: ops::RangeBounds<FakeInstant> + Debug,
        I: IntoIterator<Item = (Id, R)>,
    >(
        &self,
        timers: I,
    ) {
        self.assert_timers_installed_inner(timers, false);
    }

    /// Asserts that no timers are installed.
    ///
    /// # Panics
    ///
    /// Panics if any timers are installed.
    #[track_caller]
    pub fn assert_no_timers_installed(&self) {
        self.assert_timers_installed([]);
    }

    #[track_caller]
    fn assert_timers_installed_inner<
        R: ops::RangeBounds<FakeInstant> + Debug,
        I: IntoIterator<Item = (Id, R)>,
    >(
        &self,
        timers: I,
        exact: bool,
    ) {
        let mut timers = timers.into_iter().fold(HashMap::new(), |mut timers, (id, range)| {
            assert_matches!(timers.insert(id, range), None);
            timers
        });

        enum Error<Id, R: ops::RangeBounds<FakeInstant>> {
            ExpectedButMissing { id: Id, range: R },
            UnexpectedButPresent { id: Id, instant: FakeInstant },
            UnexpectedInstant { id: Id, range: R, instant: FakeInstant },
        }

        let mut errors = Vec::new();

        // Make sure that all installed timers were expected (present in
        // `timers`).
        for InstantAndData(instant, FakeTimer { timer_id: _, dispatch_id: id }) in
            self.timers.iter().cloned()
        {
            match timers.remove(&id) {
                None => {
                    if exact {
                        errors.push(Error::UnexpectedButPresent { id, instant })
                    }
                }
                Some(range) => {
                    if !range.contains(&instant) {
                        errors.push(Error::UnexpectedInstant { id, range, instant })
                    }
                }
            }
        }

        // Make sure that all expected timers were already found in
        // `self.timers` (and removed from `timers`).
        errors.extend(timers.drain().map(|(id, range)| Error::ExpectedButMissing { id, range }));

        if errors.len() > 0 {
            let mut s = String::from("Unexpected timer contents:");
            for err in errors {
                s += &match err {
                    Error::ExpectedButMissing { id, range } => {
                        format!("\n\tMissing timer {:?} with deadline {:?}", id, range)
                    }
                    Error::UnexpectedButPresent { id, instant } => {
                        format!("\n\tUnexpected timer {:?} with deadline {:?}", id, instant)
                    }
                    Error::UnexpectedInstant { id, range, instant } => format!(
                        "\n\tTimer {:?} has unexpected deadline {:?} (wanted {:?})",
                        id, instant, range
                    ),
                };
            }
            panic!("{}", s);
        }
    }
}

impl<Id: PartialEq> FakeTimerCtx<Id> {
    fn cancel_timer_inner(&mut self, timer: &FakeTimer<Id>) -> Option<FakeInstant> {
        let mut r: Option<FakeInstant> = None;
        // NB: Cancelling timers can be made faster than this if we keep two
        // data structures and require that `Id: Hash`.
        self.timers.retain(|InstantAndData(instant, FakeTimer { timer_id, dispatch_id: _ })| {
            if timer.timer_id == *timer_id {
                r = Some(*instant);
                false
            } else {
                true
            }
        });
        r
    }
}

impl<Id> AsRef<FakeInstantCtx> for FakeTimerCtx<Id> {
    fn as_ref(&self) -> &FakeInstantCtx {
        &self.instant
    }
}

impl<Id: Debug + Clone + Send + Sync> TimerBindingsTypes for FakeTimerCtx<Id> {
    type Timer = FakeTimer<Id>;
    type DispatchId = Id;
}

impl<Id: PartialEq + Debug + Clone + Send + Sync> TimerContext for FakeTimerCtx<Id> {
    fn new_timer(&mut self, dispatch_id: Self::DispatchId) -> Self::Timer {
        let timer_id = self.next_timer_id;
        self.next_timer_id += 1;
        FakeTimer { timer_id, dispatch_id }
    }

    fn schedule_timer_instant(
        &mut self,
        time: Self::Instant,
        timer: &mut Self::Timer,
    ) -> Option<Self::Instant> {
        let ret = self.cancel_timer_inner(timer);
        self.timers.push(InstantAndData::new(time, timer.clone()));
        ret
    }

    fn cancel_timer(&mut self, timer: &mut Self::Timer) -> Option<Self::Instant> {
        self.cancel_timer_inner(timer)
    }

    fn scheduled_instant(&self, timer: &mut Self::Timer) -> Option<Self::Instant> {
        self.timers.iter().find_map(
            |InstantAndData(instant, FakeTimer { timer_id, dispatch_id: _ })| {
                (timer.timer_id == *timer_id).then_some(*instant)
            },
        )
    }
}

/// A trait abstracting access to a [`FakeTimerCtx`] instance.
pub trait WithFakeTimerContext<TimerId> {
    /// Calls the callback with a borrow of `FakeTimerCtx`.
    fn with_fake_timer_ctx<O, F: FnOnce(&FakeTimerCtx<TimerId>) -> O>(&self, f: F) -> O;

    /// Calls the callback with a mutable borrow of `FakeTimerCtx`.
    fn with_fake_timer_ctx_mut<O, F: FnOnce(&mut FakeTimerCtx<TimerId>) -> O>(&mut self, f: F)
        -> O;
}

impl<TimerId> WithFakeTimerContext<TimerId> for FakeTimerCtx<TimerId> {
    fn with_fake_timer_ctx<O, F: FnOnce(&FakeTimerCtx<TimerId>) -> O>(&self, f: F) -> O {
        f(self)
    }

    fn with_fake_timer_ctx_mut<O, F: FnOnce(&mut FakeTimerCtx<TimerId>) -> O>(
        &mut self,
        f: F,
    ) -> O {
        f(self)
    }
}

/// Adds methods for interacting with [`FakeTimerCtx`] and its wrappers.
pub trait FakeTimerCtxExt<Id>: Sized {
    /// Triggers the next timer, if any, by using the provided `handler`.
    ///
    /// `trigger_next_timer` triggers the next timer, if any, advances the
    /// internal clock to the timer's scheduled time, and returns its ID.
    fn trigger_next_timer<H: TimerHandler<Self, Id>>(&mut self, handler: &mut H) -> Option<Id>;

    /// Skips the current time forward until `instant`, triggering all timers
    /// until then, inclusive, by calling `f` on them.
    ///
    /// Returns the timers which were triggered.
    ///
    /// # Panics
    ///
    /// Panics if `instant` is in the past.
    fn trigger_timers_until_instant<H: TimerHandler<Self, Id>>(
        &mut self,
        instant: FakeInstant,
        handler: &mut H,
    ) -> Vec<Id>;

    /// Skips the current time forward by `duration`, triggering all timers
    /// until then, inclusive, by passing them to the `handler`.
    ///
    /// Returns the timers which were triggered.
    fn trigger_timers_for<H: TimerHandler<Self, Id>>(
        &mut self,
        duration: Duration,
        handler: &mut H,
    ) -> Vec<Id>;

    /// Triggers timers and expects them to be the given timers.
    ///
    /// The number of timers to be triggered is taken to be the number of timers
    /// produced by `timers`. Timers may be triggered in any order.
    ///
    /// # Panics
    ///
    /// Panics under the following conditions:
    /// - Fewer timers could be triggered than expected
    /// - Timers were triggered that were not expected
    /// - Timers that were expected were not triggered
    #[track_caller]
    fn trigger_timers_and_expect_unordered<I: IntoIterator<Item = Id>, H: TimerHandler<Self, Id>>(
        &mut self,
        timers: I,
        handler: &mut H,
    ) where
        Id: Debug + Hash + Eq;

    /// Triggers timers until `instant` and expects them to be the given timers.
    ///
    /// Like `trigger_timers_and_expect_unordered`, except that timers will only
    /// be triggered until `instant` (inclusive).
    fn trigger_timers_until_and_expect_unordered<
        I: IntoIterator<Item = Id>,
        H: TimerHandler<Self, Id>,
    >(
        &mut self,
        instant: FakeInstant,
        timers: I,
        handler: &mut H,
    ) where
        Id: Debug + Hash + Eq;

    /// Triggers timers for `duration` and expects them to be the given timers.
    ///
    /// Like `trigger_timers_and_expect_unordered`, except that timers will only
    /// be triggered for `duration` (inclusive).
    fn trigger_timers_for_and_expect<I: IntoIterator<Item = Id>, H: TimerHandler<Self, Id>>(
        &mut self,
        duration: Duration,
        timers: I,
        handler: &mut H,
    ) where
        Id: Debug + Hash + Eq;
}

// TODO(https://fxbug.dev/42081080): hold lock on `FakeTimerCtx` across entire
// method to avoid potential race conditions.
impl<Id: Clone, Ctx: WithFakeTimerContext<Id>> FakeTimerCtxExt<Id> for Ctx {
    /// Triggers the next timer, if any, by calling `f` on it.
    ///
    /// `trigger_next_timer` triggers the next timer, if any, advances the
    /// internal clock to the timer's scheduled time, and returns its ID.
    fn trigger_next_timer<H: TimerHandler<Self, Id>>(&mut self, handler: &mut H) -> Option<Id> {
        self.with_fake_timer_ctx_mut(|timers| {
            timers.timers.pop().map(|InstantAndData(t, id)| {
                timers.instant.time = t;
                id
            })
        })
        .map(|FakeTimer { timer_id: _, dispatch_id }| {
            handler.handle_timer(self, dispatch_id.clone());
            dispatch_id
        })
    }

    /// Skips the current time forward until `instant`, triggering all timers
    /// until then, inclusive, by giving them to `handler`.
    ///
    /// Returns the timers which were triggered.
    ///
    /// # Panics
    ///
    /// Panics if `instant` is in the past.
    fn trigger_timers_until_instant<H: TimerHandler<Self, Id>>(
        &mut self,
        instant: FakeInstant,
        handler: &mut H,
    ) -> Vec<Id> {
        assert!(instant >= self.with_fake_timer_ctx(|ctx| ctx.now()));
        let mut timers = Vec::new();

        while self.with_fake_timer_ctx_mut(|ctx| {
            ctx.timers.peek().map(|InstantAndData(i, _id)| i <= &instant).unwrap_or(false)
        }) {
            timers.push(self.trigger_next_timer(handler).unwrap())
        }

        self.with_fake_timer_ctx_mut(|ctx| {
            assert!(ctx.now() <= instant);
            ctx.instant.time = instant;
        });

        timers
    }

    /// Skips the current time forward by `duration`, triggering all timers
    /// until then, inclusive, by calling `f` on them.
    ///
    /// Returns the timers which were triggered.
    fn trigger_timers_for<H: TimerHandler<Self, Id>>(
        &mut self,
        duration: Duration,
        handler: &mut H,
    ) -> Vec<Id> {
        let instant = self.with_fake_timer_ctx(|ctx| ctx.now().saturating_add(duration));
        // We know the call to `self.trigger_timers_until_instant` will not
        // panic because we provide an instant that is greater than or equal
        // to the current time.
        self.trigger_timers_until_instant(instant, handler)
    }

    /// Triggers timers and expects them to be the given timers.
    ///
    /// The number of timers to be triggered is taken to be the number of
    /// timers produced by `timers`. Timers may be triggered in any order.
    ///
    /// # Panics
    ///
    /// Panics under the following conditions:
    /// - Fewer timers could be triggered than expected
    /// - Timers were triggered that were not expected
    /// - Timers that were expected were not triggered
    #[track_caller]
    fn trigger_timers_and_expect_unordered<I: IntoIterator<Item = Id>, H: TimerHandler<Self, Id>>(
        &mut self,
        timers: I,
        handler: &mut H,
    ) where
        Id: Debug + Hash + Eq,
    {
        let mut timers = RefCountedHashSet::from_iter(timers);

        for _ in 0..timers.len() {
            let id = self.trigger_next_timer(handler).expect("ran out of timers to trigger");
            match timers.remove(id.clone()) {
                RemoveResult::Removed(()) | RemoveResult::StillPresent => {}
                RemoveResult::NotPresent => panic!("triggered unexpected timer: {:?}", id),
            }
        }

        if timers.len() > 0 {
            let mut s = String::from("Expected timers did not trigger:");
            for (id, count) in timers.iter_counts() {
                s += &format!("\n\t{count}x {id:?}");
            }
            panic!("{}", s);
        }
    }

    /// Triggers timers until `instant` and expects them to be the given
    /// timers.
    ///
    /// Like `trigger_timers_and_expect_unordered`, except that timers will
    /// only be triggered until `instant` (inclusive).
    fn trigger_timers_until_and_expect_unordered<
        I: IntoIterator<Item = Id>,
        H: TimerHandler<Self, Id>,
    >(
        &mut self,
        instant: FakeInstant,
        timers: I,
        handler: &mut H,
    ) where
        Id: Debug + Hash + Eq,
    {
        let mut timers = RefCountedHashSet::from_iter(timers);

        let triggered_timers = self.trigger_timers_until_instant(instant, handler);

        for id in triggered_timers {
            match timers.remove(id.clone()) {
                RemoveResult::Removed(()) | RemoveResult::StillPresent => {}
                RemoveResult::NotPresent => panic!("triggered unexpected timer: {:?}", id),
            }
        }

        if timers.len() > 0 {
            let mut s = String::from("Expected timers did not trigger:");
            for (id, count) in timers.iter_counts() {
                s += &format!("\n\t{count}x {id:?}");
            }
            panic!("{}", s);
        }
    }

    /// Triggers timers for `duration` and expects them to be the given
    /// timers.
    ///
    /// Like `trigger_timers_and_expect_unordered`, except that timers will
    /// only be triggered for `duration` (inclusive).
    fn trigger_timers_for_and_expect<I: IntoIterator<Item = Id>, H: TimerHandler<Self, Id>>(
        &mut self,
        duration: Duration,
        timers: I,
        handler: &mut H,
    ) where
        Id: Debug + Hash + Eq,
    {
        let instant = self.with_fake_timer_ctx(|ctx| ctx.now().saturating_add(duration));
        self.trigger_timers_until_and_expect_unordered(instant, timers, handler);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::HandleableTimer;

    use alloc::vec;

    const ONE_SEC: Duration = Duration::from_secs(1);
    const ONE_SEC_INSTANT: FakeInstant = FakeInstant { offset: ONE_SEC };

    #[derive(Debug, Eq, PartialEq, Clone, Hash)]
    struct TimerId(usize);
    #[derive(Default)]
    struct CoreCtx(Vec<(TimerId, FakeInstant)>);

    impl CoreCtx {
        fn take(&mut self) -> Vec<(TimerId, FakeInstant)> {
            core::mem::take(&mut self.0)
        }
    }

    impl HandleableTimer<CoreCtx, FakeTimerCtx<Self>> for TimerId {
        fn handle(self, CoreCtx(expired): &mut CoreCtx, bindings_ctx: &mut FakeTimerCtx<Self>) {
            expired.push((self, bindings_ctx.now()))
        }
    }

    #[test]
    fn instant_and_data() {
        // Verify implementation of InstantAndData to be used as a complex
        // type in a BinaryHeap.
        let mut heap = BinaryHeap::<InstantAndData<usize>>::new();
        let now = FakeInstant::default();

        fn new_data(time: FakeInstant, id: usize) -> InstantAndData<usize> {
            InstantAndData::new(time, id)
        }

        heap.push(new_data(now + Duration::from_secs(1), 1));
        heap.push(new_data(now + Duration::from_secs(2), 2));

        // Earlier timer is popped first.
        assert_eq!(heap.pop().unwrap().1, 1);
        assert_eq!(heap.pop().unwrap().1, 2);
        assert_eq!(heap.pop(), None);

        heap.push(new_data(now + Duration::from_secs(1), 1));
        heap.push(new_data(now + Duration::from_secs(1), 1));

        // Can pop twice with identical data.
        assert_eq!(heap.pop().unwrap().1, 1);
        assert_eq!(heap.pop().unwrap().1, 1);
        assert_eq!(heap.pop(), None);
    }

    #[test]
    fn fake_timer_context() {
        let mut core_ctx = CoreCtx::default();
        let mut bindings_ctx = FakeTimerCtx::<TimerId>::default();

        // When no timers are installed, `trigger_next_timer` should return
        // `false`.
        assert_eq!(bindings_ctx.trigger_next_timer(&mut core_ctx), None);
        assert_eq!(core_ctx.take(), vec![]);

        let mut timer0 = bindings_ctx.new_timer(TimerId(0));
        let mut timer1 = bindings_ctx.new_timer(TimerId(1));
        let mut timer2 = bindings_ctx.new_timer(TimerId(2));

        // No timer with id `0` exists yet.
        assert_eq!(bindings_ctx.scheduled_instant(&mut timer0), None);

        assert_eq!(bindings_ctx.schedule_timer(ONE_SEC, &mut timer0), None);

        // Timer with id `0` scheduled to execute at `ONE_SEC_INSTANT`.
        assert_eq!(bindings_ctx.scheduled_instant(&mut timer0).unwrap(), ONE_SEC_INSTANT);

        assert_eq!(bindings_ctx.trigger_next_timer(&mut core_ctx), Some(TimerId(0)));
        assert_eq!(core_ctx.take(), vec![(TimerId(0), ONE_SEC_INSTANT)]);

        // After the timer fires, it should not still be scheduled at some
        // instant.
        assert_eq!(bindings_ctx.scheduled_instant(&mut timer0), None);

        // The time should have been advanced.
        assert_eq!(bindings_ctx.now(), ONE_SEC_INSTANT);

        // Once it's been triggered, it should be canceled and not
        // triggerable again.
        assert_eq!(bindings_ctx.trigger_next_timer(&mut core_ctx), None);
        assert_eq!(core_ctx.take(), vec![]);

        // Unwind back time.
        bindings_ctx.instant.time = Default::default();

        // If we schedule a timer but then cancel it, it shouldn't fire.
        assert_eq!(bindings_ctx.schedule_timer(ONE_SEC, &mut timer0), None);
        assert_eq!(bindings_ctx.cancel_timer(&mut timer0), Some(ONE_SEC_INSTANT));
        assert_eq!(bindings_ctx.trigger_next_timer(&mut core_ctx), None);
        assert_eq!(core_ctx.take(), vec![]);

        // If we schedule a timer but then schedule the same ID again, the
        // second timer should overwrite the first one.
        assert_eq!(bindings_ctx.schedule_timer(Duration::from_secs(0), &mut timer0), None);
        assert_eq!(
            bindings_ctx.schedule_timer(ONE_SEC, &mut timer0),
            Some(Duration::from_secs(0).into())
        );
        assert_eq!(bindings_ctx.cancel_timer(&mut timer0), Some(ONE_SEC_INSTANT));

        // If we schedule three timers and then run `trigger_timers_until`
        // with the appropriate value, only two of them should fire.
        assert_eq!(bindings_ctx.schedule_timer(Duration::from_secs(0), &mut timer0), None);
        assert_eq!(bindings_ctx.schedule_timer(Duration::from_secs(1), &mut timer1), None);
        assert_eq!(bindings_ctx.schedule_timer(Duration::from_secs(2), &mut timer2), None);
        assert_eq!(
            bindings_ctx.trigger_timers_until_instant(ONE_SEC_INSTANT, &mut core_ctx),
            vec![TimerId(0), TimerId(1)],
        );

        // The first two timers should have fired.
        assert_eq!(
            core_ctx.take(),
            vec![
                (TimerId(0), FakeInstant::from(Duration::from_secs(0))),
                (TimerId(1), ONE_SEC_INSTANT)
            ]
        );

        // They should be canceled now.
        assert_eq!(bindings_ctx.cancel_timer(&mut timer0), None);
        assert_eq!(bindings_ctx.cancel_timer(&mut timer1), None);

        // The clock should have been updated.
        assert_eq!(bindings_ctx.now(), ONE_SEC_INSTANT);

        // The last timer should not have fired.
        assert_eq!(
            bindings_ctx.cancel_timer(&mut timer2),
            Some(FakeInstant::from(Duration::from_secs(2)))
        );
    }

    #[test]
    fn trigger_timers_until_and_expect_unordered() {
        // If the requested instant does not coincide with a timer trigger
        // point, the time should still be advanced.
        let mut core_ctx = CoreCtx::default();
        let mut bindings_ctx = FakeTimerCtx::default();
        let mut timer0 = bindings_ctx.new_timer(TimerId(0));
        let mut timer1 = bindings_ctx.new_timer(TimerId(1));
        assert_eq!(bindings_ctx.schedule_timer(Duration::from_secs(0), &mut timer0), None);
        assert_eq!(bindings_ctx.schedule_timer(Duration::from_secs(2), &mut timer1), None);
        bindings_ctx.trigger_timers_until_and_expect_unordered(
            ONE_SEC_INSTANT,
            vec![TimerId(0)],
            &mut core_ctx,
        );
        assert_eq!(bindings_ctx.now(), ONE_SEC_INSTANT);
    }
}