From 24e0308c0d84180ad41f711563d6c85a24c6d4e8 Mon Sep 17 00:00:00 2001 From: Samuel Guerra Date: Tue, 30 May 2023 18:03:41 -0300 Subject: [PATCH] More tests. --- TODO/_current.md | 6 ------ tests/config.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/TODO/_current.md b/TODO/_current.md index bb8b3c904..8ebf6e9dd 100644 --- a/TODO/_current.md +++ b/TODO/_current.md @@ -33,9 +33,3 @@ * Review wait, how do handles work when request is not found yet? * Test live update of localization file. - -# Config - -* Review save debounce. -* Test concurrent access to same config. - - Use multiple app threads. \ No newline at end of file diff --git a/tests/config.rs b/tests/config.rs index 9db6c8f53..49feae200 100644 --- a/tests/config.rs +++ b/tests/config.rs @@ -205,3 +205,49 @@ fn test_core_border() { test_config!(BorderStyle::Dotted); test_config!(BorderSides::new_all(colors::RED)); } + +#[test] +fn concurrent_read_write() { + let file = PathBuf::from("../target/tmp/test.concurrent.json"); + + { + // setup + let _ = std::fs::remove_file(&file); + let mut app = App::default().run_headless(false); + CONFIG.load(JsonConfig::sync(&file)); + CONFIG.get("key", || Txt::from_static("default")).set("custom").unwrap(); + + app.run_task(async { + task::with_deadline(CONFIG.wait_idle(), 5.secs()).await.unwrap(); + }); + let status = CONFIG.status().get(); + if status.is_err() { + panic!("{status}"); + } + } + + // tests + let threads: Vec<_> = (0..32) + .map(|_| { + std::thread::spawn(clmv!(file, || { + let mut app = App::default().run_headless(false); + CONFIG.load(JsonConfig::sync(file)); + + app.run_task(async { + task::with_deadline(CONFIG.wait_idle(), 5.secs()).await.unwrap(); + }); + + let var = CONFIG.get("key", || Txt::from_static("default")); + for _ in 0..8 { + assert_eq!("custom", var.get()); + var.set("custom").unwrap(); + app.update(false).assert_wait(); + } + })) + }) + .collect(); + + for t in threads { + t.join().unwrap(); + } +}