Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/imap_runtime.rs

8.1 KiB, 1 run

created by r1870400018:74752, 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//! A session waiting on a Maildir folder lock must not hold the async runtime.
2//!
3//! Every folder read in `MaildirStore` waits on the folder's flock. Run on a runtime
4//! worker, that wait takes the worker with it, and on a single-worker runtime nothing
5//! else moves: not another session on another folder, not a plain timer. Steel serves
6//! its HTTPS side from the same runtime, so the whole server stalls.
7
8use oxedyne_fe2o3_core::{
9 file::ExclusiveLock,
10 prelude::*,
11};
12use oxedyne_fe2o3_mail::maildir::MaildirStore;
13use oxedyne_fe2o3_net::{
14 imap::server::ImapServer,
15 mail::{
16 store::{
17 FolderName,
18 MailStore,
19 MailUser,
20 },
21 user::UserStore,
22 },
23};
24
25use std::{
26 fs,
27 path::PathBuf,
28 sync::mpsc,
29 time::Duration,
30};
31
32use tokio::{
33 io::{
34 AsyncBufReadExt,
35 AsyncWriteExt,
36 BufReader,
37 },
38 net::{
39 TcpListener,
40 TcpStream,
41 },
42};
43
44
45const USER: &str = "alice@test.local";
46const PASS: &str = "correct-horse";
47
48#[derive(Clone, Debug)]
49struct OneUser;
50
51impl UserStore for OneUser {
52 fn authenticate(&self, address: &str, password: &str) -> Outcome<Option<MailUser>> {
53 if address.eq_ignore_ascii_case(USER) && password == PASS {
54 return self.lookup(address);
55 }
56 Ok(None)
57 }
58
59 fn lookup(&self, address: &str) -> Outcome<Option<MailUser>> {
60 if !address.eq_ignore_ascii_case(USER) { return Ok(None); }
61 Ok(Some(MailUser {
62 local: fmt!("alice"),
63 domain: fmt!("test.local"),
64 delivery_key: fmt!("test.local/alice"),
65 send_as: Vec::new(),
66 }))
67 }
68}
69
70/// Sends `text` under `tag` and reads to the tagged reply, which it returns.
71async fn command(
72 r: &mut BufReader<tokio::net::tcp::OwnedReadHalf>,
73 w: &mut tokio::net::tcp::OwnedWriteHalf,
74 tag: &str,
75 text: &str,
76)
77 -> Outcome<String>
78{
79 res!(w.write_all(fmt!("{} {}\r\n", tag, text).as_bytes()).await, IO, Network);
80 loop {
81 let mut line = String::new();
82 let n = res!(r.read_line(&mut line).await, IO, Network);
83 if n == 0 {
84 return Err(err!("The server closed before answering {}.", tag; IO, Network));
85 }
86 if line.starts_with(&fmt!("{} ", tag)) {
87 return Ok(line.trim_end().to_string());
88 }
89 }
90}
91
92async fn connect(addr: std::net::SocketAddr)
93 -> Outcome<(BufReader<tokio::net::tcp::OwnedReadHalf>, tokio::net::tcp::OwnedWriteHalf)>
94{
95 let sock = res!(TcpStream::connect(addr).await, IO, Network);
96 let (r, w) = sock.into_split();
97 let mut r = BufReader::new(r);
98 let mut greeting = String::new();
99 res!(r.read_line(&mut greeting).await, IO, Network);
100 Ok((r, w))
101}
102
103#[derive(Debug)]
104enum Event {
105 Ticked, // a plain timer task ran
106 OtherFolder(String), // the second session's STATUS INBOX reply
107 LockedFolder(String), // the first session's SELECT reply, once the lock is gone
108 Failed(String),
109}
110
111/// Session A's SELECT, the second session and the timer all share one runtime thread.
112fn serve_on_one_thread(store: MaildirStore, tx: mpsc::Sender<Event>) -> Outcome<()> {
113 let rt = res!(tokio::runtime::Builder::new_current_thread().enable_all().build(), IO);
114 rt.block_on(async move {
115 let listener = res!(TcpListener::bind("127.0.0.1:0").await, IO, Network);
116 let addr = res!(listener.local_addr(), IO, Network);
117 let server = ImapServer {
118 store,
119 users: OneUser,
120 hostname: std::sync::Arc::new(fmt!("test.local")),
121 };
122 tokio::spawn(async move {
123 while let Ok((sock, peer)) = listener.accept().await {
124 let srv = server.clone();
125 tokio::spawn(async move { let _ = srv.run(sock, peer).await; });
126 }
127 });
128
129 // Session A logs in and then waits on the locked folder.
130 let (mut ra, mut wa) = res!(connect(addr).await);
131 res!(command(&mut ra, &mut wa, "a1", &fmt!("LOGIN {} {}", USER, PASS)).await);
132 let tx_a = tx.clone();
133 let a = tokio::spawn(async move {
134 let ev = match command(&mut ra, &mut wa, "a2", "SELECT Projects").await {
135 Ok(line) => Event::LockedFolder(line),
136 Err(e) => Event::Failed(fmt!("session A: {}", e)),
137 };
138 let _ = tx_a.send(ev);
139 });
140 // Give A's SELECT time to reach the store and block.
141 tokio::time::sleep(Duration::from_millis(300)).await;
142
143 let tx_t = tx.clone();
144 tokio::spawn(async move {
145 tokio::time::sleep(Duration::from_millis(10)).await;
146 let _ = tx_t.send(Event::Ticked);
147 });
148 let ev = match connect(addr).await {
149 Ok((mut rb, mut wb)) => {
150 match command(&mut rb, &mut wb, "b1", &fmt!("LOGIN {} {}", USER, PASS)).await {
151 Ok(_) => match command(&mut rb, &mut wb, "b2", "STATUS INBOX (MESSAGES)").await {
152 Ok(line) => Event::OtherFolder(line),
153 Err(e) => Event::Failed(fmt!("session B STATUS: {}", e)),
154 },
155 Err(e) => Event::Failed(fmt!("session B LOGIN: {}", e)),
156 }
157 }
158 Err(e) => Event::Failed(fmt!("session B connect: {}", e)),
159 };
160 let _ = tx.send(ev);
161 let _ = a.await;
162 Ok(())
163 })
164}
165
166#[test]
167fn a_session_waiting_on_a_folder_lock_holds_no_runtime_worker() -> Outcome<()> {
168 let root = std::env::temp_dir().join(fmt!("fe2o3_imap_runtime_{}", std::process::id()));
169 let _ = fs::remove_dir_all(&root);
170 res!(fs::create_dir_all(&root), IO, File);
171 let store = res!(MaildirStore::new(PathBuf::from(&root), "test.local"));
172 let user = match res!(OneUser.lookup(USER)) {
173 Some(u) => u,
174 None => return Err(err!("The test user did not resolve."; Test, Bug)),
175 };
176 res!(store.ensure_user(&user));
177 let projects = FolderName::new("Projects");
178 res!(store.create_folder(&user, &projects));
179
180 // Another holder of the folder lock: a delivery or another process, as the store sees it.
181 let lock_path = root.join("test.local").join("alice").join(".Projects").join("hematite-folder.lock");
182 let lock = res!(ExclusiveLock::acquire(&lock_path));
183
184 let (tx, rx) = mpsc::channel::<Event>();
185 let worker = std::thread::spawn(move || serve_on_one_thread(store, tx));
186
187 // While the lock is held, the other session and the timer must both get through.
188 let mut ticked = false;
189 let mut other: Option<String> = None;
190 let mut failures: Vec<String> = Vec::new();
191 while !(ticked && other.is_some()) {
192 match rx.recv_timeout(Duration::from_secs(5)) {
193 Ok(Event::Ticked) => ticked = true,
194 Ok(Event::OtherFolder(l)) => other = Some(l),
195 Ok(ev) => { failures.push(fmt!("while locked: {:?}", ev)); break; }
196 Err(_) => {
197 failures.push(fmt!("while the folder was locked, nothing else ran \
198 (timer ticked: {}, other session: {:?})", ticked, other));
199 break;
200 }
201 }
202 }
203 if let Some(line) = &other {
204 if !line.contains(" OK ") {
205 failures.push(fmt!("STATUS INBOX while Projects was locked: {}", line));
206 }
207 }
208
209 // Once the lock goes, the waiting session finishes its SELECT.
210 drop(lock);
211 let mut selected = false;
212 while !selected {
213 match rx.recv_timeout(Duration::from_secs(10)) {
214 Ok(Event::LockedFolder(l)) => {
215 if !l.contains(" OK ") { failures.push(fmt!("SELECT Projects: {}", l)); }
216 selected = true;
217 }
218 Ok(Event::Failed(e)) => { failures.push(e); break; }
219 Ok(_) => (),
220 Err(_) => { failures.push(fmt!("SELECT never finished after the lock went")); break; }
221 }
222 }
223 let _ = worker.join();
224 let _ = fs::remove_dir_all(&root);
225 assert!(failures.is_empty(), "{}", failures.join("\n"));
226 Ok(())
227}