oxedyne/fe2o3/fe2o3_mail/tests/qa_stress.rs
5.6 KiB, 1 run
created by r1870400018:74952, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! QA F2a: processes and threads deliver, flag, expunge and SELECT one folder; every UID seen |
| 2 | //! must name one message, APPEND's UID must be its own, UIDVALIDITY must not move. |
| 3 | //! From QA F2a's `qa_stress.rs` (D-20261026-01), ported to update_flags and the Option of |
| 4 | //! fetch_bytes: 4 processes (this test binary re-run as `qa_child`) of 3 threads each. |
| 5 | use oxedyne_fe2o3_core::prelude::*; |
| 6 | use oxedyne_fe2o3_mail::maildir::MaildirStore; |
| 7 | use oxedyne_fe2o3_net::mail::store::{FlagUpdate, FolderName, MailStore, MailUser, MessageFlags}; |
| 8 | use std::{collections::BTreeMap, fs, path::PathBuf, process::Command, thread}; |
| 9 | |
| 10 | fn user() -> MailUser { |
| 11 | MailUser { local: fmt!("alice"), domain: fmt!("test.local"), delivery_key: fmt!("test.local/alice"), send_as: Vec::new() } |
| 12 | } |
| 13 | fn body(tag: &str) -> Vec<u8> { fmt!("Subject: {}\r\n\r\nid={}\r\n", tag, tag).into_bytes() } |
| 14 | fn tag_of(b: &[u8]) -> String { |
| 15 | let s = String::from_utf8_lossy(b); |
| 16 | match s.find("id=") { Some(i) => s[i + 3..].trim().to_string(), None => String::new() } |
| 17 | } |
| 18 | |
| 19 | /// One worker: returns lines "uid tag" it observed or was given by append, and "V v". |
| 20 | fn work(root: PathBuf, who: String, rounds: usize) -> Outcome<Vec<String>> { |
| 21 | let store = res!(MaildirStore::new(root.clone(), "argonaut")); |
| 22 | let u = user(); |
| 23 | let inbox = FolderName::new("INBOX"); |
| 24 | let mut seen = Vec::new(); |
| 25 | for i in 0..rounds { |
| 26 | let tag = fmt!("{}-{}", who, i); |
| 27 | let uid = res!(store.append(&u, &inbox, &body(&tag), MessageFlags::default(), None)); |
| 28 | seen.push(fmt!("A {} {}", uid.0, tag)); |
| 29 | // An external lock-free MDA delivery into new/ as well. |
| 30 | let ib = root.join("test.local/alice"); |
| 31 | let n = fmt!("1800000000.M{}P{}X{}.ext", i, std::process::id(), who); |
| 32 | res!(fs::write(ib.join("tmp").join(&n), body(&fmt!("{}-ext{}", who, i))), IO, File); |
| 33 | res!(fs::rename(ib.join("tmp").join(&n), ib.join("new").join(&n)), IO, File); |
| 34 | let msgs = res!(store.list_messages(&u, &inbox, i % 2 == 0)); // SELECT moves new/ to cur/ |
| 35 | for m in &msgs { |
| 36 | match res!(store.fetch_bytes(&u, &inbox, m.uid)) { |
| 37 | Some(b) => seen.push(fmt!("S {} {}", m.uid.0, tag_of(&b))), |
| 38 | None => (), // Expunged since the listing. |
| 39 | } |
| 40 | // Delete every third message whose tag is one of ours, then expunge. |
| 41 | if m.uid.0 % 3 == 0 { |
| 42 | let f = MessageFlags { deleted: true, ..MessageFlags::default() }; |
| 43 | let _ = res!(store.update_flags(&u, &inbox, m.uid, FlagUpdate::Add(f))); |
| 44 | } |
| 45 | } |
| 46 | res!(store.expunge(&u, &inbox, None)); |
| 47 | let st = res!(store.folder_status(&u, &inbox)); |
| 48 | seen.push(fmt!("V {} {}", st.uid_validity, st.uid_next)); |
| 49 | } |
| 50 | Ok(seen) |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn qa_child() -> Outcome<()> { |
| 55 | let root = match std::env::var("QA_STRESS_ROOT") { Ok(r) => PathBuf::from(r), Err(_) => return Ok(()) }; |
| 56 | let who = res!(std::env::var("QA_STRESS_WHO"), Missing); |
| 57 | let out = res!(std::env::var("QA_STRESS_OUT"), Missing); |
| 58 | let mut lines = Vec::new(); |
| 59 | let hs: Vec<_> = (0..3).map(|t| { let r = root.clone(); let w = fmt!("{}t{}", who, t); thread::spawn(move || work(r, w, 25)) }).collect(); |
| 60 | for h in hs { match h.join() { Ok(Ok(l)) => lines.extend(l), Ok(Err(e)) => { println!("QA_CHILD_ERR {}", e); return Err(e) }, Err(_) => return Err(err!("panic"; Bug)) } } |
| 61 | res!(fs::write(out, lines.join("\n")), IO, File); |
| 62 | Ok(()) |
| 63 | } |
| 64 | |
| 65 | /// About 4 min in a debug build, so ignored. Run it alone: |
| 66 | /// `cargo test -p oxedyne_fe2o3_mail --test qa_stress -- --ignored --nocapture qa_stress_processes`. |
| 67 | /// Before the fair ExclusiveLock wait (2ed9d149) 1 to 3 of the 12 workers failed with File Lock |
| 68 | /// Timeout after 30 s, passed over by polling; after it, three runs gave uids=600 bad=0. |
| 69 | #[test] |
| 70 | #[ignore] |
| 71 | fn qa_stress_processes() -> Outcome<()> { |
| 72 | if std::env::var("QA_STRESS_ROOT").is_ok() { return Ok(()); } |
| 73 | let root = std::env::temp_dir().join(fmt!("qa_f2a_stress_{}", std::process::id())); |
| 74 | let _ = fs::remove_dir_all(&root); |
| 75 | res!(fs::create_dir_all(&root), IO, File); |
| 76 | let store = res!(MaildirStore::new(root.clone(), "argonaut")); |
| 77 | res!(store.ensure_user(&user())); |
| 78 | let exe = res!(std::env::current_exe(), IO); |
| 79 | let mut kids = Vec::new(); |
| 80 | for p in 0..4 { |
| 81 | let out = root.join(fmt!("out{}", p)); |
| 82 | let c = res!(Command::new(&exe).args(["qa_child", "--exact", "--nocapture"]) |
| 83 | .env("QA_STRESS_ROOT", &root).env("QA_STRESS_WHO", fmt!("p{}", p)).env("QA_STRESS_OUT", &out).spawn(), IO); |
| 84 | kids.push((c, out)); |
| 85 | } |
| 86 | let mut by_uid: BTreeMap<u32, String> = BTreeMap::new(); |
| 87 | let mut vals = std::collections::BTreeSet::new(); |
| 88 | let mut bad = Vec::new(); |
| 89 | for (mut c, out) in kids { |
| 90 | let st = res!(c.wait(), IO); |
| 91 | if !st.success() { bad.push(fmt!("child failed {:?}", st)); continue; } |
| 92 | let s = res!(fs::read_to_string(&out), IO); |
| 93 | for l in s.lines() { |
| 94 | let t: Vec<&str> = l.split(' ').collect(); |
| 95 | if t[0] == "V" { vals.insert(t[1].to_string()); continue; } |
| 96 | let uid: u32 = res!(t[1].parse::<u32>().map_err(|e| err!(e, "uid"; Invalid))); |
| 97 | match by_uid.get(&uid) { |
| 98 | Some(prev) if prev != t[2] => bad.push(fmt!("UID {} named {} and {}", uid, prev, t[2])), |
| 99 | Some(_) => (), |
| 100 | None => { by_uid.insert(uid, t[2].to_string()); }, |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | println!("uids={} validities={:?} bad={}", by_uid.len(), vals, bad.len()); |
| 105 | for b in bad.iter().take(10) { println!("{}", b); } |
| 106 | let _ = fs::remove_dir_all(&root); |
| 107 | if !bad.is_empty() || vals.len() != 1 { return Err(err!("{} conflicts, {} validities", bad.len(), vals.len(); Test)); } |
| 108 | Ok(()) |
| 109 | } |