Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/publish_outbox.rs

54.4 KiB, 28 runs

created by r1870400018:68860, 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//! The paced outbox: a sign-up queues its confirmation and answers, the drainer sends at the host's
2//! ceiling and no faster, and nobody is sent to who is no longer owed it.
3//!
4//! These run the drainer's own `step` against a real Ozone instance with a recording sender and a
5//! clock the test sets, so an hour passes in an instant and no mail is dialled.
6//!
7//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
8//! Anthropic Claude
9
10use oxedyne_fe2o3_core::prelude::*;
11use oxedyne_fe2o3_iop_db::api::Database;
12use oxedyne_fe2o3_jdat::prelude::*;
13use oxedyne_fe2o3_net::{
14 http::fields::HeaderFields,
15 imap::client::Security,
16 smtp::client::{
17 Dial,
18 OutboundClient,
19 SubmissionConfig,
20 },
21};
22use oxedyne_fe2o3_steel::srv::{
23 alert::AlertEvent,
24 id,
25 publish::{
26 Markup,
27 PostState,
28 PublishConfig,
29 Source,
30 outbox::{
31 self,
32 Courier,
33 Entry,
34 Kind,
35 Pacer,
36 Share,
37 Stats,
38 Step,
39 Watch,
40 },
41 rate::Window,
42 send::{
43 self,
44 MailSender,
45 },
46 store::{
47 self,
48 Record,
49 },
50 subscribe::{
51 self,
52 SubState,
53 Subscriber,
54 },
55 },
56};
57
58use std::{
59 future::Future,
60 net::{
61 IpAddr,
62 Ipv4Addr,
63 },
64 sync::{
65 Arc,
66 Mutex,
67 RwLock,
68 },
69 time::{
70 Duration,
71 Instant,
72 },
73};
74
75mod common;
76
77const DAY: u64 = 86_400;
78
79const SITE: &str = "https://site.test";
80const HOUR_MS: u64 = 3_600_000;
81const T0: u64 = 1_800_000_000_000;
82
83const HANG_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1));
84const FINE_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 2));
85const EVIL_IP: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 3));
86
87type Handle = (Arc<RwLock<common::TestDb>>, id::Uid);
88
89// A sender that records who it was asked to send to and dials nobody.
90#[derive(Default)]
91struct Fake {
92 to: Mutex<Vec<String>>,
93}
94
95impl Fake {
96 fn count(&self) -> usize {
97 self.to.lock().map(|v| v.len()).unwrap_or(0)
98 }
99}
100
101impl Courier for Fake {
102
103 fn default_from(&self) -> &str {
104 "news@site.test"
105 }
106
107 fn deliver(&self, _from: &str, to: &str, _msg: &str, _dial: &Dial)
108 -> impl Future<Output = Outcome<String>> + Send
109 {
110 let to = to.to_string();
111 async move {
112 let mut v = lock_mutex!(self.to);
113 v.push(to);
114 Ok(fmt!("q1"))
115 }
116 }
117}
118
119// A courier whose far end never answers for one host and accepts for every other, and which notes
120// each address it was asked to deliver to.
121#[derive(Default)]
122struct Mute {
123 asked: Mutex<Vec<String>>,
124}
125
126impl Courier for Mute {
127
128 fn default_from(&self) -> &str {
129 "news@site.test"
130 }
131
132 fn exchanges(&self, to: &str) -> impl Future<Output = Outcome<Vec<IpAddr>>> + Send {
133 let ip = if to.ends_with("@hang.test") { HANG_IP } else { FINE_IP };
134 async move { Ok(vec![ip]) }
135 }
136
137 fn deliver(&self, _from: &str, to: &str, _msg: &str, dial: &Dial)
138 -> impl Future<Output = Outcome<String>> + Send
139 {
140 let to = to.to_string();
141 async move {
142 {
143 let mut v = lock_mutex!(self.asked);
144 v.push(to.clone());
145 }
146 if to.ends_with("@hang.test") {
147 res!(dial.dialling(HANG_IP));
148 std::future::pending::<()>().await;
149 }
150 res!(dial.dialling(FINE_IP));
151 Ok(fmt!("q1"))
152 }
153 }
154}
155
156fn cfg() -> PublishConfig {
157 PublishConfig {
158 path: fmt!("/news"),
159 base_url: fmt!("{}", SITE),
160 source: Source::Store,
161 ..Default::default()
162 }
163}
164
165// `n` pending subscribers, each with a confirmation queued and due at once.
166fn queue_pending(db: &Handle, n: usize) -> Outcome<Vec<String>> {
167 let mut emails = Vec::new();
168 let mut entries = Vec::new();
169 for i in 0..n {
170 let email = fmt!("p{}@site.test", i);
171 res!(subscribe::add_pending(db, &email, &Window::default(), 1));
172 entries.push(Entry::new(Kind::Confirm, &email, "", 0));
173 emails.push(email);
174 }
175 res!(outbox::push(db, &entries));
176 Ok(emails)
177}
178
179// Runs the drainer for one simulated hour from `T0`, jumping the clock to each slot it is told of.
180async fn run_hour(db: &Handle, fake: &Fake, pacer: &Pacer) -> Outcome<()> {
181 let cfg = cfg();
182 let (mut now, mut share) = (T0, Share::default());
183 loop {
184 match res!(outbox::step(db, &cfg, fake, pacer, &mut share, "test", now).await) {
185 Step::Sent | Step::Worked => {},
186 Step::Wait(ms) => {
187 if now + ms >= T0 + HOUR_MS {
188 break;
189 }
190 now += ms;
191 },
192 _ => break,
193 }
194 }
195 Ok(())
196}
197
198#[tokio::test]
199async fn test_a_signup_queues_its_confirmation_and_sends_nothing_00() -> Outcome<()> {
200 let (db, uid, _tmp) = res!(common::test_db());
201 let handle = (db, uid);
202 let sender = res!(MailSender::new("mail.site.test".to_string(), Vec::new(), "news@site.test".to_string()));
203 let mail = Some(Arc::new(sender));
204 let _ = res!(subscribe::handle_subscribe(
205 &cfg(), Some(&handle), &mail, &HeaderFields::default(), b"email=a%40nowhere.invalid", None, "test",
206 ).await);
207 // The request wrote one queue entry and no record: the sign-up is applied when the entry is drained.
208 assert!(res!(subscribe::get(&handle, "a@nowhere.invalid")).is_none(), "the request wrote a record");
209 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 1, "the sign-up was not queued");
210 let (fake, pacer) = (Fake::default(), Pacer::new(100));
211 res!(run_hour(&handle, &fake, &pacer).await);
212 let sub = res!(subscribe::get(&handle, "a@nowhere.invalid"));
213 let sub = res!(sub.ok_or_else(|| err!("the drainer stored no subscriber"; Test, Missing)));
214 assert_eq!(sub.state, SubState::Pending);
215 assert!(sub.sent.is_some(), "the confirmation left and no send was recorded");
216 assert_eq!(fake.count(), 1);
217 Ok(())
218}
219
220#[tokio::test]
221async fn test_the_ceiling_bounds_what_the_drainer_sends_in_an_hour_01() -> Outcome<()> {
222 let (db, uid, _tmp) = res!(common::test_db());
223 let handle = (db, uid);
224 let emails = res!(queue_pending(&handle, 10));
225 let (fake, pacer) = (Fake::default(), Pacer::new(3));
226 res!(run_hour(&handle, &fake, &pacer).await);
227 assert_eq!(fake.count(), 3, "a ceiling of 3 an hour did not send exactly 3 in the hour");
228 // The time of the send is on the record, and only on the ones that left.
229 let sent = emails.iter()
230 .filter(|e| matches!(subscribe::get(&handle, e), Ok(Some(s)) if s.sent.is_some()))
231 .count();
232 assert_eq!(sent, 3, "the sent time was not recorded on exactly the three that left");
233 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 7);
234
235 let (fake, pacer) = (Fake::default(), Pacer::new(0));
236 res!(run_hour(&handle, &fake, &pacer).await);
237 assert_eq!(fake.count(), 0, "a ceiling of 0 sent mail");
238 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 7, "a held queue lost entries");
239 Ok(())
240}
241
242#[tokio::test]
243async fn test_a_record_no_longer_pending_is_skipped_02() -> Outcome<()> {
244 let (db, uid, _tmp) = res!(common::test_db());
245 let handle = (db, uid);
246 let emails = res!(queue_pending(&handle, 2));
247 // The first confirms, by its token, before the drainer reaches it.
248 let sub = res!(subscribe::get(&handle, &emails[0]));
249 let sub = res!(sub.ok_or_else(|| err!("no subscriber"; Test, Missing)));
250 let _ = res!(subscribe::confirm(&handle, &sub.token, "test"));
251 let (fake, pacer) = (Fake::default(), Pacer::new(3600));
252 res!(run_hour(&handle, &fake, &pacer).await);
253 let sent = fake.to.lock().map(|v| v.clone()).unwrap_or_default();
254 assert_eq!(sent, vec![emails[1].clone()], "a confirmed address was sent a confirmation");
255 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 0, "the skipped entry stayed queued");
256 Ok(())
257}
258
259#[tokio::test]
260async fn test_a_backlog_is_told_once_and_cleared_once_when_it_drains_03() -> Outcome<()> {
261 let (db, uid, _tmp) = res!(common::test_db());
262 let handle = (db, uid);
263 // Three confirmations queued at T0, behind a ceiling held at 0.
264 let mut entries = Vec::new();
265 for i in 0..3 {
266 let email = fmt!("b{}@site.test", i);
267 res!(subscribe::add_pending(&handle, &email, &Window::default(), 1));
268 entries.push(Entry::new(Kind::Confirm, &email, "", T0 / 1000));
269 }
270 res!(outbox::push(&handle, &entries));
271 let cfg = PublishConfig { outbox_alert_secs: 3600, ..cfg() };
272 let (fake, held, mut watch) = (Fake::default(), Pacer::new(0), Watch::default());
273 let mut share = Share::default();
274 let mut told = Vec::new();
275 // Three hours held, polled every ten minutes.
276 for m in (0..=180u64).step_by(10) {
277 let (_, owed) = res!(outbox::tick(
278 &handle, &cfg, &fake, &held, &mut share, &mut watch, "test", T0 + m * 60_000).await);
279 if let Some(e) = owed {
280 told.push((m, e));
281 }
282 }
283 assert_eq!(told.len(), 1, "a backlog lasting three hours was told {} times, not once", told.len());
284 assert_eq!(told[0].0, 70, "the backlog was not told at the first poll past the threshold");
285 assert!(matches!(&told[0].1, AlertEvent::OutboxBacklog { queued: 3, hourly: 0, .. }),
286 "the backlog told the wrong thing: {:?}", told[0].1);
287 assert_eq!(fake.count(), 0, "a held ceiling sent mail");
288
289 // The ceiling is raised, and the queue drains.
290 let open = Pacer::new(3600);
291 let mut now = T0 + 180 * 60_000;
292 let mut cleared = Vec::new();
293 for _ in 0..20 {
294 let (stepped, owed) = res!(outbox::tick(
295 &handle, &cfg, &fake, &open, &mut share, &mut watch, "test", now).await);
296 if let Some(e) = owed {
297 cleared.push(e);
298 }
299 match stepped {
300 Step::Wait(ms) => now += ms,
301 Step::Idle => break,
302 _ => {},
303 }
304 }
305 assert_eq!(fake.count(), 3, "the released queue did not send all three");
306 assert_eq!(cleared.len(), 1, "draining told {} things, not one", cleared.len());
307 assert!(matches!(&cleared[0], AlertEvent::OutboxCleared { sent: 3, .. }),
308 "draining told the wrong thing: {:?}", cleared[0]);
309 Ok(())
310}
311
312#[tokio::test]
313async fn test_a_newsletter_is_paced_by_the_ceiling_and_waits_behind_confirmations_04() -> Outcome<()> {
314 let (db, uid, _tmp) = res!(common::test_db());
315 let handle = (db, uid);
316 let rec = Record {
317 slug: fmt!("on-rent"),
318 author: String::new(),
319 categories: Vec::new(),
320 state: PostState::Live,
321 markup: Markup::Markdown,
322 date: Some(fmt!("2026-07-17")),
323 source: fmt!("# On rent\n\nAn opening sentence.\n"),
324 deliveries: Vec::new(),
325 tags: Vec::new(),
326 ai_level: None,
327 };
328 res!(store::put(&handle, &rec, &rec.slug, "test"));
329 // Ten confirmed subscribers.
330 for i in 0..10 {
331 let email = fmt!("n{}@site.test", i);
332 res!(subscribe::add_pending(&handle, &email, &Window::default(), 1));
333 let sub = res!(subscribe::get(&handle, &email));
334 let sub = res!(sub.ok_or_else(|| err!("no subscriber"; Test, Missing)));
335 let _ = res!(subscribe::confirm(&handle, &sub.token, "test"));
336 }
337 let sender = res!(MailSender::new(
338 "mail.site.test".to_string(), Vec::new(), "news@site.test".to_string()));
339 let sender = sender.with_outbound_hourly(3);
340 let report = res!(send::send_newsletter(&sender, &handle, "on-rent", "test"));
341 assert_eq!(report.attempted, 10, "the newsletter was not queued for all ten");
342 assert_eq!(res!(outbox::queued(&handle, Kind::News)), 10);
343 // A sign-up queued after the newsletter.
344 res!(subscribe::add_pending(&handle, "late@site.test", &Window::default(), 1));
345 res!(outbox::push(&handle, &[Entry::new(Kind::Confirm, "late@site.test", "", T0 / 1000)]));
346
347 let fake = Fake::default();
348 res!(run_hour(&handle, &fake, sender.pacer()).await);
349 let to = fake.to.lock().map(|v| v.clone()).unwrap_or_default();
350 assert_eq!(to.len(), 3, "a ceiling of 3 an hour sent {} messages in the hour", to.len());
351 assert_eq!(to[0], "late@site.test", "the newsletter went before the confirmation");
352 let hist = res!(send::send_history(&handle));
353 assert_eq!(hist.len(), 1);
354 assert_eq!((hist[0].attempted, hist[0].sent, hist[0].waiting()), (10, 2, 8),
355 "the send's tally did not follow the queue");
356 assert_eq!(res!(outbox::queued(&handle, Kind::News)), 8);
357 Ok(())
358}
359
360// A1: one backlog episode is told once however the head's age wobbles round the threshold. The queue
361// never empties here, so the episode never ends.
362#[test]
363fn test_a_backlog_that_wobbles_round_the_threshold_is_told_once_05() -> Outcome<()> {
364 let mut watch = Watch::default();
365 let (limit, base) = (3600u64, T0 / 1000);
366 let mut told = Vec::new();
367 for i in 0..100u64 {
368 let now = base + i * 36;
369 let age = if i % 2 == 0 { limit + 20 } else { limit - 20 };
370 let st = Stats { queued: 100, oldest: Some(now - age) };
371 if let Some(e) = watch.note("site", &st, 100, limit, now) {
372 told.push(e);
373 }
374 }
375 assert_eq!(told.len(), 1, "a queue that never drained told the operator {} times", told.len());
376 assert!(matches!(told[0], AlertEvent::OutboxBacklog { .. }), "the one telling was {:?}", told[0]);
377 Ok(())
378}
379
380// A2: three sign-ups for one address, queued over three days behind a hold, leave as one
381// confirmation when the hold lifts: "1 a day" is judged when the mail goes, not when it is queued.
382#[tokio::test]
383async fn test_three_signups_queued_behind_a_hold_send_one_confirmation_06() -> Outcome<()> {
384 let (db, uid, _tmp) = res!(common::test_db());
385 let handle = (db, uid);
386 let cfg = PublishConfig {
387 confirm_interval_secs: DAY,
388 confirm_max: 3,
389 confirm_window_days: 30,
390 ..cfg()
391 };
392 let base = T0 / 1000;
393 for d in 0..3u64 {
394 res!(outbox::push(&handle, &[Entry::new(Kind::Confirm, "v@site.test", "", base + d * DAY)]));
395 }
396 let (fake, pacer, mut share) = (Fake::default(), Pacer::new(100), Share::default());
397 let mut now = T0 + 3 * DAY * 1000;
398 for _ in 0..20 {
399 match res!(outbox::step(&handle, &cfg, &fake, &pacer, &mut share, "test", now).await) {
400 Step::Wait(ms) => now += ms,
401 Step::Idle => break,
402 _ => {},
403 }
404 }
405 assert_eq!(fake.count(), 1, "three sign-ups in a hold sent {} confirmations", fake.count());
406 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 0, "a repeat sign-up stayed queued");
407 Ok(())
408}
409
410fn median(mut v: Vec<u128>) -> u128 {
411 v.sort();
412 v[v.len() / 2]
413}
414
415// A7: the reply takes the same time whether the address is new or on the list, at 5,000 subscribers.
416#[tokio::test]
417async fn test_a_new_address_and_a_confirmed_one_are_answered_in_the_same_time_07() -> Outcome<()> {
418 let (db, uid, _tmp) = res!(common::test_db());
419 let handle = (db, uid);
420 let n = 5000;
421 let mut confirmed = Vec::new();
422 {
423 let guard = lock_read!(handle.0);
424 for i in 0..n {
425 let email = fmt!("c{:05}@site.test", i);
426 let sub = Subscriber {
427 email: email.clone(),
428 state: SubState::Confirmed,
429 token: subscribe::mint_token(),
430 created: None,
431 sent: None,
432 };
433 res!(guard.insert(dat!(fmt!("{}{}", subscribe::KEY_PREFIX, email)), sub.to_dat(), handle.1, None));
434 confirmed.push(email);
435 }
436 res!(guard.insert(dat!(subscribe::INDEX_KEY), store::names_dat(&confirmed), handle.1, None));
437 }
438 let sender = res!(MailSender::new("mail.site.test".to_string(), Vec::new(), "news@site.test".to_string()));
439 let mail = Some(Arc::new(sender.with_outbound_hourly(0)));
440 let (cfg, hdrs) = (cfg(), HeaderFields::default());
441 let (mut fresh, mut known) = (Vec::new(), Vec::new());
442 for i in 0..60usize {
443 let new_body = fmt!("email=new{}%40site.test", i);
444 let old_body = fmt!("email={}", confirmed[i * 7].replace('@', "%40"));
445 let t = Instant::now();
446 res!(subscribe::handle_subscribe(&cfg, Some(&handle), &mail, &hdrs, new_body.as_bytes(), None, "t").await);
447 fresh.push(t.elapsed().as_micros());
448 let t = Instant::now();
449 res!(subscribe::handle_subscribe(&cfg, Some(&handle), &mail, &hdrs, old_body.as_bytes(), None, "t").await);
450 known.push(t.elapsed().as_micros());
451 }
452 let (mf, mk) = (median(fresh.clone()), median(known.clone()));
453 fresh.sort();
454 known.sort();
455 println!("A7 at {} subscribers: new median {} us (p10 {}, p90 {}), confirmed median {} us (p10 {}, p90 {})",
456 n, mf, fresh[6], fresh[54], mk, known[6], known[54]);
457 assert!(fresh[6] <= known[54] && known[6] <= fresh[54],
458 "the reply times of a new ({} us) and a confirmed ({} us) address do not overlap", mf, mk);
459 Ok(())
460}
461
462// B-5: a pass over a long lane of backing-off entries never keeps the write lock for the whole lane.
463#[tokio::test]
464async fn test_a_long_backing_off_lane_does_not_hold_the_write_lock_08() -> Outcome<()> {
465 let (db, uid, _tmp) = res!(common::test_db());
466 let handle = (db, uid);
467 let base = T0 / 1000;
468 let mut entries = Vec::new();
469 for i in 0..3000 {
470 let mut e = Entry::new(Kind::News, &fmt!("n{}@site.test", i), "post", base);
471 e.next_try = base + 3600;
472 entries.push(e);
473 }
474 res!(outbox::push(&handle, &entries));
475 let (fake, pacer, cfg) = (Fake::default(), Pacer::new(100), cfg());
476 let done = Arc::new(std::sync::atomic::AtomicBool::new(false));
477 let (probe_db, flag) = ((handle.0.clone(), handle.1), done.clone());
478 let probe = std::thread::spawn(move || {
479 let mut worst = std::time::Duration::ZERO;
480 while !flag.load(std::sync::atomic::Ordering::Relaxed) {
481 let t = Instant::now();
482 let _ = store::exclusive(&probe_db, |_, _| -> Outcome<()> { Ok(()) });
483 worst = worst.max(t.elapsed());
484 std::thread::sleep(std::time::Duration::from_micros(200));
485 }
486 worst
487 });
488 std::thread::sleep(std::time::Duration::from_millis(20));
489 let t = Instant::now();
490 res!(outbox::step(&handle, &cfg, &fake, &pacer, &mut Share::default(), "test", T0).await);
491 let total = t.elapsed();
492 done.store(true, std::sync::atomic::Ordering::Relaxed);
493 let waited = probe.join().unwrap_or_default();
494 println!("B-5: a pass over 3000 backing-off entries took {:?}, the longest wait for the lock was {:?}",
495 total, waited);
496 assert!(waited * 3 < total, "a writer waited {:?} of a {:?} pass", waited, total);
497 Ok(())
498}
499
500// A live post, and `n` confirmed subscribers named `<tag><i>@site.test`.
501fn live_post_and_readers(db: &Handle, slug: &str, tag: &str, n: usize) -> Outcome<Vec<String>> {
502 let rec = Record {
503 slug: slug.to_string(),
504 author: String::new(),
505 categories: Vec::new(),
506 state: PostState::Live,
507 markup: Markup::Markdown,
508 date: Some(fmt!("2026-07-17")),
509 source: fmt!("# A post\n\nAn opening sentence.\n"),
510 deliveries: Vec::new(),
511 tags: Vec::new(),
512 ai_level: None,
513 };
514 res!(store::put(db, &rec, &rec.slug, "test"));
515 let mut emails = Vec::new();
516 for i in 0..n {
517 let email = fmt!("{}{}@site.test", tag, i);
518 res!(subscribe::add_pending(db, &email, &Window::default(), 1));
519 let sub = res!(subscribe::get(db, &email));
520 let sub = res!(sub.ok_or_else(|| err!("no subscriber"; Test, Missing)));
521 let _ = res!(subscribe::confirm(db, &sub.token, "test"));
522 emails.push(email);
523 }
524 Ok(emails)
525}
526
527// F2b erase: an address erased while its sign-up waits on the queue is not made again by the drainer
528// and is sent nothing, and a confirmed reader's queued copies leave the newsletter lane and its tally.
529#[tokio::test]
530async fn test_an_erased_address_with_queued_mail_is_not_recreated_or_mailed_09() -> Outcome<()> {
531 let (db, uid, _tmp) = res!(common::test_db());
532 let handle = (db, uid);
533 res!(outbox::push(&handle, &[
534 Entry::new(Kind::Confirm, "gone@site.test", "", T0 / 1000),
535 Entry::new(Kind::Confirm, "keep@site.test", "", T0 / 1000),
536 ]));
537 // The newsletter is queued for two confirmed readers, one of whom is erased before it is drained.
538 let readers = res!(live_post_and_readers(&handle, "on-rent", "r", 2));
539 let sender = res!(MailSender::new("mail.site.test".to_string(), Vec::new(), "news@site.test".to_string()));
540 let sender = sender.with_outbound_hourly(3600);
541 res!(send::send_newsletter(&sender, &handle, "on-rent", "test"));
542 assert!(!res!(subscribe::remove(&handle, "gone@site.test", "test")), "a record was found to erase");
543 assert!(res!(subscribe::remove(&handle, &readers[0], "test")), "the reader was not found to erase");
544 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 1, "the erased sign-up stayed queued");
545 assert_eq!(res!(outbox::queued(&handle, Kind::News)), 1, "the erased reader's copy stayed queued");
546
547 let fake = Fake::default();
548 res!(run_hour(&handle, &fake, sender.pacer()).await);
549 assert!(res!(subscribe::get(&handle, "gone@site.test")).is_none(), "the drainer made the erased record again");
550 assert!(res!(subscribe::get(&handle, &readers[0])).is_none(), "the erased reader came back");
551 let mut to = fake.to.lock().map(|v| v.clone()).unwrap_or_default();
552 to.sort();
553 assert_eq!(to, vec![fmt!("keep@site.test"), readers[1].clone()], "mail went to an erased address");
554 let hist = res!(send::send_history(&handle));
555 assert_eq!((hist[0].attempted, hist[0].sent, hist[0].waiting()), (1, 1, 0),
556 "the erased copy was not taken off the send's tally");
557 Ok(())
558}
559
560// F2b erase: a sign-up the drainer has taken and an erasure then removes is refused when it is applied,
561// and a message it was about to retry is not queued again.
562#[tokio::test]
563async fn test_a_signup_taken_then_erased_is_refused_when_applied_10() -> Outcome<()> {
564 let (db, uid, _tmp) = res!(common::test_db());
565 let handle = (db, uid);
566 res!(outbox::push(&handle, &[Entry::new(Kind::Confirm, "gone@site.test", "", T0 / 1000)]));
567 let (seq, entry) = match res!(outbox::take(&handle, Kind::Confirm, T0 / 1000, u64::MAX)) {
568 outbox::Take::Due(seq, e) => (seq, e),
569 other => return Err(err!("the sign-up was not due: {:?}", other; Test, Missing)),
570 };
571 // The erasure comes between the take and the apply.
572 res!(subscribe::remove(&handle, "gone@site.test", "test"));
573 let applied = res!(subscribe::add_pending_from(
574 &handle, &entry.email, &Window::default(), T0 / 1000, Some((entry.kind, seq))));
575 assert!(applied.is_none(), "a sign-up erased after it was taken was applied");
576 assert!(res!(subscribe::get(&handle, "gone@site.test")).is_none(), "the record was made again");
577 // A failed send retried after the erasure puts nothing back.
578 res!(outbox::retry(&handle, seq, &Entry { tries: 1, ..entry }));
579 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 0, "an erased sign-up was queued again");
580 Ok(())
581}
582// F2b A9: a post still being sent is not queued a second time, and may be again once it has drained.
583#[tokio::test]
584async fn test_a_post_still_being_sent_is_not_queued_twice_14() -> Outcome<()> {
585 let (db, uid, _tmp) = res!(common::test_db());
586 let handle = (db, uid);
587 let _ = res!(live_post_and_readers(&handle, "on-rent", "r", 3));
588 let sender = res!(MailSender::new("mail.site.test".to_string(), Vec::new(), "news@site.test".to_string()));
589 let sender = sender.with_outbound_hourly(3600);
590 let first = res!(send::send_newsletter(&sender, &handle, "on-rent", "test"));
591 assert_eq!(first.attempted, 3);
592 assert!(send::send_newsletter(&sender, &handle, "on-rent", "test").is_err(),
593 "a post with three messages waiting was queued again");
594 assert_eq!(res!(outbox::queued(&handle, Kind::News)), 3, "the refused send queued messages");
595 assert_eq!(res!(send::send_history(&handle)).len(), 1, "the refused send was recorded");
596 // Another post is not held up by it.
597 let _ = res!(live_post_and_readers(&handle, "on-work", "w", 0));
598 assert_eq!(res!(send::send_newsletter(&sender, &handle, "on-work", "test")).attempted, 3,
599 "a different post was refused");
600
601 let fake = Fake::default();
602 res!(run_hour(&handle, &fake, sender.pacer()).await);
603 assert_eq!(fake.count(), 6, "both posts were not sent to all three");
604 assert_eq!(res!(send::send_newsletter(&sender, &handle, "on-rent", "test")).attempted, 3,
605 "a drained send was refused a second");
606 Ok(())
607}
608
609
610// F2c A8: a delivery that never returns holds the drainer for one deadline and no more, its domain is
611// then left alone without a slot or a try being spent on it, and each further timeout doubles the wait.
612#[tokio::test]
613async fn test_a_courier_that_never_returns_is_given_up_and_its_domain_backed_off_15() -> Outcome<()> {
614 let (db, uid, _tmp) = res!(common::test_db());
615 let handle = (db, uid);
616 res!(outbox::push(&handle, &[
617 Entry::new(Kind::Confirm, "a@hang.test", "", T0 / 1000),
618 Entry::new(Kind::Confirm, "b@hang.test", "", T0 / 1000),
619 Entry::new(Kind::Confirm, "c@fine.test", "", T0 / 1000),
620 ]));
621 let (mute, cfg) = (Mute::default(), cfg());
622 // One send a second, and a deadline of 50 ms.
623 let pacer = Pacer::new(3600).with_deadline(Duration::from_millis(50));
624 let mut share = Share::default();
625 let mut now = T0;
626 let began = Instant::now();
627 for _ in 0..12 {
628 let pass = outbox::step(&handle, &cfg, &mute, &pacer, &mut share, "test", now);
629 let stepped = match tokio::time::timeout(Duration::from_secs(5), pass).await {
630 Ok(r) => res!(r),
631 Err(_) => return Err(err!("the drainer was held by a delivery that never returned"; Test, Timeout)),
632 };
633 match stepped {
634 Step::Wait(ms) => now += ms,
635 Step::Later(_) | Step::Idle => break,
636 _ => {},
637 }
638 }
639 assert!(began.elapsed() < Duration::from_secs(2), "the drainer spent {:?} on one dead host", began.elapsed());
640 let asked = mute.asked.lock().map(|v| v.clone()).unwrap_or_default();
641 assert_eq!(asked, vec![fmt!("a@hang.test"), fmt!("c@fine.test")],
642 "the second address at a host that timed out was dialled, or the third was not reached");
643 // Only the one failed delivery and the one that went took a slot: the put-back used none.
644 assert_eq!(now, T0 + 1000, "an entry put back for a blocked domain used a slot");
645 let first = T0 / 1000;
646 assert_eq!(res!(pacer.blocked(HANG_IP, first)), Some(first + outbox::BACKOFF_MIN_SECS),
647 "the host was not blocked for the first wait");
648 assert_eq!(res!(pacer.blocked(FINE_IP, first)), None, "a host that answered was blocked");
649 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 2, "the two entries for the dead host were lost");
650
651 // Past the block the host is tried once more, times out again, and is left alone twice as long.
652 now = T0 + (outbox::BACKOFF_MIN_SECS + 1) * 1000;
653 let pass = outbox::step(&handle, &cfg, &mute, &pacer, &mut share, "test", now);
654 match tokio::time::timeout(Duration::from_secs(5), pass).await {
655 Ok(r) => { res!(r); },
656 Err(_) => return Err(err!("the drainer was held by a delivery that never returned"; Test, Timeout)),
657 }
658 let later = now / 1000;
659 assert_eq!(res!(pacer.blocked(HANG_IP, later)), Some(later + 2 * outbox::BACKOFF_MIN_SECS),
660 "a second timeout did not double the wait");
661 Ok(())
662}
663
664// F2c A6: a steady stream of sign-ups does not keep a newsletter off the air. With a confirmation
665// queued before the newsletter's single message and eleven more behind it, the message leaves in the
666// first four slots and not the thirteenth.
667#[tokio::test]
668async fn test_a_newsletter_is_sent_within_four_slots_of_twelve_confirmations_16() -> Outcome<()> {
669 let (db, uid, _tmp) = res!(common::test_db());
670 let handle = (db, uid);
671 let readers = res!(live_post_and_readers(&handle, "on-rent", "r", 1));
672 let sender = res!(MailSender::new("mail.site.test".to_string(), Vec::new(), "news@site.test".to_string()));
673 let sender = sender.with_outbound_hourly(3600);
674 let confirmations: Vec<Entry> = (0..12)
675 .map(|i| Entry::new(Kind::Confirm, &fmt!("p{}@site.test", i), "", T0 / 1000))
676 .collect();
677 res!(outbox::push(&handle, &confirmations));
678 assert_eq!(res!(send::send_newsletter(&sender, &handle, "on-rent", "test")).attempted, 1);
679
680 let fake = Fake::default();
681 res!(run_hour(&handle, &fake, sender.pacer()).await);
682 let to = fake.to.lock().map(|v| v.clone()).unwrap_or_default();
683 assert_eq!(to.len(), 13, "the hour did not send the twelve confirmations and the newsletter");
684 let at = res!(to.iter().position(|t| *t == readers[0])
685 .ok_or_else(|| err!("the newsletter was never sent"; Test, Missing)));
686 assert!(at < 4, "the newsletter was sent in slot {}, behind the confirmations", at + 1);
687 Ok(())
688}
689
690// F2c A6: the confirmation queue stops at its cap, a sign-up past it is answered as any other and
691// queues nothing, and the queue takes sign-ups again as the drainer empties it.
692#[tokio::test]
693async fn test_the_confirmation_queue_stops_at_its_cap_17() -> Outcome<()> {
694 let (db, uid, _tmp) = res!(common::test_db());
695 let handle = (db, uid);
696 let sender = res!(MailSender::new("mail.site.test".to_string(), Vec::new(), "news@site.test".to_string()));
697 let mail = Some(Arc::new(sender));
698 let cfg = PublishConfig { outbox_confirm_max: 3, ..cfg() };
699 for i in 0..5 {
700 let body = fmt!("email=u{}%40site.test", i);
701 let _ = res!(subscribe::handle_subscribe(
702 &cfg, Some(&handle), &mail, &HeaderFields::default(), body.as_bytes(), None, "test",
703 ).await);
704 }
705 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 3, "five sign-ups against a cap of three");
706
707 // One leaves, and the room it made is taken by the next sign-up and no more.
708 let (fake, pacer, mut share) = (Fake::default(), Pacer::new(3600), Share::default());
709 assert_eq!(res!(outbox::step(&handle, &cfg, &fake, &pacer, &mut share, "test", T0).await), Step::Sent);
710 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 2);
711 for i in 5..8 {
712 let body = fmt!("email=u{}%40site.test", i);
713 let _ = res!(subscribe::handle_subscribe(
714 &cfg, Some(&handle), &mail, &HeaderFields::default(), body.as_bytes(), None, "test",
715 ).await);
716 }
717 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 3, "the queue did not refill to its cap and stop");
718 Ok(())
719}
720
721// A sender that notes the clock the test set when each address was handed to it.
722#[derive(Default)]
723struct Timed {
724 to: Mutex<Vec<(u64, String)>>,
725 clock: Mutex<u64>,
726}
727
728impl Courier for Timed {
729
730 fn default_from(&self) -> &str {
731 "news@site.test"
732 }
733
734 fn deliver(&self, _from: &str, to: &str, _msg: &str, _dial: &Dial)
735 -> impl Future<Output = Outcome<String>> + Send
736 {
737 let to = to.to_string();
738 async move {
739 let at = *lock_mutex!(self.clock);
740 let mut v = lock_mutex!(self.to);
741 v.push((at, to));
742 Ok(fmt!("q1"))
743 }
744 }
745}
746
747// F3 S1: an entry the drainer skips takes a slot as one it sends does, so when a stranger's own
748// confirmation leaves says nothing about whether the address queued ahead of it was a member.
749#[tokio::test]
750async fn test_a_skipped_entry_takes_a_slot_as_a_sent_one_does_18() -> Outcome<()> {
751 let mut gaps = Vec::new();
752 for state in [None, Some(SubState::Confirmed), Some(SubState::Bounced)] {
753 let (db, uid, _tmp) = res!(common::test_db());
754 let handle = (db, uid);
755 let victim = "v@site.test";
756 if let Some(state) = state {
757 let guard = lock_read!(handle.0);
758 let sub = Subscriber {
759 email: fmt!("{}", victim),
760 state,
761 token: subscribe::mint_token(),
762 created: None,
763 sent: None,
764 };
765 res!(guard.insert(dat!(fmt!("{}{}", subscribe::KEY_PREFIX, victim)), sub.to_dat(), handle.1, None));
766 }
767 res!(outbox::push(&handle, &[
768 Entry::new(Kind::Confirm, victim, "", T0 / 1000),
769 Entry::new(Kind::Confirm, "me@attacker.test", "", T0 / 1000),
770 ]));
771 let (timed, pacer, mut share, cfg) = (Timed::default(), Pacer::new(100), Share::default(), cfg());
772 let mut now = T0;
773 for _ in 0..20 {
774 *lock_mutex!(timed.clock) = now;
775 match res!(outbox::step(&handle, &cfg, &timed, &pacer, &mut share, "test", now).await) {
776 Step::Wait(ms) => now += ms,
777 Step::Idle => break,
778 _ => {},
779 }
780 }
781 let to = lock_mutex!(timed.to).clone();
782 let mine = res!(to.iter().find(|x| x.1 == "me@attacker.test")
783 .map(|x| x.0 - T0)
784 .ok_or_else(|| err!("the stranger's own confirmation was never sent"; Test, Missing)));
785 gaps.push(mine);
786 }
787 assert!(gaps[0] == gaps[1] && gaps[0] == gaps[2],
788 "when a stranger's own confirmation leaves depends on the state of the address ahead of it: {:?} ms \
789 (new, confirmed, bounced)", gaps);
790 Ok(())
791}
792
793// A courier that takes the first message and never reports it accepted, as a far end does that
794// takes the whole of DATA and is slow to say so, and answers at once after that.
795#[derive(Default)]
796struct Slow {
797 asked: Mutex<Vec<String>>,
798}
799
800impl Courier for Slow {
801
802 fn default_from(&self) -> &str {
803 "news@site.test"
804 }
805
806 fn deliver(&self, _from: &str, to: &str, _msg: &str, _dial: &Dial)
807 -> impl Future<Output = Outcome<String>> + Send
808 {
809 let to = to.to_string();
810 async move {
811 let first = {
812 let mut v = lock_mutex!(self.asked);
813 v.push(to);
814 v.len() == 1
815 };
816 if first {
817 std::future::pending::<()>().await;
818 }
819 Ok(fmt!("q1"))
820 }
821 }
822}
823
824// A confirmation whose delivery overran the deadline before the message was whole at the far end
825// was not taken: it is not counted against the address's interval, and the retry sends it.
826#[tokio::test]
827async fn test_a_confirmation_that_timed_out_before_it_was_whole_is_sent_again_20() -> Outcome<()> {
828 let (db, uid, _tmp) = res!(common::test_db());
829 let handle = (db, uid);
830 let cfg = PublishConfig {
831 confirm_interval_secs: DAY,
832 confirm_max: 3,
833 confirm_window_days: 30,
834 ..cfg()
835 };
836 res!(outbox::push(&handle, &[Entry::new(Kind::Confirm, "v@site.test", "", T0 / 1000)]));
837 let (slow, share) = (Slow::default(), &mut Share::default());
838 let pacer = Pacer::new(100).with_deadline(Duration::from_millis(50));
839 assert_eq!(res!(outbox::step(&handle, &cfg, &slow, &pacer, share, "test", T0).await), Step::Worked);
840 // Past the retry wait and any block on the host, on the same day.
841 let mut now = T0 + 3_600_000;
842 for _ in 0..10 {
843 match res!(outbox::step(&handle, &cfg, &slow, &pacer, share, "test", now).await) {
844 Step::Wait(ms) => now += ms,
845 Step::Later(_) | Step::Idle => break,
846 _ => {},
847 }
848 }
849 let asked = lock_mutex!(slow.asked).len();
850 assert_eq!(asked, 2, "a confirmation that timed out was delivered {} times", asked);
851 Ok(())
852}
853
854// A courier whose far end never answers for any address at a host under .evil.test, and records
855// each address it was asked to deliver to.
856#[derive(Default)]
857struct Tarpit {
858 asked: Mutex<Vec<String>>,
859}
860
861impl Tarpit {
862
863 // Every address at a host under .evil.test has the one exchange, as a wildcard MX gives it.
864 fn ip_of(to: &str) -> IpAddr {
865 if to.ends_with(".evil.test") { EVIL_IP } else { FINE_IP }
866 }
867}
868
869impl Courier for Tarpit {
870
871 fn default_from(&self) -> &str {
872 "news@site.test"
873 }
874
875 fn exchanges(&self, to: &str) -> impl Future<Output = Outcome<Vec<IpAddr>>> + Send {
876 let ip = Self::ip_of(to);
877 async move { Ok(vec![ip]) }
878 }
879
880 fn deliver(&self, _from: &str, to: &str, _msg: &str, dial: &Dial)
881 -> impl Future<Output = Outcome<String>> + Send
882 {
883 let to = to.to_string();
884 async move {
885 {
886 let mut v = lock_mutex!(self.asked);
887 v.push(to.clone());
888 }
889 res!(dial.dialling(Self::ip_of(&to)));
890 if to.ends_with(".evil.test") {
891 std::future::pending::<()>().await;
892 }
893 Ok(fmt!("q1"))
894 }
895 }
896}
897
898// F3 A8: a wildcard MX gives every fresh subdomain the same exchange, so the first delivery that
899// overruns the deadline backs the exchange off and the sign-ups behind it, at other subdomains, are
900// put back without being dialled. Keyed by recipient domain, each one costs a deadline.
901#[tokio::test]
902async fn test_fresh_subdomains_behind_one_exchange_cost_one_backoff_19() -> Outcome<()> {
903 let (db, uid, _tmp) = res!(common::test_db());
904 let handle = (db, uid);
905 let mut entries: Vec<Entry> = (0..5)
906 .map(|i| Entry::new(Kind::Confirm, &fmt!("x@s{}.evil.test", i), "", T0 / 1000))
907 .collect();
908 entries.push(Entry::new(Kind::Confirm, "good@fine.test", "", T0 / 1000));
909 res!(outbox::push(&handle, &entries));
910 let (tarpit, cfg) = (Tarpit::default(), cfg());
911 let pacer = Pacer::new(3600).with_deadline(Duration::from_millis(50));
912 let mut share = Share::default();
913 let mut now = T0;
914 for _ in 0..40 {
915 match res!(outbox::step(&handle, &cfg, &tarpit, &pacer, &mut share, "test", now).await) {
916 Step::Wait(ms) => now += ms,
917 Step::Later(_) | Step::Idle => break,
918 _ => {},
919 }
920 }
921 let asked = lock_mutex!(tarpit.asked).clone();
922 let dials = asked.iter().filter(|a| a.ends_with(".evil.test")).count();
923 assert!(asked.iter().any(|a| a == "good@fine.test"), "the good address behind the tarpit was never reached");
924 assert_eq!(dials, 1, "five subdomains of one exchange cost {} dials: {:?}", dials, asked);
925 Ok(())
926}
927
928// F3 lead ruling: an admin erase takes the address's confirmation counter with it, under the same guard,
929// so the erased address starts with its limit whole, and another address's counter is not touched.
930#[tokio::test]
931async fn test_an_admin_erase_clears_the_confirmation_counter_of_the_address_21() -> Outcome<()> {
932 let (db, uid, _tmp) = res!(common::test_db());
933 let handle = (db, uid);
934 let w = Window { interval_secs: DAY, max: 3, span_secs: 30 * DAY };
935 let now = T0 / 1000;
936 for who in ["v@site.test", "other@site.test"] {
937 assert!(res!(subscribe::add_pending(&handle, who, &w, now)).is_some(), "{} was refused a first confirmation", who);
938 res!(subscribe::count_sent(&handle, who, &w, now));
939 assert!(res!(subscribe::add_pending(&handle, who, &w, now + 60)).is_none(),
940 "{} was owed a second confirmation within the interval", who);
941 }
942 assert!(res!(subscribe::remove(&handle, "v@site.test", "test")), "the address was not found to erase");
943 assert!(res!(subscribe::add_pending(&handle, "v@site.test", &w, now + 60)).is_some(),
944 "an erased address was still held to the counter of its earlier confirmations");
945 assert!(res!(subscribe::add_pending(&handle, "other@site.test", &w, now + 60)).is_none(),
946 "the erase cleared the counter of another address");
947 Ok(())
948}
949
950// A courier through the real SMTP client to a provider on the loopback that takes the message and
951// answers it with a line past the client's bound, so whether it was accepted cannot be read.
952struct Unread {
953 addr: std::net::SocketAddr,
954 asked: Arc<Mutex<usize>>, // messages that reached the provider whole
955}
956
957impl Unread {
958 async fn up() -> Outcome<Self> {
959 use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
960 let l = res!(tokio::net::TcpListener::bind("127.0.0.1:0").await.map_err(|e| err!(e, "Bind."; Test, IO)));
961 let addr = res!(l.local_addr().map_err(|e| err!(e, "Address."; Test, IO)));
962 let asked = Arc::new(Mutex::new(0));
963 let n = asked.clone();
964 tokio::spawn(async move {
965 while let Ok((s, _)) = l.accept().await {
966 let (r, mut w) = s.into_split();
967 let (mut lines, mut data) = (BufReader::new(r).lines(), false);
968 let _ = w.write_all(b"220 unread.test\r\n").await;
969 while let Ok(Some(line)) = lines.next_line().await {
970 let say: Vec<u8> = match (data, line.as_str()) {
971 (true, ".") => {
972 if let Ok(mut g) = n.lock() { *g += 1; }
973 [&b"250 "[..], &[b'x'; 5000][..]].concat()
974 },
975 (true, _) => continue,
976 (_, l) if l.starts_with("EHLO") => b"250-unread.test\r\n250 AUTH PLAIN\r\n".to_vec(),
977 (_, l) if l.starts_with("AUTH") => b"235 ok\r\n".to_vec(),
978 (_, "DATA") => { data = true; b"354 go\r\n".to_vec() },
979 _ => b"250 ok\r\n".to_vec(),
980 };
981 let _ = w.write_all(&say).await;
982 }
983 }
984 });
985 Ok(Self { addr, asked })
986 }
987}
988
989impl Courier for Unread {
990
991 fn default_from(&self) -> &str {
992 "news@site.test"
993 }
994
995 fn deliver(&self, from: &str, to: &str, msg: &str, _dial: &Dial)
996 -> impl Future<Output = Outcome<String>> + Send
997 {
998 let (from, to, msg, addr) = (from.to_string(), to.to_string(), msg.to_string(), self.addr);
999 async move {
1000 let cfg = SubmissionConfig::new("unread.test", addr.port(), Security::Plain, "u", "p")
1001 .with_addr(addr);
1002 let client = res!(OutboundClient::with_system_roots("site.test"));
1003 client.submit(&cfg, &from, &[to], msg.as_bytes()).await
1004 }
1005 }
1006}
1007
1008/// An answer to the message that cannot be read may be its acceptance: the subscriber is counted
1009/// sent, never suppressed as a bounce, and not sent the confirmation again.
1010#[tokio::test]
1011async fn test_an_unreadable_answer_after_the_message_neither_bounces_nor_resends_22() -> Outcome<()> {
1012 let (db, uid, _tmp) = res!(common::test_db());
1013 let handle = (db, uid);
1014 res!(outbox::push(&handle, &[Entry::new(Kind::Confirm, "v@site.test", "", T0 / 1000)]));
1015 let (unread, share, pacer) = (res!(Unread::up().await), &mut Share::default(), Pacer::new(100));
1016 let mut now = T0;
1017 for _ in 0..10 {
1018 match res!(outbox::step(&handle, &cfg(), &unread, &pacer, share, "test", now).await) {
1019 Step::Wait(ms) => now += ms,
1020 Step::Later(_) | Step::Idle => break,
1021 _ => now += 3_600_000,
1022 }
1023 }
1024 let asked = *lock_mutex!(unread.asked);
1025 assert_eq!(asked, 1, "the confirmation reached the provider {} times", asked);
1026 let sub = res!(res!(subscribe::get(&handle, "v@site.test"))
1027 .ok_or_else(|| err!("No subscriber was stored."; Test, Missing)));
1028 assert!(sub.state != SubState::Bounced, "a subscriber whose provider may have the message was suppressed");
1029 assert!(sub.sent.is_some(), "a message the provider may have was not counted sent");
1030 Ok(())
1031}
1032
1033// A courier through the real SMTP client to a provider on the loopback that takes the whole message,
1034// holds its answer past the drainer's deadline, then drops the line (F1-a).
1035struct Stall {
1036 addr: std::net::SocketAddr,
1037 asked: Arc<Mutex<usize>>, // messages that reached the provider whole
1038}
1039
1040impl Stall {
1041 async fn up(hold: Duration) -> Outcome<Self> {
1042 use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
1043 let l = res!(tokio::net::TcpListener::bind("127.0.0.1:0").await.map_err(|e| err!(e, "Bind."; Test, IO)));
1044 let addr = res!(l.local_addr().map_err(|e| err!(e, "Address."; Test, IO)));
1045 let asked = Arc::new(Mutex::new(0));
1046 let n = asked.clone();
1047 tokio::spawn(async move {
1048 while let Ok((s, _)) = l.accept().await {
1049 let n = n.clone();
1050 tokio::spawn(async move {
1051 let (r, mut w) = s.into_split();
1052 let (mut lines, mut data) = (BufReader::new(r).lines(), false);
1053 let _ = w.write_all(b"220 stall.test\r\n").await;
1054 while let Ok(Some(line)) = lines.next_line().await {
1055 let say: &[u8] = match (data, line.as_str()) {
1056 (true, ".") => {
1057 if let Ok(mut g) = n.lock() { *g += 1; }
1058 tokio::time::sleep(hold).await;
1059 return;
1060 },
1061 (true, _) => continue,
1062 (_, l) if l.starts_with("EHLO") => b"250-stall.test\r\n250 AUTH PLAIN\r\n",
1063 (_, l) if l.starts_with("AUTH") => b"235 ok\r\n",
1064 (_, "DATA") => { data = true; b"354 go\r\n" },
1065 _ => b"250 ok\r\n",
1066 };
1067 let _ = w.write_all(say).await;
1068 }
1069 });
1070 }
1071 });
1072 Ok(Self { addr, asked })
1073 }
1074}
1075
1076impl Courier for Stall {
1077
1078 fn default_from(&self) -> &str {
1079 "news@site.test"
1080 }
1081
1082 fn deliver(&self, from: &str, to: &str, msg: &str, dial: &Dial)
1083 -> impl Future<Output = Outcome<String>> + Send
1084 {
1085 let (from, to, msg, addr) = (from.to_string(), to.to_string(), msg.to_string(), self.addr);
1086 async move {
1087 let cfg = SubmissionConfig::new("stall.test", addr.port(), Security::Plain, "u", "p")
1088 .with_addr(addr);
1089 let client = res!(OutboundClient::with_system_roots("site.test"));
1090 client.submit_with(&cfg, &from, &[to], msg.as_bytes(), dial).await
1091 }
1092 }
1093}
1094
1095/// A message whose answer is held past the drainer's deadline after the server has it whole may
1096/// have been taken: it is recorded as maybe sent, and never sent again.
1097#[tokio::test]
1098async fn test_a_whole_message_dropped_at_the_deadline_is_sent_once_23() -> Outcome<()> {
1099 let (db, uid, _tmp) = res!(common::test_db());
1100 let handle = (db, uid);
1101 res!(outbox::push(&handle, &[Entry::new(Kind::Confirm, "v@site.test", "", T0 / 1000)]));
1102 let stall = res!(Stall::up(Duration::from_secs(2)).await);
1103 let (share, pacer) = (&mut Share::default(), Pacer::new(100).with_deadline(Duration::from_millis(300)));
1104 let first = res!(outbox::step(&handle, &cfg(), &stall, &pacer, share, "test", T0).await);
1105 let mut now = T0;
1106 for _ in 0..10 {
1107 now += 3_600_000;
1108 match res!(outbox::step(&handle, &cfg(), &stall, &pacer, share, "test", now).await) {
1109 Step::Wait(ms) => now += ms,
1110 Step::Later(_) | Step::Idle => break,
1111 _ => {},
1112 }
1113 }
1114 let asked = *lock_mutex!(stall.asked);
1115 assert_eq!(asked, 1, "the confirmation reached the provider {} times", asked);
1116 assert_eq!(first, Step::Sent, "a message the provider may have was not taken as sent");
1117 let sub = res!(res!(subscribe::get(&handle, "v@site.test"))
1118 .ok_or_else(|| err!("No subscriber was stored."; Test, Missing)));
1119 assert!(sub.state != SubState::Bounced, "a subscriber whose provider may have the message was suppressed");
1120 assert!(sub.sent.is_some(), "a message the provider may have was not counted sent");
1121 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 0, "the message is still queued");
1122 Ok(())
1123}
1124
1125/// A drainer stopped while the answer to a whole message is awaited, then started again, finds that
1126/// the message may have been taken: it is counted sent and never sent again.
1127#[tokio::test]
1128async fn test_a_whole_message_cut_off_by_a_stop_is_not_sent_again_on_restart_24() -> Outcome<()> {
1129 let (db, uid, _tmp) = res!(common::test_db());
1130 let handle = (db, uid);
1131 res!(outbox::push(&handle, &[Entry::new(Kind::Confirm, "v@site.test", "", T0 / 1000)]));
1132 let stall = res!(Stall::up(Duration::from_secs(5)).await);
1133 // The stop: the drainer is dropped mid-delivery, well inside its deadline, so no arm of it runs.
1134 {
1135 let (share, pacer) = (&mut Share::default(), Pacer::new(100).with_deadline(Duration::from_secs(60)));
1136 let stopped = tokio::time::timeout(Duration::from_millis(500),
1137 outbox::step(&handle, &cfg(), &stall, &pacer, share, "test", T0)).await;
1138 assert!(stopped.is_err(), "the delivery finished before the stop");
1139 }
1140 assert_eq!(*lock_mutex!(stall.asked), 1, "the message did not reach the provider before the stop");
1141 match res!(outbox::take(&handle, Kind::Confirm, T0 / 1000, u64::MAX)) {
1142 outbox::Take::Due(_, e) => assert!(e.whole, "the entry left by the stop does not read maybe sent"),
1143 other => return Err(err!("The stop left no entry: {:?}.", other; Test, Missing)),
1144 }
1145 // The restart: a fresh pacer and share, an hour on.
1146 let (share, pacer) = (&mut Share::default(), Pacer::new(100).with_deadline(Duration::from_millis(300)));
1147 let mut now = T0;
1148 for _ in 0..10 {
1149 now += 3_600_000;
1150 match res!(outbox::step(&handle, &cfg(), &stall, &pacer, share, "test", now).await) {
1151 Step::Wait(ms) => now += ms,
1152 Step::Later(_) | Step::Idle => break,
1153 _ => {},
1154 }
1155 }
1156 let asked = *lock_mutex!(stall.asked);
1157 assert_eq!(asked, 1, "the confirmation reached the provider {} times", asked);
1158 let sub = res!(res!(subscribe::get(&handle, "v@site.test"))
1159 .ok_or_else(|| err!("No subscriber was stored."; Test, Missing)));
1160 assert!(sub.state != SubState::Bounced, "a subscriber whose provider may have the message was suppressed");
1161 assert!(sub.sent.is_some(), "a message the provider may have was not counted sent");
1162 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 0, "the message is still queued");
1163 Ok(())
1164}
1165
1166// A courier whose first delivery records the message whole and then never finishes writing its ".",
1167// as a far end that has stopped reading does; later ones go through. It counts the ones that finish.
1168#[derive(Default)]
1169struct DotHang {
1170 tries: Mutex<usize>,
1171}
1172
1173impl Courier for DotHang {
1174
1175 fn default_from(&self) -> &str {
1176 "news@site.test"
1177 }
1178
1179 fn deliver(&self, _from: &str, _to: &str, _msg: &str, dial: &Dial)
1180 -> impl Future<Output = Outcome<String>> + Send
1181 {
1182 async move {
1183 let first = {
1184 let mut n = lock_mutex!(self.tries);
1185 *n += 1;
1186 *n == 1
1187 };
1188 res!(dial.record(true));
1189 if first {
1190 std::future::pending::<()>().await;
1191 }
1192 res!(dial.mark_whole(true));
1193 Ok(fmt!("q2"))
1194 }
1195 }
1196}
1197
1198/// A delivery given up while its "." is still being written was not sent: it is retried, and counted
1199/// sent only when it goes.
1200#[tokio::test]
1201async fn test_a_delivery_dropped_while_its_dot_is_written_is_sent_again_25() -> Outcome<()> {
1202 let (db, uid, _tmp) = res!(common::test_db());
1203 let handle = (db, uid);
1204 res!(outbox::push(&handle, &[Entry::new(Kind::Confirm, "v@site.test", "", T0 / 1000)]));
1205 let (hang, share) = (DotHang::default(), &mut Share::default());
1206 let pacer = Pacer::new(100).with_deadline(Duration::from_millis(300));
1207 let first = res!(outbox::step(&handle, &cfg(), &hang, &pacer, share, "test", T0).await);
1208 assert_eq!(first, Step::Worked, "a message whose \".\" never went was taken as sent");
1209 let mut now = T0;
1210 for _ in 0..10 {
1211 now += 3_600_000;
1212 match res!(outbox::step(&handle, &cfg(), &hang, &pacer, share, "test", now).await) {
1213 Step::Wait(ms) => now += ms,
1214 Step::Later(_) | Step::Idle => break,
1215 _ => {},
1216 }
1217 }
1218 assert_eq!(*lock_mutex!(hang.tries), 2, "the message was not tried again, or tried too often");
1219 assert_eq!(res!(outbox::queued(&handle, Kind::Confirm)), 0, "the message is still queued");
1220 Ok(())
1221}
1222
1223/// A newsletter copy the far end may have is counted unconfirmed in the send's history, not sent.
1224#[tokio::test]
1225async fn test_a_newsletter_copy_that_may_have_gone_is_counted_unconfirmed_26() -> Outcome<()> {
1226 let (db, uid, _tmp) = res!(common::test_db());
1227 let handle = (db, uid);
1228 let _ = res!(live_post_and_readers(&handle, "on-rent", "r", 1));
1229 let sender = res!(MailSender::new("mail.site.test".to_string(), Vec::new(), "news@site.test".to_string()));
1230 let _ = res!(send::send_newsletter(&sender, &handle, "on-rent", "test"));
1231 let stall = res!(Stall::up(Duration::from_secs(2)).await);
1232 let (share, pacer) = (&mut Share::default(), Pacer::new(100).with_deadline(Duration::from_millis(300)));
1233 let mut now = T0;
1234 for _ in 0..10 {
1235 match res!(outbox::step(&handle, &cfg(), &stall, &pacer, share, "test", now).await) {
1236 Step::Wait(ms) => now += ms,
1237 Step::Later(_) | Step::Idle => break,
1238 _ => now += 3_600_000,
1239 }
1240 }
1241 assert_eq!(*lock_mutex!(stall.asked), 1, "the copy reached the provider more than once");
1242 let hist = res!(send::send_history(&handle));
1243 let e = res!(hist.first().ok_or_else(|| err!("No send was recorded."; Test, Missing)));
1244 assert_eq!((e.sent, e.unconfirmed, e.waiting()), (0, 1, 0), "the copy was not counted unconfirmed");
1245 Ok(())
1246}
1247
1248/// A confirmation that may not have arrived does not hold the address back: a repeat sign-up within
1249/// the interval is sent another.
1250#[tokio::test]
1251async fn test_an_unconfirmed_confirmation_lets_the_person_ask_again_27() -> Outcome<()> {
1252 let (db, uid, _tmp) = res!(common::test_db());
1253 let handle = (db, uid);
1254 let cfg = PublishConfig {
1255 confirm_interval_secs: DAY,
1256 confirm_max: 3,
1257 confirm_window_days: 30,
1258 ..cfg()
1259 };
1260 let stall = res!(Stall::up(Duration::from_secs(2)).await);
1261 let pacer = Pacer::new(100).with_deadline(Duration::from_millis(300));
1262 for (i, at) in [T0, T0 + 600_000].iter().enumerate() {
1263 res!(outbox::push(&handle, &[Entry::new(Kind::Confirm, "v@site.test", "", at / 1000)]));
1264 let step = res!(outbox::step(&handle, &cfg, &stall, &pacer, &mut Share::default(), "test", *at).await);
1265 assert_eq!(step, Step::Sent, "sign-up {} was not sent its confirmation", i + 1);
1266 }
1267 assert_eq!(*lock_mutex!(stall.asked), 2, "the repeat sign-up was held back by an unconfirmed copy");
1268 Ok(())
1269}