-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtests.rs
197 lines (171 loc) · 5.36 KB
/
tests.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
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::time::Duration;
use super::{Callback, Watcher};
use tempfile::TempDir;
impl Callback for mpsc::Sender<()> {
fn file_reload(&self) {
self.send(()).unwrap();
}
fn file_change(&self, _: String) {
self.send(()).unwrap();
}
}
fn touch(file: &Path) {
let now = filetime::FileTime::now();
filetime::set_file_mtime(file, now).unwrap();
}
#[derive(Clone)]
struct Delays {
delay: Duration,
short_timeout: Duration,
long_timeout: Duration,
}
impl Delays {
fn new() -> Self {
Self {
delay: Duration::from_millis(75),
short_timeout: Duration::from_millis(25),
long_timeout: Duration::from_millis(1_500),
}
}
fn increase_delays(&mut self) {
self.delay *= 2;
self.short_timeout *= 2;
self.long_timeout += Duration::from_secs(1);
}
fn delay(&self) {
std::thread::sleep(self.delay);
}
#[track_caller]
fn assert_no_message(&self, callback: &mpsc::Receiver<()>) {
assert!(callback.recv_timeout(self.short_timeout).is_err());
}
#[track_caller]
fn assert_at_least_one_message(&self, callback: &mpsc::Receiver<()>) {
assert!(callback.recv_timeout(self.long_timeout).is_ok());
while callback.recv_timeout(self.short_timeout).is_ok() {}
}
}
fn init_test_env() -> (TestEnv, TempDir) {
// Create our dummy test env
let temp_dir = tempfile::Builder::new()
.prefix("inlyne-tests-")
.tempdir()
.unwrap();
let base = temp_dir.path();
let main_file = base.join("main.md");
let rel_file = base.join("rel.md");
fs::write(&main_file, "# Main\n\n[rel](./rel.md)").unwrap();
fs::write(&rel_file, "# Rel").unwrap();
// Setup our watcher
let (callback_tx, callback_rx) = mpsc::channel();
let watcher = Watcher::spawn_inner(callback_tx, main_file.clone());
let test_env = TestEnv {
base_dir: temp_dir.path().to_owned(),
main_file,
rel_file,
watcher,
callback_rx,
};
(test_env, temp_dir)
}
struct TestEnv {
base_dir: PathBuf,
main_file: PathBuf,
rel_file: PathBuf,
watcher: Watcher,
callback_rx: mpsc::Receiver<()>,
}
macro_rules! gen_watcher_test {
( $( ($test_name:ident, $test_fn:ident) ),* $(,)? ) => {
$(
#[test]
fn $test_name() {
$crate::test_utils::log::init();
// Give the test a few chances
let mut last_panic = None;
let mut delays = Delays::new();
for _ in 0..4 {
let result = std::panic::catch_unwind(|| {
let (test_env, _temp_dir) = init_test_env();
// Give the watcher time to get comfy :)
delays.delay();
// For some reason it looks like MacOS gets a create event even though the
// watcher gets registered after the file is already created. Drain any
// initial notifications to start
while test_env.callback_rx.recv_timeout(delays.short_timeout).is_ok() {}
$test_fn(test_env, delays.clone())
});
let Err(panic) = result else {
return;
};
last_panic = Some(panic);
delays.increase_delays();
}
std::panic::resume_unwind(last_panic.unwrap());
}
)*
}
}
gen_watcher_test!(
(sanity, sanity_fn),
(update_moves_watcher, update_moves_watcher_fn),
(slowly_swap_file, slowly_swap_file_fn),
);
fn sanity_fn(
TestEnv {
main_file,
callback_rx,
..
}: TestEnv,
delays: Delays,
) {
// Sanity check watching
touch(&main_file);
delays.assert_at_least_one_message(&callback_rx);
}
fn update_moves_watcher_fn(
TestEnv {
main_file,
rel_file,
watcher,
callback_rx,
..
}: TestEnv,
delays: Delays,
) {
// Updating a file follows the new file and not the old one
watcher.update_file(&rel_file, fs::read_to_string(&rel_file).unwrap());
delays.assert_at_least_one_message(&callback_rx);
touch(&main_file);
delays.assert_no_message(&callback_rx);
touch(&rel_file);
delays.assert_at_least_one_message(&callback_rx);
}
fn slowly_swap_file_fn(
TestEnv {
base_dir,
callback_rx,
main_file,
..
}: TestEnv,
delays: Delays,
) {
let swapped_in_file = base_dir.join("swap_me_in.md");
let swapped_out_file = base_dir.join("swap_out_to_me.md");
fs::write(&swapped_in_file, "# Swapped").unwrap();
// We can slowly swap out the file and it will only follow the file it's supposed to
fs::rename(&main_file, &swapped_out_file).unwrap();
touch(&swapped_out_file);
delays.assert_no_message(&callback_rx);
// The "slowly" part of this (give the watcher time to fail and start polling)
delays.delay();
fs::rename(&swapped_in_file, &main_file).unwrap();
delays.assert_at_least_one_message(&callback_rx);
fs::remove_file(&swapped_out_file).unwrap();
delays.assert_no_message(&callback_rx);
touch(&main_file);
delays.assert_at_least_one_message(&callback_rx);
}