Skip to content

Commit 938cad2

Browse files
committed
wip: test delayed write to file in unittests
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 0b86727 commit 938cad2

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

plotly/src/plot.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ impl PartialEq for Plot {
485485

486486
#[cfg(test)]
487487
mod tests {
488-
use std::path::PathBuf;
489-
490488
use serde_json::{json, to_value};
489+
use std::path::PathBuf;
490+
use std::thread;
491491

492492
use super::*;
493493
use crate::Scatter;
@@ -499,6 +499,14 @@ mod tests {
499499
plot
500500
}
501501

502+
fn delay_file_check() {
503+
#[cfg(target_os = "linux")]
504+
let delay = std::time::Duration::from_millis(0);
505+
#[cfg(not(target_os = "linux"))]
506+
let delay = std::time::Duration::from_millis(100);
507+
thread::sleep(delay);
508+
}
509+
502510
#[test]
503511
fn test_inline_plot() {
504512
let plot = create_test_plot();
@@ -686,26 +694,28 @@ mod tests {
686694
assert!(!dst.exists());
687695
}
688696

689-
#[cfg(not(target_os = "windows"))]
697+
// #[cfg(not(target_os = "windows"))]
690698
#[test]
691-
#[ignore] // This seems to fail unpredictably on MacOs.
699+
// #[ignore] // This seems to fail unpredictably on MacOs.
692700
#[cfg(feature = "kaleido")]
693701
fn test_save_to_eps() {
694702
let plot = create_test_plot();
695703
let dst = PathBuf::from("example.eps");
696704
plot.write_image(&dst, ImageFormat::EPS, 1024, 680, 1.0);
705+
delay_file_check();
697706
assert!(dst.exists());
698707
assert!(std::fs::remove_file(&dst).is_ok());
699708
assert!(!dst.exists());
700709
}
701710

702-
#[cfg(not(target_os = "windows"))]
711+
// #[cfg(not(target_os = "windows"))]
703712
#[test]
704713
#[cfg(feature = "kaleido")]
705714
fn test_save_to_pdf() {
706715
let plot = create_test_plot();
707716
let dst = PathBuf::from("example.pdf");
708717
plot.write_image(&dst, ImageFormat::PDF, 1024, 680, 1.0);
718+
delay_file_check();
709719
assert!(dst.exists());
710720
assert!(std::fs::remove_file(&dst).is_ok());
711721
assert!(!dst.exists());

plotly_kaleido/src/lib.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ impl Kaleido {
183183

184184
#[cfg(test)]
185185
mod tests {
186-
use std::path::PathBuf;
187-
188186
use serde_json::{json, to_value};
187+
use std::path::PathBuf;
188+
use std::thread;
189189

190190
use super::*;
191191

@@ -218,6 +218,14 @@ mod tests {
218218
.unwrap()
219219
}
220220

221+
fn delay_file_check() {
222+
#[cfg(target_os = "linux")]
223+
let delay = std::time::Duration::from_millis(0);
224+
#[cfg(not(target_os = "linux"))]
225+
let delay = std::time::Duration::from_millis(100);
226+
thread::sleep(delay);
227+
}
228+
221229
#[test]
222230
fn test_can_find_kaleido_executable() {
223231
let _k = Kaleido::new();
@@ -238,62 +246,67 @@ mod tests {
238246
assert_eq!(to_value(kaleido_data).unwrap(), expected);
239247
}
240248

241-
#[cfg(not(target_os = "windows"))]
249+
// #[cfg(not(target_os = "windows"))]
242250
#[test]
243251
fn test_save_png() {
244252
let test_plot = create_test_plot();
245253
let k = Kaleido::new();
246254
let dst = PathBuf::from("example.png");
247255
let r = k.save(dst.as_path(), &test_plot, "png", 1200, 900, 4.5);
248256
assert!(r.is_ok());
257+
delay_file_check();
249258
assert!(std::fs::remove_file(dst.as_path()).is_ok());
250259
}
251260

252-
#[cfg(not(target_os = "windows"))]
261+
// #[cfg(not(target_os = "windows"))]
253262
#[test]
254263
fn test_save_jpeg() {
255264
let test_plot = create_test_plot();
256265
let k = Kaleido::new();
257266
let dst = PathBuf::from("example.jpeg");
258267
let r = k.save(dst.as_path(), &test_plot, "jpeg", 1200, 900, 4.5);
259268
assert!(r.is_ok());
269+
delay_file_check();
260270
assert!(std::fs::remove_file(dst.as_path()).is_ok());
261271
}
262272

263-
#[cfg(not(target_os = "windows"))]
273+
// #[cfg(not(target_os = "windows"))]
264274
#[test]
265275
fn test_save_webp() {
266276
let test_plot = create_test_plot();
267277
let k = Kaleido::new();
268278
let dst = PathBuf::from("example.webp");
269279
let r = k.save(dst.as_path(), &test_plot, "webp", 1200, 900, 4.5);
270280
assert!(r.is_ok());
281+
delay_file_check();
271282
assert!(std::fs::remove_file(dst.as_path()).is_ok());
272283
}
273284

274-
#[cfg(not(target_os = "windows"))]
285+
// #[cfg(not(target_os = "windows"))]
275286
#[test]
276287
fn test_save_svg() {
277288
let test_plot = create_test_plot();
278289
let k = Kaleido::new();
279290
let dst = PathBuf::from("example.svg");
280291
let r = k.save(dst.as_path(), &test_plot, "svg", 1200, 900, 4.5);
281292
assert!(r.is_ok());
293+
delay_file_check();
282294
assert!(std::fs::remove_file(dst.as_path()).is_ok());
283295
}
284296

285-
#[cfg(not(target_os = "windows"))]
297+
// #[cfg(not(target_os = "windows"))]
286298
#[test]
287299
fn test_save_pdf() {
288300
let test_plot = create_test_plot();
289301
let k = Kaleido::new();
290302
let dst = PathBuf::from("example.pdf");
291303
let r = k.save(dst.as_path(), &test_plot, "pdf", 1200, 900, 4.5);
292304
assert!(r.is_ok());
305+
delay_file_check();
293306
assert!(std::fs::remove_file(dst.as_path()).is_ok());
294307
}
295308

296-
#[cfg(not(target_os = "windows"))]
309+
// #[cfg(not(target_os = "windows"))]
297310
#[test]
298311
#[ignore]
299312
fn test_save_eps() {
@@ -302,6 +315,7 @@ mod tests {
302315
let dst = PathBuf::from("example.eps");
303316
let r = k.save(dst.as_path(), &test_plot, "eps", 1200, 900, 4.5);
304317
assert!(r.is_ok());
318+
delay_file_check();
305319
assert!(std::fs::remove_file(dst.as_path()).is_ok());
306320
}
307321
}

0 commit comments

Comments
 (0)