Always flush historical events in `fsevent` tests

Antonio Scandurra created

Change summary

fsevent/src/lib.rs | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

Detailed changes

fsevent/src/lib.rs 🔗

@@ -307,14 +307,12 @@ mod tests {
             for i in 0..10 {
                 fs::write(path.join(format!("existing-file-{}", i)), "").unwrap();
             }
+            flush_historical_events();
 
             let (tx, rx) = mpsc::channel();
             let (stream, handle) = EventStream::new(&[&path], Duration::from_millis(50));
             thread::spawn(move || stream.run(move |events| tx.send(events.to_vec()).is_ok()));
 
-            // Flush any historical events.
-            rx.recv_timeout(Duration::from_secs(2)).ok();
-
             fs::write(path.join("new-file"), "").unwrap();
             let events = rx.recv_timeout(Duration::from_secs(2)).unwrap();
             let event = events.last().unwrap();
@@ -338,6 +336,7 @@ mod tests {
             for i in 0..10 {
                 fs::write(path.join(format!("existing-file-{}", i)), "").unwrap();
             }
+            flush_historical_events();
 
             let (tx, rx) = mpsc::channel();
             let (stream, handle) = EventStream::new(&[&path], Duration::from_millis(50));
@@ -350,7 +349,7 @@ mod tests {
             });
 
             fs::write(path.join("new-file"), "").unwrap();
-            let events = rx.recv_timeout(Duration::from_millis(800)).unwrap();
+            let events = rx.recv_timeout(Duration::from_secs(2)).unwrap();
             let event = events.last().unwrap();
             assert_eq!(event.path, path.join("new-file"));
             assert!(event.flags.contains(StreamFlags::ITEM_CREATED));
@@ -368,6 +367,7 @@ mod tests {
     fn test_event_stream_shutdown_by_dropping_handle() {
         let dir = TempDir::new("test-event-stream").unwrap();
         let path = dir.path().canonicalize().unwrap();
+        flush_historical_events();
 
         let (tx, rx) = mpsc::channel();
         let (stream, handle) = EventStream::new(&[&path], Duration::from_millis(50));
@@ -401,4 +401,13 @@ mod tests {
         // This returns immediately because the handle was already dropped.
         stream.run(|_| true);
     }
+
+    fn flush_historical_events() {
+        let duration = if std::env::var("CI").is_ok() {
+            Duration::from_secs(2)
+        } else {
+            Duration::from_millis(500)
+        };
+        thread::sleep(duration);
+    }
 }