-
Notifications
You must be signed in to change notification settings - Fork 202
/
lib.rs
794 lines (700 loc) 路 25.6 KB
/
lib.rs
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
//! A debouncer for [notify] that is optimized for ease of use.
//!
//! * Only emits a single `Rename` event if the rename `From` and `To` events can be matched
//! * Merges multiple `Rename` events
//! * Takes `Rename` events into account and updates paths for events that occurred before the rename event, but which haven't been emitted, yet
//! * Optionally keeps track of the file system IDs all files and stiches rename events together (FSevents, Windows)
//! * Emits only one `Remove` event when deleting a directory (inotify)
//! * Doesn't emit duplicate create events
//! * Doesn't emit `Modify` events after a `Create` event
//!
//! # Installation
//!
//! ```toml
//! [dependencies]
//! notify-debouncer-full = "0.3.1"
//! ```
//!
//! In case you want to select specific features of notify,
//! specify notify as dependency explicitly in your dependencies.
//! Otherwise you can just use the re-export of notify from debouncer-full.
//!
//! ```toml
//! notify-debouncer-full = "0.3.1"
//! notify = { version = "..", features = [".."] }
//! ```
//!
//! # Examples
//!
//! ```rust,no_run
//! # use std::path::Path;
//! # use std::time::Duration;
//! use notify_debouncer_full::{notify::*, new_debouncer, DebounceEventResult};
//!
//! // Select recommended watcher for debouncer.
//! // Using a callback here, could also be a channel.
//! let mut debouncer = new_debouncer(Duration::from_secs(2), None, |result: DebounceEventResult| {
//! match result {
//! Ok(events) => events.iter().for_each(|event| println!("{event:?}")),
//! Err(errors) => errors.iter().for_each(|error| println!("{error:?}")),
//! }
//! }).unwrap();
//!
//! // Add a path to be watched. All files and directories at that path and
//! // below will be monitored for changes.
//! debouncer.watcher().watch(Path::new("."), RecursiveMode::Recursive).unwrap();
//!
//! // Add the same path to the file ID cache. The cache uses unique file IDs
//! // provided by the file system and is used to stich together rename events
//! // in case the notification back-end doesn't emit rename cookies.
//! debouncer.cache().add_root(Path::new("."), RecursiveMode::Recursive);
//! ```
//!
//! # Features
//!
//! The following crate features can be turned on or off in your cargo dependency config:
//!
//! - `crossbeam` enabled by default, adds [`DebounceEventHandler`](DebounceEventHandler) support for crossbeam channels.
//! Also enables crossbeam-channel in the re-exported notify. You may want to disable this when using the tokio async runtime.
//! - `serde` enables serde support for events.
//!
//! # Caveats
//!
//! As all file events are sourced from notify, the [known problems](https://docs.rs/notify/latest/notify/#known-problems) section applies here too.
mod cache;
mod debounced_event;
#[cfg(test)]
mod testing;
use std::{
collections::{HashMap, VecDeque},
path::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::Duration,
};
pub use cache::{FileIdCache, FileIdMap, NoCache};
pub use debounced_event::DebouncedEvent;
pub use file_id;
pub use notify;
use file_id::FileId;
use notify::{
event::{ModifyKind, RemoveKind, RenameMode},
Error, ErrorKind, Event, EventKind, RecommendedWatcher, Watcher,
};
use parking_lot::{MappedMutexGuard, Mutex, MutexGuard};
#[cfg(test)]
use mock_instant::Instant;
#[cfg(not(test))]
use std::time::Instant;
/// The set of requirements for watcher debounce event handling functions.
///
/// # Example implementation
///
/// ```rust,no_run
/// # use notify::{Event, Result, EventHandler};
/// # use notify_debouncer_full::{DebounceEventHandler, DebounceEventResult};
///
/// /// Prints received events
/// struct EventPrinter;
///
/// impl DebounceEventHandler for EventPrinter {
/// fn handle_event(&mut self, result: DebounceEventResult) {
/// match result {
/// Ok(events) => events.iter().for_each(|event| println!("{event:?}")),
/// Err(errors) => errors.iter().for_each(|error| println!("{error:?}")),
/// }
/// }
/// }
/// ```
pub trait DebounceEventHandler: Send + 'static {
/// Handles an event.
fn handle_event(&mut self, event: DebounceEventResult);
}
impl<F> DebounceEventHandler for F
where
F: FnMut(DebounceEventResult) + Send + 'static,
{
fn handle_event(&mut self, event: DebounceEventResult) {
(self)(event);
}
}
#[cfg(feature = "crossbeam")]
impl DebounceEventHandler for crossbeam_channel::Sender<DebounceEventResult> {
fn handle_event(&mut self, event: DebounceEventResult) {
let _ = self.send(event);
}
}
impl DebounceEventHandler for std::sync::mpsc::Sender<DebounceEventResult> {
fn handle_event(&mut self, event: DebounceEventResult) {
let _ = self.send(event);
}
}
/// A result of debounced events.
/// Comes with either a vec of events or vec of errors.
pub type DebounceEventResult = Result<Vec<DebouncedEvent>, Vec<Error>>;
type DebounceData<T> = Arc<Mutex<DebounceDataInner<T>>>;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct Queue {
/// Events must be stored in the following order:
/// 1. `remove` or `move out` event
/// 2. `rename` event
/// 3. Other events
events: VecDeque<DebouncedEvent>,
}
impl Queue {
fn was_created(&self) -> bool {
self.events.front().map_or(false, |event| {
matches!(
event.kind,
EventKind::Create(_) | EventKind::Modify(ModifyKind::Name(RenameMode::To))
)
})
}
fn was_removed(&self) -> bool {
self.events.front().map_or(false, |event| {
matches!(
event.kind,
EventKind::Remove(_) | EventKind::Modify(ModifyKind::Name(RenameMode::From))
)
})
}
}
#[derive(Debug)]
pub(crate) struct DebounceDataInner<T> {
queues: HashMap<PathBuf, Queue>,
cache: T,
rename_event: Option<(DebouncedEvent, Option<FileId>)>,
rescan_event: Option<DebouncedEvent>,
errors: Vec<Error>,
timeout: Duration,
}
impl<T: FileIdCache> DebounceDataInner<T> {
pub(crate) fn new(cache: T, timeout: Duration) -> Self {
Self {
queues: HashMap::new(),
cache,
rename_event: None,
rescan_event: None,
errors: Vec::new(),
timeout,
}
}
/// Retrieve a vec of debounced events, removing them if not continuous
pub fn debounced_events(&mut self) -> Vec<DebouncedEvent> {
let now = Instant::now();
let mut events_expired = Vec::with_capacity(self.queues.len());
let mut queues_remaining = HashMap::with_capacity(self.queues.len());
if let Some(event) = self.rescan_event.take() {
if now.saturating_duration_since(event.time) >= self.timeout {
log::trace!("debounced event: {event:?}");
events_expired.push(event);
} else {
self.rescan_event = Some(event);
}
}
// TODO: perfect fit for drain_filter https://github.com/rust-lang/rust/issues/59618
for (path, mut queue) in self.queues.drain() {
let mut kind_index = HashMap::new();
while let Some(event) = queue.events.pop_front() {
if now.saturating_duration_since(event.time) >= self.timeout {
// remove previous event of the same kind
if let Some(idx) = kind_index.get(&event.kind).copied() {
events_expired.remove(idx);
kind_index.values_mut().for_each(|i| {
if *i > idx {
*i -= 1
}
})
}
kind_index.insert(event.kind, events_expired.len());
events_expired.push(event);
} else {
queue.events.push_front(event);
break;
}
}
if !queue.events.is_empty() {
queues_remaining.insert(path, queue);
}
}
self.queues = queues_remaining;
// order events for different files chronologically, but keep the order of events for the same file
events_expired.sort_by(|event_a, event_b| {
// use the last path because rename events are emitted for the target path
if event_a.paths.last() == event_b.paths.last() {
std::cmp::Ordering::Equal
} else {
event_a.time.cmp(&event_b.time)
}
});
events_expired
}
/// Returns all currently stored errors
pub fn errors(&mut self) -> Vec<Error> {
let mut v = Vec::new();
std::mem::swap(&mut v, &mut self.errors);
v
}
/// Add an error entry to re-send later on
pub fn add_error(&mut self, error: Error) {
self.errors.push(error);
}
/// Add new event to debouncer cache
pub fn add_event(&mut self, event: Event) {
log::trace!("raw event: {event:?}");
if event.need_rescan() {
self.cache.rescan();
self.rescan_event = Some(event.into());
return;
}
let path = &event.paths[0];
match &event.kind {
EventKind::Create(_) => {
self.cache.add_path(path);
self.push_event(event, Instant::now());
}
EventKind::Modify(ModifyKind::Name(rename_mode)) => {
match rename_mode {
RenameMode::Any => {
if event.paths[0].exists() {
self.handle_rename_to(event);
} else {
self.handle_rename_from(event);
}
}
RenameMode::To => {
self.handle_rename_to(event);
}
RenameMode::From => {
self.handle_rename_from(event);
}
RenameMode::Both => {
// ignore and handle `To` and `From` events instead
}
RenameMode::Other => {
// unused
}
}
}
EventKind::Remove(_) => {
self.push_remove_event(event, Instant::now());
}
EventKind::Other => {
// ignore meta events
}
_ => {
if self.cache.cached_file_id(path).is_none() {
self.cache.add_path(path);
}
self.push_event(event, Instant::now());
}
}
}
fn handle_rename_from(&mut self, event: Event) {
let time = Instant::now();
let path = &event.paths[0];
// store event
let file_id = self.cache.cached_file_id(path).cloned();
self.rename_event = Some((DebouncedEvent::new(event.clone(), time), file_id));
self.cache.remove_path(path);
self.push_event(event, time);
}
fn handle_rename_to(&mut self, event: Event) {
self.cache.add_path(&event.paths[0]);
let trackers_match = self
.rename_event
.as_ref()
.and_then(|(e, _)| e.tracker())
.and_then(|from_tracker| {
event
.attrs
.tracker()
.map(|to_tracker| from_tracker == to_tracker)
})
.unwrap_or_default();
let file_ids_match = self
.rename_event
.as_ref()
.and_then(|(_, id)| id.as_ref())
.and_then(|from_file_id| {
self.cache
.cached_file_id(&event.paths[0])
.map(|to_file_id| from_file_id == to_file_id)
})
.unwrap_or_default();
if trackers_match || file_ids_match {
// connect rename
let (mut rename_event, _) = self.rename_event.take().unwrap(); // unwrap is safe because `rename_event` must be set at this point
let path = rename_event.paths.remove(0);
let time = rename_event.time;
self.push_rename_event(path, event, time);
} else {
// move in
self.push_event(event, Instant::now());
}
self.rename_event = None;
}
fn push_rename_event(&mut self, path: PathBuf, event: Event, time: Instant) {
self.cache.remove_path(&path);
let mut source_queue = self.queues.remove(&path).unwrap_or_default();
// remove rename `from` event
source_queue.events.pop_back();
// remove existing rename event
let (remove_index, original_path, original_time) = source_queue
.events
.iter()
.enumerate()
.find_map(|(index, e)| {
if matches!(
e.kind,
EventKind::Modify(ModifyKind::Name(RenameMode::Both))
) {
Some((Some(index), e.paths[0].clone(), e.time))
} else {
None
}
})
.unwrap_or((None, path, time));
if let Some(remove_index) = remove_index {
source_queue.events.remove(remove_index);
}
// split off remove or move out event and add it back to the events map
if source_queue.was_removed() {
let event = source_queue.events.pop_front().unwrap();
self.queues.insert(
event.paths[0].clone(),
Queue {
events: [event].into(),
},
);
}
// update paths
for e in &mut source_queue.events {
e.paths = vec![event.paths[0].clone()];
}
// insert rename event at the front, unless the file was just created
if !source_queue.was_created() {
source_queue.events.push_front(DebouncedEvent {
event: Event {
kind: EventKind::Modify(ModifyKind::Name(RenameMode::Both)),
paths: vec![original_path, event.paths[0].clone()],
attrs: event.attrs,
},
time: original_time,
});
}
if let Some(target_queue) = self.queues.get_mut(&event.paths[0]) {
if !target_queue.was_created() {
let mut remove_event = DebouncedEvent {
event: Event {
kind: EventKind::Remove(RemoveKind::Any),
paths: vec![event.paths[0].clone()],
attrs: Default::default(),
},
time: original_time,
};
if !target_queue.was_removed() {
remove_event.event = remove_event.event.set_info("override");
}
source_queue.events.push_front(remove_event);
}
*target_queue = source_queue;
} else {
self.queues.insert(event.paths[0].clone(), source_queue);
}
}
fn push_remove_event(&mut self, event: Event, time: Instant) {
let path = &event.paths[0];
// remove child queues
self.queues.retain(|p, _| !p.starts_with(path) || p == path);
// remove cached file ids
self.cache.remove_path(path);
match self.queues.get_mut(path) {
Some(queue) if queue.was_created() => {
self.queues.remove(path);
}
Some(queue) => {
queue.events = [DebouncedEvent::new(event, time)].into();
}
None => {
self.push_event(event, time);
}
}
}
fn push_event(&mut self, event: Event, time: Instant) {
let path = &event.paths[0];
if let Some(queue) = self.queues.get_mut(path) {
// skip duplicate create events and modifications right after creation
if match event.kind {
EventKind::Modify(ModifyKind::Data(_) | ModifyKind::Metadata(_))
| EventKind::Create(_) => !queue.was_created(),
_ => true,
} {
queue.events.push_back(DebouncedEvent::new(event, time));
}
} else {
self.queues.insert(
path.to_path_buf(),
Queue {
events: [DebouncedEvent::new(event, time)].into(),
},
);
}
}
}
/// Debouncer guard, stops the debouncer on drop.
#[derive(Debug)]
pub struct Debouncer<T: Watcher, C: FileIdCache> {
watcher: T,
debouncer_thread: Option<std::thread::JoinHandle<()>>,
data: DebounceData<C>,
stop: Arc<AtomicBool>,
}
impl<T: Watcher, C: FileIdCache> Debouncer<T, C> {
/// Stop the debouncer, waits for the event thread to finish.
/// May block for the duration of one tick_rate.
pub fn stop(mut self) {
self.set_stop();
if let Some(t) = self.debouncer_thread.take() {
let _ = t.join();
}
}
/// Stop the debouncer, does not wait for the event thread to finish.
pub fn stop_nonblocking(self) {
self.set_stop();
}
fn set_stop(&self) {
self.stop.store(true, Ordering::Relaxed);
}
/// Access to the internally used notify Watcher backend
pub fn watcher(&mut self) -> &mut T {
&mut self.watcher
}
/// Access to the internally used notify Watcher backend
pub fn cache(&mut self) -> MappedMutexGuard<C> {
MutexGuard::map(self.data.lock(), |data| &mut data.cache)
}
}
impl<T: Watcher, C: FileIdCache> Drop for Debouncer<T, C> {
fn drop(&mut self) {
self.set_stop();
}
}
/// Creates a new debounced watcher with custom configuration.
///
/// Timeout is the amount of time after which a debounced event is emitted.
///
/// If tick_rate is None, notify will select a tick rate that is 1/4 of the provided timeout.
pub fn new_debouncer_opt<F: DebounceEventHandler, T: Watcher, C: FileIdCache + Send + 'static>(
timeout: Duration,
tick_rate: Option<Duration>,
mut event_handler: F,
file_id_cache: C,
config: notify::Config,
) -> Result<Debouncer<T, C>, Error> {
let data = Arc::new(Mutex::new(DebounceDataInner::new(file_id_cache, timeout)));
let stop = Arc::new(AtomicBool::new(false));
let tick_div = 4;
let tick = match tick_rate {
Some(v) => {
if v > timeout {
return Err(Error::new(ErrorKind::Generic(format!(
"Invalid tick_rate, tick rate {:?} > {:?} timeout!",
v, timeout
))));
}
v
}
None => timeout.checked_div(tick_div).ok_or_else(|| {
Error::new(ErrorKind::Generic(format!(
"Failed to calculate tick as {:?}/{}!",
timeout, tick_div
)))
})?,
};
let data_c = data.clone();
let stop_c = stop.clone();
let thread = std::thread::Builder::new()
.name("notify-rs debouncer loop".to_string())
.spawn(move || loop {
if stop_c.load(Ordering::Acquire) {
break;
}
std::thread::sleep(tick);
let send_data;
let errors;
{
let mut lock = data_c.lock();
send_data = lock.debounced_events();
errors = lock.errors();
}
if !send_data.is_empty() {
event_handler.handle_event(Ok(send_data));
}
if !errors.is_empty() {
event_handler.handle_event(Err(errors));
}
})?;
let data_c = data.clone();
let watcher = T::new(
move |e: Result<Event, Error>| {
let mut lock = data_c.lock();
match e {
Ok(e) => lock.add_event(e),
// can't have multiple TX, so we need to pipe that through our debouncer
Err(e) => lock.add_error(e),
}
},
config,
)?;
let guard = Debouncer {
watcher,
debouncer_thread: Some(thread),
data,
stop,
};
Ok(guard)
}
/// Short function to create a new debounced watcher with the recommended debouncer and the built-in file ID cache.
///
/// Timeout is the amount of time after which a debounced event is emitted.
///
/// If tick_rate is None, notify will select a tick rate that is 1/4 of the provided timeout.
pub fn new_debouncer<F: DebounceEventHandler>(
timeout: Duration,
tick_rate: Option<Duration>,
event_handler: F,
) -> Result<Debouncer<RecommendedWatcher, FileIdMap>, Error> {
new_debouncer_opt::<F, RecommendedWatcher, FileIdMap>(
timeout,
tick_rate,
event_handler,
FileIdMap::new(),
notify::Config::default(),
)
}
#[cfg(test)]
mod tests {
use std::{fs, path::Path};
use super::*;
use mock_instant::MockClock;
use pretty_assertions::assert_eq;
use rstest::rstest;
use testing::TestCase;
#[rstest]
fn state(
#[values(
"add_create_event",
"add_create_event_after_remove_event",
"add_create_dir_event_twice",
"add_modify_content_event_after_create_event",
"add_rename_from_event",
"add_rename_from_event_after_create_event",
"add_rename_from_event_after_modify_event",
"add_rename_from_event_after_create_and_modify_event",
"add_rename_from_event_after_rename_from_event",
"add_rename_to_event",
"add_rename_to_dir_event",
"add_rename_from_and_to_event",
"add_rename_from_and_to_event_after_create",
"add_rename_from_and_to_event_after_rename",
"add_rename_from_and_to_event_after_modify_content",
"add_rename_from_and_to_event_override_created",
"add_rename_from_and_to_event_override_modified",
"add_rename_from_and_to_event_override_removed",
"add_rename_from_and_to_event_with_file_ids",
"add_rename_from_and_to_event_with_different_file_ids",
"add_rename_from_and_to_event_with_different_tracker",
"add_rename_both_event",
"add_remove_event",
"add_remove_event_after_create_event",
"add_remove_event_after_modify_event",
"add_remove_event_after_create_and_modify_event",
"add_remove_parent_event_after_remove_child_event",
"add_errors",
"emit_continuous_modify_content_events",
"emit_events_in_chronological_order",
"emit_events_with_a_prepended_rename_event",
"emit_close_events_only_once",
"emit_modify_event_after_close_event",
"emit_needs_rescan_event",
"read_file_id_without_create_event"
)]
file_name: &str,
) {
let file_content =
fs::read_to_string(Path::new(&format!("./test_cases/{file_name}.hjson"))).unwrap();
let mut test_case = deser_hjson::from_str::<TestCase>(&file_content).unwrap();
MockClock::set_time(Duration::default());
let time = Instant::now();
let mut state = test_case.state.into_debounce_data_inner(time);
for event in test_case.events {
let event = event.into_debounced_event(time, None);
MockClock::set_time(event.time - time);
state.add_event(event.event);
}
for error in test_case.errors {
let e = error.into_notify_error();
state.add_error(e);
}
let expected_errors = std::mem::take(&mut test_case.expected.errors);
let expected_events = std::mem::take(&mut test_case.expected.events);
let expected_state = test_case.expected.into_debounce_data_inner(time);
assert_eq!(
state.queues, expected_state.queues,
"queues not as expected"
);
assert_eq!(
state.rename_event, expected_state.rename_event,
"rename event not as expected"
);
assert_eq!(
state.rescan_event, expected_state.rescan_event,
"rescan event not as expected"
);
assert_eq!(
state.cache.paths, expected_state.cache.paths,
"cache not as expected"
);
assert_eq!(
state
.errors
.iter()
.map(|e| format!("{:?}", e))
.collect::<Vec<_>>(),
expected_errors
.iter()
.map(|e| format!("{:?}", e.clone().into_notify_error()))
.collect::<Vec<_>>(),
"errors not as expected"
);
let backup_time = Instant::now().duration_since(time);
let backup_queues = state.queues.clone();
for (delay, events) in expected_events {
MockClock::set_time(backup_time);
state.queues = backup_queues.clone();
match delay.as_str() {
"none" => {}
"short" => MockClock::advance(Duration::from_millis(10)),
"long" => MockClock::advance(Duration::from_millis(100)),
_ => {
if let Ok(ts) = delay.parse::<u64>() {
let ts = time + Duration::from_millis(ts);
MockClock::set_time(ts - time);
}
}
}
let events = events
.into_iter()
.map(|event| event.into_debounced_event(time, None))
.collect::<Vec<_>>();
assert_eq!(
state.debounced_events(),
events,
"debounced events after a `{delay}` delay"
);
}
}
}