diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-06-08 14:57:56 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-07-03 10:53:20 -0300 |
| commit | e2ab0eba4f7098eecb77a3494010f39b9d015a67 (patch) | |
| tree | b1aa5536ab1b8f51f597c38eec3730802a3da0cb /mullvad-tests/src/bin | |
| parent | 88f9f4ced3c3245c0c0f3a1d10f87bb05b8ecd1b (diff) | |
| download | mullvadvpn-e2ab0eba4f7098eecb77a3494010f39b9d015a67.tar.xz mullvadvpn-e2ab0eba4f7098eecb77a3494010f39b9d015a67.zip | |
Refactor file notification monitoring in tests
Create a `PathWatcher` helper type that presents itself as an `Iterator`
over events on a specified path. It also allows setting a timeout for
waiting for each event, so that it doesn't block indefinately.
Although it watches a specific path, in reality it watches the path's
parent directory, so that it can continue watching the file if it gets
removed and recreated.
Diffstat (limited to 'mullvad-tests/src/bin')
| -rw-r--r-- | mullvad-tests/src/bin/mock_openvpn.rs | 35 |
1 files changed, 13 insertions, 22 deletions
diff --git a/mullvad-tests/src/bin/mock_openvpn.rs b/mullvad-tests/src/bin/mock_openvpn.rs index ac7e2c2b39..cbc5b6caf6 100644 --- a/mullvad-tests/src/bin/mock_openvpn.rs +++ b/mullvad-tests/src/bin/mock_openvpn.rs @@ -1,30 +1,32 @@ -extern crate notify; +extern crate mullvad_tests; use std::env; use std::fs::{self, File}; use std::io::{self, Read, Write}; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::sync::mpsc; use std::thread; +use std::time::Duration; -use notify::{raw_watcher, RawEvent, RecursiveMode, Watcher}; +use mullvad_tests::{watch_event, PathWatcher}; + +const MAX_EVENT_TIME: Duration = Duration::from_secs(60); fn main() { let (file, path) = create_args_file(); - let path_to_wait_for = path; - let path_to_remove = path_to_wait_for.clone(); let (finished_tx, finished_rx) = mpsc::channel(); + let watcher = PathWatcher::watch(&path).expect("Failed to watch file for events"); write_command_line(file); wait_thread(wait_for_stdin_to_be_closed, finished_tx.clone()); wait_thread( - move || wait_for_file_to_be_deleted(path_to_wait_for), + move || wait_for_file_to_be_deleted(watcher, MAX_EVENT_TIME), finished_tx, ); let _ = finished_rx.recv(); - let _ = fs::remove_file(path_to_remove); + let _ = fs::remove_file(path); } fn create_args_file() -> (File, PathBuf) { @@ -61,19 +63,8 @@ fn wait_for_stdin_to_be_closed() { let _ignore_bytes = io::stdin().bytes().last(); } -fn wait_for_file_to_be_deleted<P: AsRef<Path>>(file: P) { - let file = file.as_ref(); - let (tx, rx) = mpsc::channel(); - - if let Ok(mut watcher) = raw_watcher(tx) { - if watcher.watch(&file, RecursiveMode::NonRecursive).is_ok() { - for event in rx { - if let RawEvent { op: Ok(op), .. } = event { - if op.contains(notify::op::REMOVE) { - break; - } - } - } - } - } +fn wait_for_file_to_be_deleted(mut watcher: PathWatcher, timeout: Duration) { + let _ignore_event = watcher + .set_timeout(timeout) + .find(|&event| event == watch_event::REMOVE); } |
