Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/outbound_sweep.rs

7.3 KiB, 1 run

created by r1870400018:65568, 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//! What the outbound spool does with a message the far end refused.
2//!
3//! On karri two stale messages were retried every thirty seconds for twenty-five days: one answered
4//! `550 5.4.1 Recipient address rejected`, the other addressed a domain with a null MX. A 5xx is
5//! final (RFC 5321 §4.2.1), and the client already tagged it `Permanent`. The worker never read the
6//! tag, and left every failure on disk for the next sweep. The transport here is a closure, so what
7//! is under test is the spool's disposition of each outcome, with no network.
8
9use oxedyne_fe2o3_core::prelude::*;
10use oxedyne_fe2o3_mail::outbound::{
11 GIVE_UP_SECS,
12 OutboundSpool,
13 RetrySchedule,
14 Sweep,
15 retry_after,
16};
17
18use std::{
19 fs,
20 path::PathBuf,
21 sync::{
22 Arc,
23 atomic::{
24 AtomicUsize,
25 Ordering,
26 },
27 },
28 time::{
29 SystemTime,
30 UNIX_EPOCH,
31 },
32};
33
34
35/// A spool in a directory of its own, holding one message.
36fn spool_with_one(tag: &str) -> Outcome<(OutboundSpool, PathBuf)> {
37 let root = std::env::temp_dir().join(fmt!("fe2o3_spool_{}_{}", std::process::id(), tag));
38 let _ = fs::remove_dir_all(&root);
39 let spool = res!(OutboundSpool::new(root.clone()));
40 res!(spool.enqueue("alice@sender.test", &[fmt!("bob@far.test")], b"Subject: hi\r\n\r\nhi\r\n"));
41 Ok((spool, root))
42}
43
44fn now() -> u64 {
45 SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or_default()
46}
47
48fn queued(spool: &OutboundSpool) -> Outcome<usize> {
49 Ok(res!(spool.list()).len())
50}
51
52fn failed_files(root: &PathBuf) -> usize {
53 fs::read_dir(root.join("failed")).map(|r| r.flatten().count()).unwrap_or(0)
54}
55
56
57/// A sweep in which the far end answers every message with a 451, counting how often it is asked.
58async fn greylisted(
59 spool: &OutboundSpool,
60 sched: &mut RetrySchedule,
61 at: u64,
62 calls: &Arc<AtomicUsize>,
63)
64 -> Outcome<Sweep>
65{
66 let calls = calls.clone();
67 spool.sweep(sched, at, move |_m| {
68 let calls = calls.clone();
69 async move {
70 calls.fetch_add(1, Ordering::SeqCst);
71 let r: Outcome<String> = Err(err!(
72 "RCPT TO:<bob@far.test> deferred: 451 4.7.1 Greylisted";
73 IO, Network, Wire));
74 r
75 }
76 }).await
77}
78
79
80/// The 550 case. Before the fix the message stayed queued and the far end was asked again on every
81/// sweep, which is what the journal showed.
82#[tokio::test]
83async fn a_permanent_refusal_ends_the_message_at_once() -> Outcome<()> {
84 let (spool, root) = res!(spool_with_one("permanent"));
85 let calls = Arc::new(AtomicUsize::new(0));
86 let mut sched = RetrySchedule::default();
87 let t = now();
88
89 let c = calls.clone();
90 let first = res!(spool.sweep(&mut sched, t, move |_m| {
91 let c = c.clone();
92 async move {
93 c.fetch_add(1, Ordering::SeqCst);
94 let r: Outcome<String> = Err(err!(
95 "RCPT TO:<bob@far.test> rejected: 550 5.4.1 Recipient address rejected";
96 IO, Network, Wire, Permanent));
97 r
98 }
99 }).await);
100 req!(Sweep { delivered: 0, failed: 1, deferred: 0, waiting: 0 }, first,
101 "a 550 did not end the message");
102 req!(0, res!(queued(&spool)), "the refused message is still queued");
103 req!(1, failed_files(&root), "the refused message was not kept in failed/");
104
105 // Another sweep, a day on: nothing is left to try.
106 let c = calls.clone();
107 let later = res!(spool.sweep(&mut sched, t + 86_400, move |_m| {
108 let c = c.clone();
109 async move {
110 c.fetch_add(1, Ordering::SeqCst);
111 let r: Outcome<String> = Ok(fmt!("unexpected"));
112 r
113 }
114 }).await);
115 req!(Sweep::default(), later, "a later sweep found something to do");
116 req!(1, calls.load(Ordering::SeqCst), "the far end was asked again after a 550");
117
118 let _ = fs::remove_dir_all(&root);
119 Ok(())
120}
121
122/// The retry that remains is a retry, not a loop: it waits, and the wait grows.
123#[tokio::test]
124async fn a_transient_failure_waits_before_the_next_try() -> Outcome<()> {
125 let (spool, root) = res!(spool_with_one("transient"));
126 let calls = Arc::new(AtomicUsize::new(0));
127 let mut sched = RetrySchedule::default();
128 let t = now();
129
130 let first = res!(greylisted(&spool, &mut sched, t, &calls).await);
131 req!(Sweep { delivered: 0, failed: 0, deferred: 1, waiting: 0 }, first,
132 "a 451 was not deferred");
133
134 // The worker sweeps every thirty seconds; the message must not be tried on each one.
135 let too_soon = res!(greylisted(&spool, &mut sched, t + 30, &calls).await);
136 req!(Sweep { delivered: 0, failed: 0, deferred: 0, waiting: 1 }, too_soon,
137 "a deferred message was tried again thirty seconds later");
138 req!(1, calls.load(Ordering::SeqCst), "the far end was asked again before the wait was up");
139
140 let due = res!(greylisted(&spool, &mut sched, t + retry_after(1), &calls).await);
141 req!(Sweep { delivered: 0, failed: 0, deferred: 1, waiting: 0 }, due,
142 "a deferred message was not tried once its wait was up");
143 req!(2, calls.load(Ordering::SeqCst), "the second attempt did not happen");
144 req!(1, res!(queued(&spool)), "a transient failure dropped the message");
145 req!(0, failed_files(&root), "a transient failure was set aside as final");
146
147 let _ = fs::remove_dir_all(&root);
148 Ok(())
149}
150
151/// A greylist that never lifts, or a host that is gone for good, must not hold a message for ever.
152#[tokio::test]
153async fn a_message_that_never_gets_through_is_given_up_on() -> Outcome<()> {
154 let (spool, root) = res!(spool_with_one("stale"));
155 let mut sched = RetrySchedule::default();
156
157 let out = res!(spool.sweep(&mut sched, now() + GIVE_UP_SECS + 60, |_m| async move {
158 let r: Outcome<String> = Err(err!("Connection timed out."; IO, Network, Timeout));
159 r
160 }).await);
161 req!(Sweep { delivered: 0, failed: 1, deferred: 0, waiting: 0 }, out,
162 "a message older than the give-up time stayed queued");
163 req!(0, res!(queued(&spool)), "the stale message is still queued");
164 req!(1, failed_files(&root), "the stale message was not kept in failed/");
165
166 let _ = fs::remove_dir_all(&root);
167 Ok(())
168}
169
170/// The ordinary case, so the fix cannot have broken it.
171#[tokio::test]
172async fn a_delivered_message_leaves_the_queue_and_is_not_set_aside() -> Outcome<()> {
173 let (spool, root) = res!(spool_with_one("delivered"));
174 let mut sched = RetrySchedule::default();
175
176 let out = res!(spool.sweep(&mut sched, now(), |_m| async move {
177 let r: Outcome<String> = Ok(fmt!("250 queued as 1234"));
178 r
179 }).await);
180 req!(Sweep { delivered: 1, failed: 0, deferred: 0, waiting: 0 }, out,
181 "an accepted message was not counted as delivered");
182 req!(0, res!(queued(&spool)), "an accepted message is still queued");
183 req!(0, failed_files(&root), "an accepted message was set aside as failed");
184
185 let _ = fs::remove_dir_all(&root);
186 Ok(())
187}
188
189#[test]
190fn the_wait_doubles_from_a_minute_to_a_ceiling() -> Outcome<()> {
191 req!(60, retry_after(1), "first wait");
192 req!(120, retry_after(2), "second wait");
193 req!(240, retry_after(3), "third wait");
194 req!(4 * 3_600, retry_after(30), "the ceiling");
195 req!(4 * 3_600, retry_after(u32::MAX), "the ceiling without overflow");
196 Ok(())
197}