Skip to main content

wse_client/
outbox.rs

1//! The client's outbox: every message it sends, except a live or signed
2//! one, waits in it until the server answers for it, and goes again after a
3//! reconnect.
4//!
5//! A server that answers (`acks`, from 3.2) confirms what its application
6//! took (`ack`, up to and with a number) and says what it refused (`nack`).
7//! The messages go out in order, numbered (`s`, first in the object) per
8//! producer, one producer per outbox, and the server drops a repeat, so the
9//! application takes each message once. A server that does not answer takes
10//! a message once it is written, as before.
11//!
12//! The connection's task owns the outbox: the send path takes no lock, and
13//! a message's text is shared with the writer, not copied. Only its room is
14//! shared with the client's handles, so that a send is refused at once when
15//! it is full.
16//!
17//! In a file, the outbox outlives the application. The file is a log of
18//! lines, each `<crc32 of the salt and the JSON, 8 hex digits> <JSON>`: the
19//! first line names the file's salt, producer and next number, then come a
20//! message kept (its text as it is), the highest number written, an `ack`,
21//! messages dropped, the last user and node. It is read up to the first line
22//! that does not check (a tail cut short as the application ended), and
23//! written anew under a new salt when it is opened and once what went
24//! outweighs what is kept. A thread of its own writes it, the lines of a
25//! batch at once and to the disk right after, so the connection's task never
26//! waits for the disk.
27
28use std::collections::VecDeque;
29use std::sync::Arc;
30use std::sync::atomic::{AtomicUsize, Ordering};
31use std::time::Duration;
32
33use bytestring::ByteString;
34use serde::Serialize;
35use web_time::Instant;
36
37/// How many messages wait at most, by default: the bytes bound memory, and
38/// a count too small would hold a busy connection back, the server's answers
39/// taking a while to come back.
40const MAX_MESSAGES: usize = 100_000;
41
42/// How many bytes of messages wait at most, by default.
43const MAX_BYTES: usize = 16 * 1024 * 1024;
44
45/// How long a message waits to be sent, by default.
46const MAX_AGE: Duration = Duration::from_secs(3600);
47
48/// The client's outbox ([`Config::offline`](crate::Config::offline)): where
49/// the messages wait until the server answers for them, and how many.
50#[derive(Clone, Debug)]
51#[non_exhaustive]
52pub struct Offline {
53    /// The file they wait in, so that they outlive the application (outside
54    /// the browser). It is this client's alone while it runs; a lock file
55    /// beside it (`<path>.lock`) says so. None keeps them in memory, for the
56    /// client's life.
57    #[cfg(not(target_arch = "wasm32"))]
58    pub path: Option<std::path::PathBuf>,
59    /// The IndexedDB database they wait in, so that they outlive the page
60    /// (in the browser): each tab keeps a producer of its own there, held
61    /// by a Web Lock, and a page that opens takes over what a closed tab
62    /// left. None keeps them in memory, for the client's life.
63    #[cfg(target_arch = "wasm32")]
64    pub database: Option<String>,
65    /// How many wait at most (100,000), those written and not answered yet
66    /// included: past it [`Client::send`](crate::Client::send) waits for
67    /// room, and [`Client::try_send`](crate::Client::try_send) fails with
68    /// [`Error::Full`](crate::Error::Full).
69    pub max_messages: usize,
70    /// How many bytes of messages wait at most (16 MiB), as
71    /// `max_messages`. A message larger than this fails at once with
72    /// [`Error::Full`](crate::Error::Full).
73    pub max_bytes: usize,
74    /// How long a message waits to be sent (an hour), unless it says
75    /// otherwise: one not sent by then is dropped and the application told.
76    /// Once written it waits for the server's answer whatever its age.
77    pub max_age: Duration,
78}
79
80impl Offline {
81    /// Kept in memory, for the client's life.
82    pub fn in_memory() -> Self {
83        Self {
84            #[cfg(not(target_arch = "wasm32"))]
85            path: None,
86            #[cfg(target_arch = "wasm32")]
87            database: None,
88            max_messages: MAX_MESSAGES,
89            max_bytes: MAX_BYTES,
90            max_age: MAX_AGE,
91        }
92    }
93
94    /// Kept in the file at `path`, past the application's end.
95    #[cfg(not(target_arch = "wasm32"))]
96    pub fn in_file(path: impl Into<std::path::PathBuf>) -> Self {
97        Self {
98            path: Some(path.into()),
99            ..Self::in_memory()
100        }
101    }
102
103    /// Kept in the IndexedDB database `name`, past the page's end (in the
104    /// browser). Without IndexedDB or Web Locks (a page without HTTPS) the
105    /// outbox stays in memory, and the client says so.
106    #[cfg(target_arch = "wasm32")]
107    pub fn in_indexed_db(name: impl Into<String>) -> Self {
108        Self {
109            database: Some(name.into()),
110            ..Self::in_memory()
111        }
112    }
113}
114
115impl Default for Offline {
116    fn default() -> Self {
117        Self::in_memory()
118    }
119}
120
121/// Room the client leaves before a kept message's JSON for its number:
122/// `{"s":` and at most 20 digits. The number is written in it, and the text
123/// never copied for it.
124pub(crate) const HEADROOM: usize = 25;
125
126/// Says what failed in the file: the operation and why.
127pub(crate) type Report = Arc<dyn Fn(&'static str, String) + Send + Sync>;
128
129/// Wakes the connection's task: the store wrote what messages waited for.
130pub(crate) type Wake = Arc<dyn Fn() + Send + Sync>;
131
132/// The outbox's room, shared with the client's handles: a send takes its
133/// place before it is queued, so a full outbox refuses it at once, or the
134/// send waits until a place is given back.
135pub(crate) struct Room {
136    count: AtomicUsize,
137    bytes: AtomicUsize,
138    max_messages: usize,
139    max_bytes: usize,
140    /// Sends that wait for a place, woken when one is given back.
141    waiting: AtomicUsize,
142    freed: tokio::sync::Notify,
143    /// The client ended: no send waits any longer.
144    closed: std::sync::atomic::AtomicBool,
145}
146
147impl Room {
148    pub(crate) fn new(offline: &Offline) -> Self {
149        Self {
150            count: AtomicUsize::new(0),
151            bytes: AtomicUsize::new(0),
152            max_messages: offline.max_messages.max(1),
153            max_bytes: offline.max_bytes.max(1),
154            waiting: AtomicUsize::new(0),
155            freed: tokio::sync::Notify::new(),
156            closed: std::sync::atomic::AtomicBool::new(false),
157        }
158    }
159
160    /// Waits for a place for a message of `bytes`. Fails with
161    /// [`Error::Full`](crate::Error::Full) for one larger than the whole
162    /// room, and [`Error::Closed`](crate::Error::Closed) once the client
163    /// ended.
164    pub(crate) async fn wait_take(&self, bytes: usize) -> Result<(), crate::Error> {
165        if bytes > self.max_bytes {
166            return Err(crate::Error::Full);
167        }
168        loop {
169            if self.closed.load(Ordering::Acquire) {
170                return Err(crate::Error::Closed);
171            }
172            if self.take(bytes) {
173                return Ok(());
174            }
175            let freed = self.freed.notified();
176            tokio::pin!(freed);
177            // Woken by any place given back from here on.
178            freed.as_mut().enable();
179            self.waiting.fetch_add(1, Ordering::AcqRel);
180            let taken = !self.closed.load(Ordering::Acquire) && self.take(bytes);
181            if !taken {
182                freed.await;
183            }
184            self.waiting.fetch_sub(1, Ordering::AcqRel);
185            if taken {
186                return Ok(());
187            }
188        }
189    }
190
191    /// The client ended: the sends that wait for a place stop waiting.
192    pub(crate) fn close(&self) {
193        self.closed.store(true, Ordering::Release);
194        self.freed.notify_waiters();
195    }
196
197    /// A place for a message of `bytes`; false when there is none.
198    pub(crate) fn take(&self, bytes: usize) -> bool {
199        if self.count.fetch_add(1, Ordering::AcqRel) >= self.max_messages {
200            self.count.fetch_sub(1, Ordering::AcqRel);
201            return false;
202        }
203        if self.bytes.fetch_add(bytes, Ordering::AcqRel) + bytes > self.max_bytes {
204            self.bytes.fetch_sub(bytes, Ordering::AcqRel);
205            self.count.fetch_sub(1, Ordering::AcqRel);
206            return false;
207        }
208        true
209    }
210
211    /// A place whatever the limits: a message read from the file, which is
212    /// not dropped because the limits shrank.
213    #[cfg_attr(target_arch = "wasm32", allow(dead_code))]
214    fn force(&self, bytes: usize) {
215        self.count.fetch_add(1, Ordering::AcqRel);
216        self.bytes.fetch_add(bytes, Ordering::AcqRel);
217    }
218
219    /// A message of `bytes` left: a send that waits takes its place.
220    pub(crate) fn give_back(&self, bytes: usize) {
221        self.give_back_many(1, bytes);
222    }
223
224    /// `count` messages of `bytes` in all left, given back at once: the
225    /// counters are shared with the sending threads.
226    pub(crate) fn give_back_many(&self, count: usize, bytes: usize) {
227        if count == 0 {
228            return;
229        }
230        self.count.fetch_sub(count, Ordering::AcqRel);
231        self.bytes.fetch_sub(bytes, Ordering::AcqRel);
232        if self.waiting.load(Ordering::Acquire) > 0 {
233            self.freed.notify_waiters();
234        }
235    }
236
237    /// How many wait.
238    pub(crate) fn count(&self) -> usize {
239        self.count.load(Ordering::Acquire)
240    }
241}
242
243/// A message kept.
244#[derive(Clone, Debug)]
245struct Record {
246    s: u64,
247    id: Box<str>,
248    /// When it was sent, in milliseconds since the Unix epoch.
249    #[cfg_attr(target_arch = "wasm32", allow(dead_code))]
250    at: u64,
251    /// Until when it may wait to be written first; once written, it waits
252    /// for the answer whatever its age.
253    until: u64,
254    /// Sent after a connection of the last user: it is that user's, and goes
255    /// nowhere once the server takes a connection for another. False: sent
256    /// before any connection, or after one without a user; it goes on any.
257    owned: bool,
258    /// Topics each continuous right before it goes (`requires`), every time
259    /// it goes: once more after a reconnect too.
260    requires: Option<Box<[String]>>,
261    /// The message as it goes, numbered: `{"s":N,` before the rest. Shared
262    /// with the writer, not copied.
263    text: ByteString,
264    /// Its place in the room: the size of the message as it was sent.
265    size: usize,
266    /// A bridge owner's window that sent it ([`Tags`]), in memory alone: 0
267    /// for the application's own.
268    tag: u64,
269}
270
271/// A bridge owner of this crate: told what became of its windows' sends,
272/// each tagged with its window, and asked whether a window holds one back.
273pub(crate) trait Tags: Send + Sync {
274    /// What became of tagged messages, in the order it came.
275    fn outcomes(&self, outcomes: Vec<Tagged>);
276    /// A message of window `tag` that requires `topics` waits while this
277    /// says one of them is not continuous for it.
278    fn holds_back(&self, tag: u64, topics: &[String]) -> bool;
279}
280
281/// What became of a tagged message: delivered (`reason` None), or not and why.
282#[derive(Debug, PartialEq, Eq)]
283pub(crate) struct Tagged {
284    pub(crate) tag: u64,
285    pub(crate) id: String,
286    pub(crate) reason: Option<String>,
287}
288
289/// What became of messages: delivered, or not and why.
290#[derive(Debug, Default, PartialEq, Eq)]
291pub(crate) struct Outcomes {
292    pub(crate) delivered: Vec<String>,
293    /// Not delivered: the reason, and the ids.
294    pub(crate) not_delivered: Vec<(String, Vec<String>)>,
295    /// Those of tagged messages, in the order they came: theirs alone, the
296    /// application's events do not name them.
297    pub(crate) tagged: Vec<Tagged>,
298}
299
300impl Outcomes {
301    fn merge(&mut self, other: Self) {
302        self.delivered.extend(other.delivered);
303        for (why, ids) in other.not_delivered {
304            match self.not_delivered.iter_mut().find(|(w, _)| *w == why) {
305                Some((_, mine)) => mine.extend(ids),
306                None => self.not_delivered.push((why, ids)),
307            }
308        }
309        self.tagged.extend(other.tagged);
310    }
311
312    /// `id` (of window `tag`, or the application's: 0) was delivered.
313    pub(crate) fn deliver(&mut self, id: Box<str>, tag: u64) {
314        if tag == 0 {
315            self.delivered.push(id.into());
316        } else {
317            self.tagged.push(Tagged {
318                tag,
319                id: id.into(),
320                reason: None,
321            });
322        }
323    }
324
325    /// `id` (of window `tag`, or the application's: 0) was not delivered,
326    /// for `why`.
327    pub(crate) fn refuse(&mut self, why: &str, id: Box<str>, tag: u64) {
328        if tag != 0 {
329            self.tagged.push(Tagged {
330                tag,
331                id: id.into(),
332                reason: Some(why.to_owned()),
333            });
334            return;
335        }
336        match self.not_delivered.iter_mut().find(|(w, _)| w == why) {
337            Some((_, ids)) => ids.push(id.into()),
338            None => self.not_delivered.push((why.to_owned(), vec![id.into()])),
339        }
340    }
341}
342
343/// The outbox, owned by the connection's task.
344pub(crate) struct Outbox {
345    records: VecDeque<Record>,
346    /// The next message's number: above every number used before.
347    next: u64,
348    /// Records before this index went to the connection's writer.
349    cursor: usize,
350    producer: Box<str>,
351    /// The node the client was connected to last, as it named itself.
352    node: Option<Box<str>>,
353    /// The highest number handed to a connection: one above it never
354    /// reached a server.
355    written: u64,
356    /// Written ahead: the highest number the store was handed the mark of
357    /// (that up to it, messages may have gone). A numbered message goes once
358    /// the store wrote its mark ([`Log::written`]), so that after a crash
359    /// its number is never another's.
360    promised: u64,
361    /// The highest number written to the current connection's socket.
362    flushed: u64,
363    /// The user of the last connection, once there was one.
364    user: Option<Option<String>>,
365    /// The server took the current connection.
366    ready: bool,
367    /// The server answered the hello on the current connection: messages
368    /// go numbered and wait for its `ack`.
369    answered: bool,
370    /// The server of the current connection does not answer: a message
371    /// written is gone.
372    plain: bool,
373    /// Written on the current connection and cleared before the server
374    /// answered for them: its answer on it says what became of them, and
375    /// they are not delivered when it ends first.
376    limbo: VecDeque<(u64, Box<str>, u64)>,
377    /// When the first message not written yet runs out (ms since the
378    /// epoch), u64::MAX for none, and when that is for the connection's
379    /// timer.
380    earliest: u64,
381    deadline: Option<Instant>,
382    max_age_ms: u64,
383    room: Arc<Room>,
384    /// Where what waits outlives the client: a file, or IndexedDB in the
385    /// browser.
386    log: Option<Log>,
387    /// The IndexedDB database to open before the first connection.
388    #[cfg(target_arch = "wasm32")]
389    database: Option<String>,
390    /// A write of the store failed: it is written anew, at most once a
391    /// second (`heal_at`, ms since the epoch), and when the outbox goes.
392    owed: bool,
393    heal_at: u64,
394    /// When a rewrite owed within that second can go: the driver wakes for
395    /// it ([`Outbox::deadline`]), whether or not anything changes meanwhile.
396    heal: Option<Instant>,
397    report: Report,
398    wake: Wake,
399}
400
401#[cfg(not(target_arch = "wasm32"))]
402use file::Log;
403#[cfg(target_arch = "wasm32")]
404use idb::Log;
405
406#[cfg(target_arch = "wasm32")]
407mod idb;
408
409impl Drop for Outbox {
410    fn drop(&mut self) {
411        if self.owed || self.log.as_ref().is_some_and(Log::take_broken) {
412            self.rewrite();
413        } else if let Some(log) = &mut self.log {
414            log.flush(self.promised);
415        }
416    }
417}
418
419/// What a store held when it was opened.
420struct Read {
421    producer: Box<str>,
422    node: Option<Box<str>>,
423    written: u64,
424    /// Above every number it names.
425    next: u64,
426    user: Option<String>,
427    records: VecDeque<Record>,
428}
429
430/// Lines of the file but a message kept, as written: borrowed, nothing
431/// copied.
432#[derive(Serialize)]
433#[serde(rename_all = "snake_case")]
434enum LineRef<'a> {
435    Written(u64),
436    Ack(u64),
437    Drop(&'a [u64]),
438    User(Option<&'a str>),
439    Node(&'a str),
440}
441
442/// The first line of a file, checked without a salt.
443#[derive(Serialize, serde::Deserialize)]
444#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
445struct Head<'a> {
446    wse_outbox: u32,
447    #[serde(borrow)]
448    salt: std::borrow::Cow<'a, str>,
449    #[serde(borrow)]
450    producer: std::borrow::Cow<'a, str>,
451    next: u64,
452}
453
454impl Outbox {
455    /// The outbox `offline` names, with `room` shared with the client's
456    /// handles. A file that cannot be opened is said (`report`) and the
457    /// outbox kept in memory; `wake` is called once the store wrote what
458    /// messages waited for.
459    pub(crate) fn open(offline: &Offline, room: Arc<Room>, report: Report, wake: Wake) -> Self {
460        let max_age_ms = u64::try_from(offline.max_age.as_millis()).unwrap_or(u64::MAX);
461        #[allow(unused_mut)]
462        let mut outbox = Self {
463            records: VecDeque::new(),
464            next: 1,
465            cursor: 0,
466            producer: new_id().into(),
467            node: None,
468            written: 0,
469            promised: 0,
470            flushed: 0,
471            user: None,
472            ready: false,
473            answered: false,
474            plain: false,
475            limbo: VecDeque::new(),
476            earliest: u64::MAX,
477            deadline: None,
478            max_age_ms,
479            room,
480            log: None,
481            #[cfg(target_arch = "wasm32")]
482            database: offline.database.clone(),
483            owed: false,
484            heal_at: 0,
485            heal: None,
486            report,
487            wake,
488        };
489        #[cfg(not(target_arch = "wasm32"))]
490        if let Some(path) = &offline.path {
491            match file::Log::open(path, Arc::clone(&outbox.report), Arc::clone(&outbox.wake)) {
492                Ok((log, read)) => {
493                    if let Some(read) = read {
494                        outbox.restore(read);
495                    }
496                    outbox.log = Some(log);
497                    outbox.rewrite();
498                }
499                Err(why) => (outbox.report)("open", why),
500            }
501        }
502        outbox
503    }
504
505    /// Opens the IndexedDB database the outbox is kept in, before the first
506    /// connection (it opens in its own time): takes over a producer it holds
507    /// that no tab holds, else makes one, and takes up what it holds. One
508    /// that cannot be opened is said, and the outbox kept in memory.
509    #[cfg(target_arch = "wasm32")]
510    pub(crate) async fn open_store(&mut self) {
511        let Some(name) = self.database.take() else {
512            return;
513        };
514        match idb::open(&name, Arc::clone(&self.report), Arc::clone(&self.wake)).await {
515            Ok((log, read)) => {
516                self.restore(read);
517                self.log = Some(log);
518            }
519            Err(why) => (self.report)("open", why),
520        }
521    }
522
523    /// Takes up what the store held.
524    fn restore(&mut self, read: Read) {
525        self.producer = read.producer;
526        self.node = read.node;
527        self.written = read.written;
528        self.promised = read.written;
529        self.user = read.user.map(Some);
530        self.next = read.next;
531        for record in read.records {
532            self.room.force(record.size);
533            self.earliest = self.earliest.min(record.until);
534            self.records.push_back(record);
535        }
536        self.set_deadline(now_ms());
537    }
538
539    /// What the client's hello says of it: the producer, the node it was
540    /// on, the highest number written, and the first it waits to hear of
541    /// (the server forgets the refusals before it).
542    pub(crate) fn hello(&self) -> serde_json::Value {
543        serde_json::json!({
544            "producer": &*self.producer,
545            "node": self.node.as_deref(),
546            "written": self.written,
547            "from": self.records.front().map_or(self.next, |r| r.s),
548        })
549    }
550
551    /// How many messages wait.
552    #[cfg(test)]
553    pub(crate) fn len(&self) -> usize {
554        self.records.len()
555    }
556
557    /// When the driver is to wake for the outbox: the first message not
558    /// written yet runs out, or a rewrite owed can go.
559    pub(crate) fn deadline(&self) -> Option<Instant> {
560        match (self.deadline, self.heal) {
561            (Some(a), Some(b)) => Some(a.min(b)),
562            (a, b) => a.or(b),
563        }
564    }
565
566    /// A rewrite owed whose time came goes.
567    pub(crate) fn heal(&mut self, now: Instant) {
568        if self.heal.is_some_and(|at| at <= now) {
569            self.heal = None;
570            self.flush();
571        }
572    }
573
574    /// Keeps a message the application sent, `text` a JSON object after
575    /// [`HEADROOM`] spaces, its place of `size` bytes in the room taken
576    /// already: its number. It goes at the next [`Outbox::due`].
577    #[allow(clippy::too_many_arguments)]
578    pub(crate) fn add(
579        &mut self,
580        id: Box<str>,
581        text: String,
582        size: usize,
583        ttl: Option<Duration>,
584        now: u64,
585        requires: Option<Box<[String]>>,
586        tag: u64,
587    ) -> u64 {
588        let s = self.next;
589        self.next += 1;
590        let wait = ttl.map_or(self.max_age_ms, |ttl| {
591            u64::try_from(ttl.as_millis()).unwrap_or(u64::MAX)
592        });
593        let record = Record {
594            s,
595            id,
596            at: now,
597            until: now.saturating_add(wait),
598            owned: matches!(self.user, Some(Some(_))),
599            requires,
600            text: number_in_place(s, text),
601            size,
602            tag,
603        };
604        if record.until < self.earliest {
605            self.earliest = record.until;
606            self.set_deadline(now);
607        }
608        if let Some(log) = &mut self.log {
609            log.push_add(&record);
610        }
611        self.records.push_back(record);
612        self.changed();
613        s
614    }
615
616    /// The server took a connection of `user`: what was kept for another
617    /// user goes, and what waited too long to be sent.
618    pub(crate) fn connected(&mut self, user: Option<&str>, now: u64) -> Outcomes {
619        let mut outcomes = Outcomes::default();
620        self.ready = true;
621        if let Some(last) = &self.user
622            && last.as_deref() != user
623        {
624            // What was sent before any connection goes on any, until a
625            // connection wrote it: then it was that connection's user's (the
626            // server may have taken it as theirs), and it never goes as
627            // another's.
628            let written = self.written;
629            self.remove_where("user_changed", &mut outcomes, |_, r| {
630                r.owned || r.s <= written
631            });
632            self.user = Some(user.map(str::to_owned));
633            self.put(&LineRef::User(user));
634        } else if self.user.is_none() {
635            self.user = Some(user.map(str::to_owned));
636            self.put(&LineRef::User(user));
637        }
638        if self.earliest <= now {
639            self.expire(now, &mut outcomes);
640        }
641        self.changed();
642        outcomes
643    }
644
645    /// The server of this connection answers numbered messages (`acks`),
646    /// or it does not.
647    pub(crate) fn server_answers(&mut self, answers: bool) {
648        self.plain = !answers;
649    }
650
651    /// The server's answer to the hello: what it decided, the refusals
652    /// among that (first, last, why), and its name. What it decided is
653    /// delivered or not, and the rest goes again, in order. It may come
654    /// before the server took the connection or after.
655    pub(crate) fn answered(
656        &mut self,
657        acked: u64,
658        refused: &[(u64, u64, &str)],
659        node: &str,
660        unsure: u64,
661    ) -> Outcomes {
662        let mut outcomes = Outcomes::default();
663        for (from, to, why) in refused {
664            outcomes.merge(self.refused(*from, *to, why));
665        }
666        outcomes.merge(self.acked(acked));
667        // Past both, the server took none of what may have gone: those are
668        // like any not written yet, and what ran out goes as expired (the
669        // next time they are due).
670        let may_have_gone = acked.max(self.written.min(unsure));
671        if may_have_gone < self.written {
672            self.written = may_have_gone;
673            self.rearm(now_ms());
674        }
675        self.answered = true;
676        if self.node.as_deref() != Some(node) {
677            self.node = Some(node.into());
678            self.put(&LineRef::Node(node));
679        }
680        self.changed();
681        outcomes
682    }
683
684    /// The server took every message up to and with `s`.
685    pub(crate) fn acked(&mut self, s: u64) -> Outcomes {
686        let mut outcomes = Outcomes::default();
687        let (gone, bytes) = self.take_front(s, &mut outcomes);
688        self.room.give_back_many(gone, bytes);
689        self.cursor = self.cursor.saturating_sub(gone);
690        while self.limbo.front().is_some_and(|(n, _, _)| *n <= s) {
691            if let Some((_, id, tag)) = self.limbo.pop_front() {
692                outcomes.deliver(id, tag);
693            }
694        }
695        if gone > 0 {
696            self.put(&LineRef::Ack(s));
697            self.changed();
698        }
699        outcomes
700    }
701
702    /// The server refused the messages `from` to `to` for good, for `why`.
703    pub(crate) fn refused(&mut self, from: u64, to: u64, why: &str) -> Outcomes {
704        let mut outcomes = Outcomes::default();
705        self.remove_where(why, &mut outcomes, |_, r| (from..=to).contains(&r.s));
706        self.limbo.retain_mut(|(n, id, tag)| {
707            let keep = !(from..=to).contains(n);
708            if !keep {
709                outcomes.refuse(why, std::mem::take(id), *tag);
710            }
711            keep
712        });
713        self.changed();
714        outcomes
715    }
716
717    /// Hands the connection's writer (`out`) the messages it did not take
718    /// yet, in order, each with its number, once the server took the
719    /// connection and its mode is known; then the file gets the batch's
720    /// lines. What waited too long to be sent goes instead.
721    pub(crate) fn due(
722        &mut self,
723        now: u64,
724        outcomes: &mut Outcomes,
725        continuous: impl Fn(&[String], u64) -> bool,
726        mut out: impl FnMut(u64, ByteString),
727    ) {
728        if self.ready && (self.answered || self.plain) {
729            if self.earliest <= now {
730                self.expire(now, outcomes);
731            }
732            if self.cursor < self.records.len() {
733                let numbered = self.answered;
734                // What the store wrote that messages up to may have gone;
735                // None: no store to wait for (none, or one that fails).
736                let stored = self
737                    .log
738                    .as_ref()
739                    .filter(|log| numbered && !log.behind())
740                    .map(Log::written);
741                let mut ahead = false;
742                let mut last = 0;
743                for record in self.records.range(self.cursor..) {
744                    // Its topics not continuous on this connection (a
745                    // reconnect's resync not done): it waits in its place,
746                    // and what comes after it with it.
747                    if record
748                        .requires
749                        .as_deref()
750                        .is_some_and(|t| !continuous(t, record.tag))
751                    {
752                        break;
753                    }
754                    // Written ahead: it goes once the store wrote the mark.
755                    if stored.is_some_and(|stored| record.s > stored) {
756                        ahead = true;
757                        break;
758                    }
759                    let text = if numbered {
760                        record.text.clone()
761                    } else {
762                        unnumbered(&record.text).into()
763                    };
764                    out(record.s, text);
765                    last = record.s;
766                    self.cursor += 1;
767                }
768                if numbered && last > self.written {
769                    self.written = last;
770                }
771                // The mark for every message kept now, in this batch: they
772                // go once the store wrote it.
773                let kept = self.records.back().map_or(0, |r| r.s);
774                if ahead && kept > self.promised {
775                    self.promised = kept;
776                    self.put(&LineRef::Written(kept));
777                }
778            }
779        }
780        self.flush();
781    }
782
783    /// The connection wrote the messages up to and with `last` to its
784    /// socket. A server that does not answer took them.
785    pub(crate) fn wrote(&mut self, last: u64) -> Outcomes {
786        let mut outcomes = Outcomes::default();
787        self.flushed = self.flushed.max(last);
788        if self.plain && !self.answered {
789            let (gone, bytes) = self.take_front(last, &mut outcomes);
790            self.room.give_back_many(gone, bytes);
791            self.cursor = self.cursor.saturating_sub(gone);
792            if gone > 0 {
793                self.put(&LineRef::Ack(last));
794                self.changed();
795            }
796        }
797        outcomes
798    }
799
800    /// Takes the records up to and with `s` off the front, delivered: how
801    /// many, and their bytes.
802    fn take_front(&mut self, s: u64, outcomes: &mut Outcomes) -> (usize, usize) {
803        let (mut gone, mut bytes) = (0, 0);
804        while self.records.front().is_some_and(|r| r.s <= s) {
805            if let Some(record) = self.records.pop_front() {
806                bytes += record.size;
807                outcomes.deliver(record.id, record.tag);
808                gone += 1;
809            }
810        }
811        (gone, bytes)
812    }
813
814    /// The server owes an answer on this connection: for messages it has
815    /// in hand, or to the hello, while messages wait for it to go.
816    pub(crate) fn owed(&self) -> bool {
817        if self.answered {
818            !self.limbo.is_empty() || self.records.front().is_some_and(|r| r.s <= self.flushed)
819        } else {
820            self.ready && !self.plain && !self.records.is_empty()
821        }
822    }
823
824    /// The client ends: in memory, what waits goes with it; in a file, it
825    /// waits for the next client.
826    pub(crate) fn close(&mut self) -> Outcomes {
827        let mut outcomes = self.disconnected();
828        if self.log.is_none() {
829            let (count, mut bytes) = (self.records.len(), 0);
830            for record in self.records.drain(..) {
831                bytes += record.size;
832                outcomes.refuse("closed", record.id, record.tag);
833            }
834            self.room.give_back_many(count, bytes);
835            self.earliest = u64::MAX;
836            self.deadline = None;
837        }
838        outcomes
839    }
840
841    /// The connection went: what waits goes on the next one. What was
842    /// cleared while the server had it in hand is not delivered.
843    pub(crate) fn disconnected(&mut self) -> Outcomes {
844        let mut outcomes = Outcomes::default();
845        self.ready = false;
846        self.answered = false;
847        self.plain = false;
848        self.cursor = 0;
849        self.flushed = 0;
850        for (_, id, tag) in self.limbo.drain(..) {
851            outcomes.refuse("cleared", id, tag);
852        }
853        self.flush();
854        outcomes
855    }
856
857    /// The user signed out: every message goes, and the user is forgotten.
858    /// One the server has in hand on this connection is delivered when it
859    /// confirms it on it, and not delivered when the connection ends first.
860    pub(crate) fn clear(&mut self) -> Outcomes {
861        let mut outcomes = Outcomes::default();
862        let in_hand = if self.answered { self.flushed } else { 0 };
863        let mut numbers = Vec::with_capacity(self.records.len());
864        let mut bytes = 0;
865        for mut record in self.records.drain(..) {
866            bytes += record.size;
867            numbers.push(record.s);
868            let id = std::mem::take(&mut record.id);
869            if record.s <= in_hand {
870                self.limbo.push_back((record.s, id, record.tag));
871            } else {
872                outcomes.refuse("cleared", id, record.tag);
873            }
874        }
875        self.room.give_back_many(numbers.len(), bytes);
876        self.cursor = 0;
877        self.earliest = u64::MAX;
878        self.deadline = None;
879        if !numbers.is_empty() {
880            self.put(&LineRef::Drop(&numbers));
881        }
882        self.user = None;
883        self.put(&LineRef::User(None));
884        self.changed();
885        outcomes
886    }
887
888    /// Drops what waited longer than it may to be written, and sets the
889    /// timer for the next to run out.
890    pub(crate) fn expire(&mut self, now: u64, outcomes: &mut Outcomes) {
891        let (written, cursor) = (self.written, self.cursor);
892        self.remove_where("expired", outcomes, |at, r| {
893            at >= cursor && r.s > written && r.until < now
894        });
895        self.rearm(now);
896        self.changed();
897    }
898
899    /// The timer, for the first message not written yet to run out.
900    fn rearm(&mut self, now: u64) {
901        let first = self.records.partition_point(|r| r.s <= self.written);
902        self.earliest = self
903            .records
904            .range(first..)
905            .map(|r| r.until)
906            .min()
907            .unwrap_or(u64::MAX);
908        self.set_deadline(now);
909    }
910
911    fn set_deadline(&mut self, now: u64) {
912        self.deadline = if self.earliest == u64::MAX {
913            None
914        } else {
915            Instant::now().checked_add(Duration::from_millis(self.earliest.saturating_sub(now)))
916        };
917    }
918
919    /// Removes the records `gone` names (by index and record), telling the
920    /// application why.
921    fn remove_where(
922        &mut self,
923        why: &str,
924        outcomes: &mut Outcomes,
925        mut gone: impl FnMut(usize, &Record) -> bool,
926    ) {
927        let mut numbers = Vec::new();
928        let cursor = self.cursor;
929        let (mut at, mut before, mut bytes) = (0, 0, 0);
930        self.records.retain_mut(|r| {
931            let keep = !gone(at, r);
932            if !keep {
933                bytes += r.size;
934                outcomes.refuse(why, std::mem::take(&mut r.id), r.tag);
935                numbers.push(r.s);
936                if at < cursor {
937                    before += 1;
938                }
939            }
940            at += 1;
941            keep
942        });
943        self.cursor -= before;
944        self.room.give_back_many(numbers.len(), bytes);
945        if !numbers.is_empty() {
946            self.put(&LineRef::Drop(&numbers));
947        }
948    }
949
950    /// Puts `line` in the store's batch.
951    fn put(&mut self, line: &LineRef<'_>) {
952        if let Some(log) = &mut self.log {
953            log.push(line);
954        }
955    }
956
957    /// Something changed: with no connection, the file gets it now; with
958    /// one, at the connection's next write ([`Outbox::due`]), a batch at
959    /// once.
960    fn changed(&mut self) {
961        if !self.ready {
962            self.flush();
963        }
964    }
965
966    /// Hands the batch's lines to the file's thread; writes the file anew
967    /// instead once what went outweighs what is kept, or after a write
968    /// failed (the lines are in what is written anew).
969    pub(crate) fn flush(&mut self) {
970        if let Some(log) = &mut self.log {
971            self.owed |= log.take_broken();
972            if self.owed {
973                let now = now_ms();
974                if now >= self.heal_at {
975                    self.owed = false;
976                    self.heal_at = now.saturating_add(1000);
977                    self.heal = None;
978                    self.rewrite();
979                } else {
980                    log.forget();
981                    self.heal = Some(Instant::now() + Duration::from_millis(self.heal_at - now));
982                }
983            } else if log.lines > 2 * self.records.len() + 64 {
984                self.rewrite();
985            } else {
986                log.flush(self.promised);
987            }
988        }
989    }
990
991    /// Writes the file anew, with what is kept now alone, under a new salt.
992    #[cfg(not(target_arch = "wasm32"))]
993    fn rewrite(&mut self) {
994        let Some(log) = &mut self.log else {
995            return;
996        };
997        let salt = new_id();
998        let head = Head {
999            wse_outbox: 1,
1000            salt: salt.as_str().into(),
1001            producer: (&*self.producer).into(),
1002            next: self.next,
1003        };
1004        let mut out = Vec::with_capacity(
1005            256 + self
1006                .records
1007                .iter()
1008                .map(|r| r.text.len() + 128)
1009                .sum::<usize>(),
1010        );
1011        file::encode(&mut out, b"", &head);
1012        let mut lines = 1;
1013        let salted = salt.as_bytes();
1014        if let Some(node) = &self.node {
1015            file::encode(&mut out, salted, &LineRef::Node(node));
1016            lines += 1;
1017        }
1018        // What went meanwhile, while the store failed, is in it.
1019        let mark = self.written.max(self.promised);
1020        self.promised = mark;
1021        if mark > 0 {
1022            file::encode(&mut out, salted, &LineRef::Written(mark));
1023            lines += 1;
1024        }
1025        if let Some(Some(user)) = &self.user {
1026            file::encode(&mut out, salted, &LineRef::User(Some(user)));
1027            lines += 1;
1028        }
1029        for record in &self.records {
1030            file::encode_add(&mut out, salted, record);
1031            lines += 1;
1032        }
1033        log.rewrite(out, salt, lines, mark);
1034    }
1035
1036    /// Writes the database anew, with what is kept now alone.
1037    #[cfg(target_arch = "wasm32")]
1038    fn rewrite(&mut self) {
1039        let Some(log) = &mut self.log else {
1040            return;
1041        };
1042        let user = match &self.user {
1043            Some(Some(user)) => Some(user.as_str()),
1044            _ => None,
1045        };
1046        // What went meanwhile, while the store failed, is in it.
1047        let mark = self.written.max(self.promised);
1048        self.promised = mark;
1049        log.rewrite(self.next, mark, self.node.as_deref(), user, &self.records);
1050    }
1051}
1052
1053/// The message `text` (its JSON object after [`HEADROOM`] spaces, as the
1054/// client makes it) with its number written in that room: `{"s":N` right
1055/// before the object, whose own `{` becomes `,`, or a space for an empty
1056/// object. The text is not copied, and what is left of the room skipped.
1057fn number_in_place(s: u64, mut text: String) -> ByteString {
1058    let bytes = text.as_bytes();
1059    if bytes.len() <= HEADROOM
1060        || bytes[HEADROOM] != b'{'
1061        || bytes[..HEADROOM].iter().any(|b| *b != b' ')
1062    {
1063        return numbered(s, &text).into();
1064    }
1065    let mut digits = [0u8; 20];
1066    let mut at = digits.len();
1067    let mut n = s;
1068    loop {
1069        at -= 1;
1070        digits[at] = b'0' + (n % 10) as u8;
1071        n /= 10;
1072        if n == 0 {
1073            break;
1074        }
1075    }
1076    let number = &digits[at..];
1077    let start = HEADROOM - 5 - number.len();
1078    let empty = bytes[HEADROOM + 1..]
1079        .iter()
1080        .find(|b| !b.is_ascii_whitespace())
1081        == Some(&b'}');
1082    let mut prefix = [b' '; HEADROOM + 1];
1083    prefix[start..start + 5].copy_from_slice(b"{\"s\":");
1084    prefix[start + 5..HEADROOM].copy_from_slice(number);
1085    prefix[HEADROOM] = if empty { b' ' } else { b',' };
1086    // ASCII over ASCII of the same length: written in place, the text
1087    // still UTF-8.
1088    if let Ok(prefix) = std::str::from_utf8(&prefix) {
1089        text.replace_range(..=HEADROOM, prefix);
1090    }
1091    let mut text = ByteString::from(text);
1092    text.advance(start);
1093    text
1094}
1095
1096/// A message with its number: `{"s":N,` before the rest of its JSON object.
1097fn numbered(s: u64, text: &str) -> String {
1098    let rest = text.trim_start();
1099    let rest = rest.strip_prefix('{').unwrap_or(rest);
1100    let mut out = String::with_capacity(text.len() + 24);
1101    out.push_str("{\"s\":");
1102    push_number(&mut out, s);
1103    if !rest.trim_start().starts_with('}') {
1104        out.push(',');
1105    }
1106    out.push_str(rest);
1107    out
1108}
1109
1110/// The message without its number, for a server that does not answer.
1111fn unnumbered(text: &str) -> String {
1112    let Some(rest) = text.strip_prefix("{\"s\":") else {
1113        return text.to_owned();
1114    };
1115    let rest = rest.trim_start_matches(|c: char| c.is_ascii_digit());
1116    let rest = rest.strip_prefix(',').unwrap_or(rest);
1117    let mut out = String::with_capacity(rest.len() + 1);
1118    out.push('{');
1119    out.push_str(rest);
1120    out
1121}
1122
1123/// Writes `n` in decimal, without the formatting machinery.
1124fn push_number(out: &mut String, n: u64) {
1125    // A u64 has 20 digits at most.
1126    let mut digits = [0u8; 20];
1127    let mut at = digits.len();
1128    let mut n = n;
1129    loop {
1130        at -= 1;
1131        digits[at] = b'0' + (n % 10) as u8;
1132        n /= 10;
1133        if n == 0 {
1134            break;
1135        }
1136    }
1137    for digit in &digits[at..] {
1138        out.push(char::from(*digit));
1139    }
1140}
1141
1142/// A random id: a producer's, a salt; 128 bits as 32 hex digits.
1143fn new_id() -> String {
1144    let (hi, lo) = crate::client::random_pair();
1145    format!("{hi:016x}{lo:016x}")
1146}
1147
1148/// Milliseconds since the Unix epoch.
1149pub(crate) fn now_ms() -> u64 {
1150    web_time::SystemTime::now()
1151        .duration_since(web_time::UNIX_EPOCH)
1152        .map_or(0, |d| u64::try_from(d.as_millis()).unwrap_or(u64::MAX))
1153}
1154
1155/// CRC-32 (IEEE) tables for eight bytes at a time: the table for a byte
1156/// followed by `k` zero bytes in row `k`.
1157#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
1158const CRC_TABLES: [[u32; 256]; 8] = {
1159    let mut tables = [[0u32; 256]; 8];
1160    let mut i = 0;
1161    while i < 256 {
1162        let mut c = i as u32;
1163        let mut k = 0;
1164        while k < 8 {
1165            c = if c & 1 != 0 {
1166                0xEDB8_8320 ^ (c >> 1)
1167            } else {
1168                c >> 1
1169            };
1170            k += 1;
1171        }
1172        tables[0][i] = c;
1173        i += 1;
1174    }
1175    let mut i = 0;
1176    while i < 256 {
1177        let mut k = 1;
1178        while k < 8 {
1179            let before = tables[k - 1][i];
1180            tables[k][i] = (before >> 8) ^ tables[0][(before & 0xFF) as usize];
1181            k += 1;
1182        }
1183        i += 1;
1184    }
1185    tables
1186};
1187
1188/// CRC-32 (IEEE) of `parts` one after another, as zlib computes it: what
1189/// checks a line of the file.
1190#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
1191fn crc32(parts: &[&[u8]]) -> u32 {
1192    let t = &CRC_TABLES;
1193    let mut crc = !0u32;
1194    for part in parts {
1195        let (chunks, rest) = part.as_chunks::<8>();
1196        for c in chunks {
1197            let lo = u32::from_le_bytes([c[0], c[1], c[2], c[3]]) ^ crc;
1198            let hi = u32::from_le_bytes([c[4], c[5], c[6], c[7]]);
1199            crc = t[7][(lo & 0xFF) as usize]
1200                ^ t[6][((lo >> 8) & 0xFF) as usize]
1201                ^ t[5][((lo >> 16) & 0xFF) as usize]
1202                ^ t[4][(lo >> 24) as usize]
1203                ^ t[3][(hi & 0xFF) as usize]
1204                ^ t[2][((hi >> 8) & 0xFF) as usize]
1205                ^ t[1][((hi >> 16) & 0xFF) as usize]
1206                ^ t[0][(hi >> 24) as usize];
1207        }
1208        for byte in rest {
1209            crc = t[0][((crc ^ u32::from(*byte)) & 0xFF) as usize] ^ (crc >> 8);
1210        }
1211    }
1212    !crc
1213}
1214
1215#[cfg(not(target_arch = "wasm32"))]
1216mod file {
1217    /// The lock every client takes of an outbox's file on Linux beside the
1218    /// lock file: a socket in the abstract namespace, named for the file,
1219    /// the one Node can take (it has no file locks); it goes with the
1220    /// process.
1221    #[cfg(any(target_os = "linux", target_os = "android"))]
1222    mod linux {
1223        // The same abstract namespace; Android's standard library names it apart.
1224        #[cfg(target_os = "android")]
1225        use std::os::android::net::SocketAddrExt;
1226        #[cfg(target_os = "linux")]
1227        use std::os::linux::net::SocketAddrExt;
1228        use std::os::unix::net::{SocketAddr, UnixListener};
1229        use std::path::Path;
1230
1231        use sha2::{Digest, Sha256};
1232
1233        /// `\0wse-outbox-` and 40 hex digits of the SHA-256 of the file's
1234        /// path, its directory's resolved.
1235        pub(super) fn name(path: &Path) -> std::io::Result<String> {
1236            let dir = path.parent().filter(|d| !d.as_os_str().is_empty());
1237            let dir = std::fs::canonicalize(dir.unwrap_or(Path::new(".")))?;
1238            let file = path.file_name().unwrap_or_default();
1239            let full = dir.join(file);
1240            let digest = Sha256::digest(full.as_os_str().as_encoded_bytes());
1241            let mut name = String::from("wse-outbox-");
1242            for byte in &digest[..20] {
1243                use std::fmt::Write;
1244                let _ = write!(name, "{byte:02x}");
1245            }
1246            Ok(name)
1247        }
1248
1249        #[cfg(test)]
1250        #[test]
1251        fn the_lock_is_named_as_the_other_clients_name_it() {
1252            assert_eq!(
1253                name(Path::new("/wse-outbox-test-queue")).unwrap(),
1254                "wse-outbox-0bd17a84322f83203c373a9c2d09c16e9474d9c6"
1255            );
1256        }
1257
1258        #[cfg(test)]
1259        #[test]
1260        fn a_file_another_process_holds_on_linux_is_another_clients() {
1261            let dir = std::env::temp_dir().join(format!("wse-lock-{}", std::process::id()));
1262            std::fs::create_dir_all(&dir).unwrap();
1263            let path = dir.join("queue");
1264            let held = named_lock(&path).unwrap();
1265            assert!(matches!(named_lock(&path), Err(None)));
1266            drop(held);
1267            assert!(named_lock(&path).is_ok());
1268            std::fs::remove_dir_all(&dir).unwrap();
1269        }
1270
1271        /// The lock, or None in the error when another process has it.
1272        pub(super) fn named_lock(path: &Path) -> Result<UnixListener, Option<std::io::Error>> {
1273            let name = name(path).map_err(Some)?;
1274            let addr = SocketAddr::from_abstract_name(name.as_bytes()).map_err(Some)?;
1275            UnixListener::bind_addr(&addr)
1276                .map_err(|e| (e.kind() != std::io::ErrorKind::AddrInUse).then_some(e))
1277        }
1278    }
1279
1280    use std::collections::VecDeque;
1281    use std::fs::{File, OpenOptions, TryLockError};
1282    use std::io::Write;
1283    use std::path::{Path, PathBuf};
1284    use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
1285    use std::sync::{Arc, mpsc};
1286
1287    use serde::{Deserialize, Serialize};
1288    use serde_json::value::RawValue;
1289
1290    use super::{Head, LineRef, Read, Record, Report, Wake, crc32};
1291
1292    /// A message kept, as read: its text as it was written.
1293    #[derive(Deserialize)]
1294    struct RecordIn<'a> {
1295        s: u64,
1296        id: Box<str>,
1297        at: u64,
1298        until: u64,
1299        #[serde(default)]
1300        owned: bool,
1301        #[serde(default)]
1302        requires: Option<Vec<String>>,
1303        #[serde(borrow)]
1304        text: &'a RawValue,
1305    }
1306
1307    /// A line, as read.
1308    #[derive(Deserialize)]
1309    #[serde(rename_all = "snake_case")]
1310    enum Line<'a> {
1311        #[serde(borrow)]
1312        Add(RecordIn<'a>),
1313        Written(u64),
1314        Ack(u64),
1315        Drop(Vec<u64>),
1316        User(Option<String>),
1317        Node(String),
1318    }
1319
1320    /// What the file's thread is handed, in order, each with the mark it
1321    /// holds (that messages up to it may have gone).
1322    enum Job {
1323        Append(Vec<u8>, u64),
1324        Rewrite(Vec<u8>, u64),
1325    }
1326
1327    /// The file, as the connection's task sees it: a batch of lines, handed
1328    /// to the thread that writes them.
1329    pub(super) struct Log {
1330        jobs: Option<mpsc::Sender<Job>>,
1331        thread: Option<std::thread::JoinHandle<()>>,
1332        salt: String,
1333        /// The lines of the batch not handed over yet.
1334        batch: Vec<u8>,
1335        /// Lines in the file, the batch's included.
1336        pub(super) lines: usize,
1337        /// A write failed: the file is to be written anew.
1338        broken: Arc<AtomicBool>,
1339        /// The highest mark the file holds, past a crash of the process.
1340        written: Arc<AtomicU64>,
1341        /// A write failed: what is appended waits for the file anew.
1342        behind: Arc<AtomicBool>,
1343    }
1344
1345    impl Log {
1346        /// Opens the file at `path`, the client's alone from now on, and
1347        /// what it holds (None: a new file, or not one of this format).
1348        pub(super) fn open(
1349            path: &Path,
1350            report: Report,
1351            wake: Wake,
1352        ) -> Result<(Self, Option<Read>), String> {
1353            let fail = |what: &str, e: std::io::Error| format!("{}: {what}: {e}", path.display());
1354            let mut lock_path = path.as_os_str().to_owned();
1355            lock_path.push(".lock");
1356            let lock = OpenOptions::new()
1357                .create(true)
1358                .truncate(false)
1359                .write(true)
1360                .open(&lock_path)
1361                .map_err(|e| fail("cannot open its lock file", e))?;
1362            match lock.try_lock() {
1363                Ok(()) => {}
1364                Err(TryLockError::WouldBlock) => {
1365                    return Err(format!("{} is another client's", path.display()));
1366                }
1367                Err(TryLockError::Error(e)) => return Err(fail("cannot lock it", e)),
1368            }
1369            #[cfg(any(target_os = "linux", target_os = "android"))]
1370            let named = linux::named_lock(path).map_err(|e| match e {
1371                Some(e) => fail("cannot lock it", e),
1372                None => format!("{} is another client's", path.display()),
1373            })?;
1374            let read = match std::fs::read(path) {
1375                Ok(bytes) => read(&bytes),
1376                Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
1377                Err(e) => return Err(fail("cannot read it", e)),
1378            };
1379            let file = append(path).map_err(|e| fail("cannot open it", e))?;
1380            let mut tmp = path.as_os_str().to_owned();
1381            tmp.push(".tmp");
1382            let broken = Arc::new(AtomicBool::new(false));
1383            let written = Arc::new(AtomicU64::new(read.as_ref().map_or(0, |r| r.written)));
1384            let behind = Arc::new(AtomicBool::new(false));
1385            let writer = Writer {
1386                path: path.to_owned(),
1387                tmp: PathBuf::from(tmp),
1388                file: Some(file),
1389                _lock: lock,
1390                #[cfg(any(target_os = "linux", target_os = "android"))]
1391                _named: named,
1392                report,
1393                broken: Arc::clone(&broken),
1394                written: Arc::clone(&written),
1395                wake,
1396                skipping: Arc::clone(&behind),
1397                failing: false,
1398            };
1399            let (jobs, taken) = mpsc::channel();
1400            let thread = std::thread::Builder::new()
1401                .name("wse-outbox".to_owned())
1402                .spawn(move || writer.run(&taken))
1403                .map_err(|e| fail("cannot start its thread", e))?;
1404            let log = Self {
1405                jobs: Some(jobs),
1406                thread: Some(thread),
1407                salt: String::new(),
1408                batch: Vec::new(),
1409                lines: 0,
1410                broken,
1411                written,
1412                behind,
1413            };
1414            Ok((log, read))
1415        }
1416
1417        /// Puts `line` in the batch.
1418        pub(super) fn push(&mut self, line: &LineRef<'_>) {
1419            encode(&mut self.batch, self.salt.as_bytes(), line);
1420            self.lines += 1;
1421        }
1422
1423        /// Puts a message kept in the batch.
1424        pub(super) fn push_add(&mut self, record: &Record) {
1425            encode_add(&mut self.batch, self.salt.as_bytes(), record);
1426            self.lines += 1;
1427        }
1428
1429        /// Hands the batch to the thread, with `mark`, the highest mark in
1430        /// the file once it is written.
1431        pub(super) fn flush(&mut self, mark: u64) {
1432            if self.batch.is_empty() {
1433                return;
1434            }
1435            let capacity = self.batch.capacity().min(64 * 1024);
1436            let batch = std::mem::replace(&mut self.batch, Vec::with_capacity(capacity));
1437            if let Some(jobs) = &self.jobs {
1438                let _ = jobs.send(Job::Append(batch, mark));
1439            }
1440        }
1441
1442        /// The highest mark the file holds, past a crash of the process.
1443        pub(super) fn written(&self) -> u64 {
1444            self.written.load(Ordering::Acquire)
1445        }
1446
1447        /// The file is behind: a write failed, and messages do not wait for
1448        /// it until it is written anew.
1449        pub(super) fn behind(&self) -> bool {
1450            self.behind.load(Ordering::Acquire)
1451        }
1452
1453        /// The batch is in what is written anew.
1454        pub(super) fn forget(&mut self) {
1455            self.batch.clear();
1456        }
1457
1458        /// A write failed since the last look: the file is to be written
1459        /// anew.
1460        pub(super) fn take_broken(&self) -> bool {
1461            self.broken.swap(false, Ordering::AcqRel)
1462        }
1463
1464        /// The file anew: `bytes`, `lines` lines under `salt`, `mark` the
1465        /// highest mark in it.
1466        pub(super) fn rewrite(&mut self, bytes: Vec<u8>, salt: String, lines: usize, mark: u64) {
1467            self.batch.clear();
1468            self.salt = salt;
1469            self.lines = lines;
1470            if let Some(jobs) = &self.jobs {
1471                let _ = jobs.send(Job::Rewrite(bytes, mark));
1472            }
1473        }
1474    }
1475
1476    impl Drop for Log {
1477        /// The thread writes what it was handed and ends; the file is free
1478        /// for the next client once it has.
1479        fn drop(&mut self) {
1480            self.jobs.take();
1481            if let Some(thread) = self.thread.take() {
1482                let _ = thread.join();
1483            }
1484        }
1485    }
1486
1487    /// The thread that writes the file.
1488    struct Writer {
1489        path: PathBuf,
1490        tmp: PathBuf,
1491        /// Appended to; None between a failed rewrite and the next.
1492        file: Option<File>,
1493        /// Held while the client runs: the file is its alone.
1494        _lock: File,
1495        /// The same, as the TypeScript client in Node takes it on Linux.
1496        #[cfg(any(target_os = "linux", target_os = "android"))]
1497        _named: std::os::unix::net::UnixListener,
1498        report: Report,
1499        broken: Arc<AtomicBool>,
1500        /// The highest mark written, and who hears it rose.
1501        written: Arc<AtomicU64>,
1502        wake: Wake,
1503        /// A write failed: what is appended waits for the file anew.
1504        skipping: Arc<AtomicBool>,
1505        /// The last failure was said; the next is said after a success.
1506        failing: bool,
1507    }
1508
1509    impl Writer {
1510        /// Writes what comes, each run of batches then synced to the disk
1511        /// at once.
1512        fn run(mut self, jobs: &mpsc::Receiver<Job>) {
1513            while let Ok(job) = jobs.recv() {
1514                let mut mark = 0;
1515                let mut dirty = self.take(job, &mut mark);
1516                while let Ok(job) = jobs.try_recv() {
1517                    dirty |= self.take(job, &mut mark);
1518                }
1519                // Written, past a crash of the process: what waited for the
1520                // mark may go; the disk has it once synced, at once after.
1521                if mark > self.written.fetch_max(mark, Ordering::AcqRel) {
1522                    (self.wake)();
1523                }
1524                if dirty
1525                    && let Some(file) = &self.file
1526                    && let Err(e) = file.sync_data()
1527                {
1528                    self.fail(&format!("cannot sync it to the disk: {e}"));
1529                }
1530            }
1531        }
1532
1533        /// True: appended, to be synced. `mark` takes the mark of what was
1534        /// written.
1535        fn take(&mut self, job: Job, mark: &mut u64) -> bool {
1536            match job {
1537                Job::Append(lines, holds) => {
1538                    if self.skipping.load(Ordering::Acquire) {
1539                        return false;
1540                    }
1541                    let written = match &mut self.file {
1542                        Some(file) => file.write_all(&lines),
1543                        None => Err(std::io::Error::other("no file")),
1544                    };
1545                    match written {
1546                        Ok(()) => {
1547                            *mark = (*mark).max(holds);
1548                            true
1549                        }
1550                        Err(e) => {
1551                            self.fail(&format!("cannot write it: {e}"));
1552                            false
1553                        }
1554                    }
1555                }
1556                Job::Rewrite(bytes, holds) => {
1557                    match self.rewrite(&bytes) {
1558                        Ok(()) => {
1559                            // Synced as it was written.
1560                            *mark = (*mark).max(holds);
1561                            self.skipping.store(false, Ordering::Release);
1562                            self.failing = false;
1563                        }
1564                        Err(e) => self.fail(&format!("cannot write it anew: {e}")),
1565                    }
1566                    false
1567                }
1568            }
1569        }
1570
1571        /// The file is behind: written anew at the next change, said once.
1572        fn fail(&mut self, why: &str) {
1573            self.skipping.store(true, Ordering::Release);
1574            self.broken.store(true, Ordering::Release);
1575            if !std::mem::replace(&mut self.failing, true) {
1576                (self.report)("write", format!("{}: {why}", self.path.display()));
1577            }
1578        }
1579
1580        /// The new file beside the old, on the disk, then in its place.
1581        fn rewrite(&mut self, bytes: &[u8]) -> std::io::Result<()> {
1582            let written = File::create(&self.tmp).and_then(|mut tmp| {
1583                tmp.write_all(bytes)?;
1584                tmp.sync_all()
1585            });
1586            if let Err(e) = written {
1587                let _ = std::fs::remove_file(&self.tmp);
1588                return Err(e);
1589            }
1590            // Closed before it is replaced: Windows does not replace an
1591            // open file everywhere.
1592            self.file = None;
1593            if let Err(e) = std::fs::rename(&self.tmp, &self.path) {
1594                let _ = std::fs::remove_file(&self.tmp);
1595                self.file = append(&self.path).ok();
1596                return Err(e);
1597            }
1598            sync_dir(&self.path);
1599            self.file = Some(append(&self.path)?);
1600            Ok(())
1601        }
1602    }
1603
1604    fn append(path: &Path) -> std::io::Result<File> {
1605        OpenOptions::new().create(true).append(true).open(path)
1606    }
1607
1608    /// The directory's entry of a file renamed into it, on the disk.
1609    fn sync_dir(path: &Path) {
1610        #[cfg(unix)]
1611        if let Some(dir) = path.parent() {
1612            let dir = if dir.as_os_str().is_empty() {
1613                Path::new(".")
1614            } else {
1615                dir
1616            };
1617            if let Ok(dir) = File::open(dir) {
1618                let _ = dir.sync_all();
1619            }
1620        }
1621        #[cfg(not(unix))]
1622        let _ = path;
1623    }
1624
1625    /// Appends `line` to `out`: its check, a space, its JSON, a newline.
1626    pub(super) fn encode(out: &mut Vec<u8>, salt: &[u8], line: &impl Serialize) {
1627        let start = begin(out);
1628        // Into a Vec, of these types: it cannot fail.
1629        let _ = serde_json::to_writer(&mut *out, line);
1630        end(out, start, salt);
1631    }
1632
1633    /// Appends a message kept to `out`, its text as it is (a JSON object):
1634    /// `{"add":{"s":..,"id":..,"at":..,"until":..,"owned":..,"text":{..}}}`.
1635    pub(super) fn encode_add(out: &mut Vec<u8>, salt: &[u8], record: &Record) {
1636        let start = begin(out);
1637        out.extend_from_slice(b"{\"add\":{\"s\":");
1638        number(out, record.s);
1639        out.extend_from_slice(b",\"id\":");
1640        let _ = serde_json::to_writer(&mut *out, &*record.id);
1641        out.extend_from_slice(b",\"at\":");
1642        number(out, record.at);
1643        out.extend_from_slice(b",\"until\":");
1644        number(out, record.until);
1645        out.extend_from_slice(if record.owned {
1646            b",\"owned\":true"
1647        } else {
1648            b",\"owned\":false"
1649        });
1650        if let Some(requires) = &record.requires {
1651            out.extend_from_slice(b",\"requires\":");
1652            let _ = serde_json::to_writer(&mut *out, requires);
1653        }
1654        out.extend_from_slice(b",\"text\":");
1655        out.extend_from_slice(record.text.as_bytes());
1656        out.extend_from_slice(b"}}");
1657        end(out, start, salt);
1658    }
1659
1660    /// Room for the check, a space after it: where the line starts.
1661    fn begin(out: &mut Vec<u8>) -> usize {
1662        let start = out.len();
1663        out.extend_from_slice(b"00000000 ");
1664        start
1665    }
1666
1667    /// The check of the line from `start`, written in its room, and the
1668    /// newline.
1669    fn end(out: &mut Vec<u8>, start: usize, salt: &[u8]) {
1670        const HEX: &[u8; 16] = b"0123456789abcdef";
1671        let crc = crc32(&[salt, &out[start + 9..]]);
1672        for (i, byte) in out[start..start + 8].iter_mut().enumerate() {
1673            *byte = HEX[((crc >> (28 - 4 * i)) & 0xF) as usize];
1674        }
1675        out.push(b'\n');
1676    }
1677
1678    fn number(out: &mut Vec<u8>, n: u64) {
1679        let mut digits = [0u8; 20];
1680        let mut at = digits.len();
1681        let mut n = n;
1682        loop {
1683            at -= 1;
1684            digits[at] = b'0' + (n % 10) as u8;
1685            n /= 10;
1686            if n == 0 {
1687                break;
1688            }
1689        }
1690        out.extend_from_slice(&digits[at..]);
1691    }
1692
1693    /// One line: its JSON, when its check holds.
1694    fn checked<'a>(line: &'a [u8], salt: &[u8]) -> Option<&'a [u8]> {
1695        if line.get(8) != Some(&b' ') {
1696            return None;
1697        }
1698        let (crc, json) = (line.get(..8)?, line.get(9..)?);
1699        let crc = u32::from_str_radix(std::str::from_utf8(crc).ok()?, 16).ok()?;
1700        (crc32(&[salt, json]) == crc).then_some(json)
1701    }
1702
1703    /// A line of a kind a later version writes, an object of one key this
1704    /// one does not know: skipped. A change it must not skip is a new
1705    /// format (`wse_outbox`).
1706    fn unknown_kind(json: &[u8]) -> bool {
1707        const KNOWN: [&str; 6] = ["add", "written", "ack", "drop", "user", "node"];
1708        serde_json::from_slice::<std::collections::BTreeMap<String, &RawValue>>(json)
1709            .is_ok_and(|line| line.len() == 1 && line.keys().all(|k| !KNOWN.contains(&k.as_str())))
1710    }
1711
1712    /// What a file holds, read up to the first line that does not check: a
1713    /// tail cut short, or left from an older file, is not read. None: not a
1714    /// file of this format.
1715    pub(super) fn read(bytes: &[u8]) -> Option<Read> {
1716        let mut lines = bytes.split(|b| *b == b'\n');
1717        let head: Head<'_> = serde_json::from_slice(checked(lines.next()?, b"")?).ok()?;
1718        if head.wse_outbox != 1 {
1719            return None;
1720        }
1721        let salt = head.salt.as_bytes();
1722        let mut read = Read {
1723            producer: head.producer.as_ref().into(),
1724            node: None,
1725            written: 0,
1726            next: head.next.max(1),
1727            user: None,
1728            records: VecDeque::new(),
1729        };
1730        for line in lines {
1731            if line.is_empty() {
1732                continue;
1733            }
1734            let Some(json) = checked(line, salt) else {
1735                tracing::warn!("[WSE] the outbox's file ends in a line that does not check");
1736                break;
1737            };
1738            let line = match serde_json::from_slice::<Line<'_>>(json) {
1739                Ok(line) => line,
1740                Err(_) if unknown_kind(json) => continue,
1741                Err(_) => {
1742                    tracing::warn!("[WSE] the outbox's file ends in a line that does not check");
1743                    break;
1744                }
1745            };
1746            match line {
1747                Line::Add(record) => {
1748                    let text = record.text.get();
1749                    // A message is a JSON object; what it requires, topics.
1750                    let requires = match record.requires {
1751                        None => None,
1752                        Some(topics)
1753                            if !topics.is_empty() && topics.iter().all(|t| !t.is_empty()) =>
1754                        {
1755                            Some(topics.into_boxed_slice())
1756                        }
1757                        Some(_) => {
1758                            tracing::warn!(
1759                                "[WSE] the outbox's file ends in a line that does not check"
1760                            );
1761                            break;
1762                        }
1763                    };
1764                    if !text.starts_with('{') {
1765                        tracing::warn!(
1766                            "[WSE] the outbox's file ends in a line that does not check"
1767                        );
1768                        break;
1769                    }
1770                    read.next = read.next.max(record.s + 1);
1771                    read.records.push_back(Record {
1772                        s: record.s,
1773                        id: record.id,
1774                        at: record.at,
1775                        until: record.until,
1776                        owned: record.owned,
1777                        requires,
1778                        size: super::unnumbered(text).len(),
1779                        text: text.into(),
1780                        tag: 0,
1781                    });
1782                }
1783                Line::Written(n) => {
1784                    read.written = read.written.max(n);
1785                    read.next = read.next.max(n + 1);
1786                }
1787                // The records are in the order of their numbers: what an
1788                // answer took is at the front.
1789                Line::Ack(n) => {
1790                    while read.records.front().is_some_and(|r| r.s <= n) {
1791                        read.records.pop_front();
1792                    }
1793                }
1794                Line::Drop(mut numbers) => {
1795                    numbers.sort_unstable();
1796                    read.records
1797                        .retain(|r| numbers.binary_search(&r.s).is_err());
1798                }
1799                Line::User(user) => read.user = user,
1800                Line::Node(node) => read.node = Some(node.into()),
1801            }
1802        }
1803        Some(read)
1804    }
1805}
1806
1807#[cfg(test)]
1808mod tests {
1809    use super::*;
1810
1811    fn quiet() -> Report {
1812        Arc::new(|_, _| {})
1813    }
1814
1815    fn outbox(offline: &Offline) -> Outbox {
1816        Outbox::open(
1817            offline,
1818            Arc::new(Room::new(offline)),
1819            quiet(),
1820            Arc::new(|| {}),
1821        )
1822    }
1823
1824    /// `json` as the client keeps it: after the room for its number.
1825    fn kept(json: &str) -> String {
1826        format!("{}{json}", " ".repeat(HEADROOM))
1827    }
1828
1829    fn add(outbox: &mut Outbox, id: &str) -> u64 {
1830        let json = format!(r#"{{"t":"m","p":{{}},"id":"{id}"}}"#);
1831        assert!(outbox.room.take(json.len()));
1832        outbox.add(id.into(), kept(&json), json.len(), None, 0, None, 0)
1833    }
1834
1835    /// What goes to the connection's writer now.
1836    fn due(outbox: &mut Outbox, now: u64, outcomes: &mut Outcomes) -> Vec<(u64, String)> {
1837        let mut out = Vec::new();
1838        outbox.due(
1839            now,
1840            outcomes,
1841            |_, _| true,
1842            |s, text| out.push((s, text.to_string())),
1843        );
1844        out
1845    }
1846
1847    /// A connection to a server that answers: taken, and answered with
1848    /// `acked` (and as far as it cannot tell, `unsure`).
1849    fn answering(outbox: &mut Outbox, acked: u64) -> Outcomes {
1850        answering_unsure(outbox, acked, 0)
1851    }
1852
1853    fn answering_unsure(outbox: &mut Outbox, acked: u64, unsure: u64) -> Outcomes {
1854        outbox.connected(Some("ann"), 0);
1855        outbox.server_answers(true);
1856        outbox.answered(acked, &[], "n1", unsure)
1857    }
1858
1859    fn numbers(out: &[(u64, String)]) -> Vec<u64> {
1860        out.iter().map(|(s, _)| *s).collect()
1861    }
1862
1863    fn ids(list: &[String]) -> Vec<&str> {
1864        list.iter().map(String::as_str).collect()
1865    }
1866
1867    #[test]
1868    fn a_message_waits_for_the_answer_and_goes_again_after_a_reconnect() {
1869        let mut outbox = outbox(&Offline::in_memory());
1870        add(&mut outbox, "a");
1871        add(&mut outbox, "b");
1872        let mut outcomes = Outcomes::default();
1873        assert!(
1874            due(&mut outbox, 0, &mut outcomes).is_empty(),
1875            "no connection"
1876        );
1877        outbox.connected(Some("ann"), 0);
1878        outbox.server_answers(true);
1879        assert!(
1880            due(&mut outbox, 0, &mut outcomes).is_empty(),
1881            "no answer yet"
1882        );
1883        assert_eq!(outbox.hello()["from"], 1);
1884        outbox.answered(0, &[], "n1", 0);
1885        let out = due(&mut outbox, 0, &mut outcomes);
1886        assert_eq!(
1887            out,
1888            [
1889                (1, r#"{"s":1,"t":"m","p":{},"id":"a"}"#.to_owned()),
1890                (2, r#"{"s":2,"t":"m","p":{},"id":"b"}"#.to_owned())
1891            ]
1892        );
1893        outbox.wrote(2);
1894        assert_eq!(ids(&outbox.acked(1).delivered), ["a"]);
1895        // Lost before 2 was answered: it goes again on the next connection.
1896        assert_eq!(outbox.disconnected(), Outcomes::default());
1897        assert_eq!(outbox.hello()["written"], 2);
1898        assert_eq!(outbox.hello()["node"], "n1");
1899        assert!(answering(&mut outbox, 1).delivered.is_empty());
1900        assert_eq!(numbers(&due(&mut outbox, 0, &mut outcomes)), [2]);
1901        add(&mut outbox, "c");
1902        assert_eq!(
1903            numbers(&due(&mut outbox, 0, &mut outcomes)),
1904            [3],
1905            "a new one"
1906        );
1907        assert_eq!(ids(&outbox.acked(3).delivered), ["b", "c"]);
1908        assert_eq!(outbox.room.count(), 0);
1909        assert_eq!(outcomes, Outcomes::default());
1910    }
1911
1912    #[test]
1913    fn the_answer_may_come_before_the_server_takes_the_connection() {
1914        let mut outbox = outbox(&Offline::in_memory());
1915        add(&mut outbox, "a");
1916        let mut outcomes = Outcomes::default();
1917        outbox.server_answers(true);
1918        outbox.answered(0, &[], "n1", 0);
1919        assert!(
1920            due(&mut outbox, 0, &mut outcomes).is_empty(),
1921            "not taken yet"
1922        );
1923        outbox.connected(Some("ann"), 0);
1924        assert_eq!(numbers(&due(&mut outbox, 0, &mut outcomes)), [1]);
1925    }
1926
1927    #[test]
1928    fn a_server_without_answers_takes_what_was_written() {
1929        let mut outbox = outbox(&Offline::in_memory());
1930        add(&mut outbox, "a");
1931        outbox.connected(None, 0);
1932        outbox.server_answers(false);
1933        let mut outcomes = Outcomes::default();
1934        let out = due(&mut outbox, 0, &mut outcomes);
1935        assert_eq!(
1936            out,
1937            [(1, r#"{"t":"m","p":{},"id":"a"}"#.to_owned())],
1938            "not numbered"
1939        );
1940        assert_eq!(ids(&outbox.wrote(1).delivered), ["a"]);
1941        assert_eq!(outbox.len(), 0);
1942        assert_eq!(outbox.hello()["written"], 0);
1943    }
1944
1945    #[test]
1946    fn a_message_never_goes_again_as_another_user_than_it_was_written_as() {
1947        let mut outbox = outbox(&Offline::in_memory());
1948        add(&mut outbox, "before any");
1949        answering(&mut outbox, 0);
1950        let mut outcomes = Outcomes::default();
1951        assert_eq!(numbers(&due(&mut outbox, 0, &mut outcomes)), [1]);
1952        // Lost before its answer; the next connection is bob's.
1953        outbox.disconnected();
1954        let outcomes = outbox.connected(Some("bob"), 0);
1955        assert_eq!(
1956            outcomes.not_delivered,
1957            [("user_changed".to_owned(), vec!["before any".to_owned()])]
1958        );
1959        outbox.server_answers(true);
1960        outbox.answered(0, &[], "n1", 0);
1961        assert!(due(&mut outbox, 0, &mut Outcomes::default()).is_empty());
1962    }
1963
1964    #[test]
1965    fn another_users_messages_go_when_a_connection_is_taken_for_another() {
1966        let mut outbox = outbox(&Offline::in_memory());
1967        add(&mut outbox, "before any");
1968        outbox.connected(Some("ann"), 0);
1969        outbox.disconnected();
1970        add(&mut outbox, "anns");
1971        let outcomes = outbox.connected(Some("bob"), 0);
1972        assert_eq!(
1973            outcomes.not_delivered,
1974            [("user_changed".to_owned(), vec!["anns".to_owned()])]
1975        );
1976        assert_eq!(
1977            outbox.len(),
1978            1,
1979            "sent before any connection: it goes on any"
1980        );
1981        assert_eq!(outbox.room.count(), 1);
1982    }
1983
1984    #[test]
1985    fn a_message_not_written_in_its_time_runs_out_on_a_timer() {
1986        let mut outbox = outbox(&Offline::in_memory());
1987        assert!(outbox.room.take(2));
1988        outbox.add(
1989            "late".into(),
1990            kept("{}"),
1991            2,
1992            Some(Duration::from_millis(5)),
1993            0,
1994            None,
1995            0,
1996        );
1997        add(&mut outbox, "on time");
1998        assert!(outbox.deadline().is_some(), "a timer for the late one");
1999        let mut outcomes = Outcomes::default();
2000        outbox.expire(10, &mut outcomes);
2001        assert_eq!(
2002            outcomes.not_delivered,
2003            [("expired".to_owned(), vec!["late".to_owned()])]
2004        );
2005        assert!(outbox.deadline().is_some(), "a timer for the one on time");
2006        let outcomes = answering(&mut outbox, 0);
2007        assert_eq!(outcomes, Outcomes::default());
2008        let mut outcomes = Outcomes::default();
2009        assert_eq!(numbers(&due(&mut outbox, 10, &mut outcomes)), [2]);
2010    }
2011
2012    #[test]
2013    fn a_refusal_not_heard_comes_with_the_answer() {
2014        let mut outbox = outbox(&Offline::in_memory());
2015        answering(&mut outbox, 0);
2016        for id in ["a", "b", "c"] {
2017            add(&mut outbox, id);
2018        }
2019        let mut outcomes = Outcomes::default();
2020        due(&mut outbox, 0, &mut outcomes);
2021        outbox.disconnected();
2022        assert_eq!(outbox.hello()["from"], 1, "waits to hear of 1 on");
2023        outbox.connected(Some("ann"), 0);
2024        outbox.server_answers(true);
2025        let outcomes = outbox.answered(3, &[(2, 2, "busy")], "n1", 0);
2026        assert_eq!(ids(&outcomes.delivered), ["a", "c"]);
2027        assert_eq!(
2028            outcomes.not_delivered,
2029            [("busy".to_owned(), vec!["b".to_owned()])]
2030        );
2031        assert_eq!(outbox.hello()["from"], 4, "nothing waits");
2032        assert_eq!(outbox.room.count(), 0);
2033    }
2034
2035    #[test]
2036    fn a_refused_message_is_said() {
2037        let mut outbox = outbox(&Offline::in_memory());
2038        answering(&mut outbox, 0);
2039        add(&mut outbox, "x");
2040        add(&mut outbox, "y");
2041        let mut outcomes = Outcomes::default();
2042        assert_eq!(numbers(&due(&mut outbox, 0, &mut outcomes)), [1, 2]);
2043        let refused = outbox.refused(1, 1, "busy");
2044        assert_eq!(
2045            refused.not_delivered,
2046            [("busy".to_owned(), vec!["x".to_owned()])]
2047        );
2048        assert_eq!(ids(&outbox.acked(2).delivered), ["y"]);
2049        assert_eq!(outbox.room.count(), 0);
2050    }
2051
2052    #[test]
2053    fn a_message_written_waits_for_its_answer_whatever_its_age_where_the_server_cannot_tell() {
2054        let mut outbox = outbox(&Offline::in_memory());
2055        answering(&mut outbox, 0);
2056        assert!(outbox.room.take(2));
2057        outbox.add(
2058            "a".into(),
2059            kept("{}"),
2060            2,
2061            Some(Duration::from_millis(5)),
2062            0,
2063            None,
2064            0,
2065        );
2066        let mut outcomes = Outcomes::default();
2067        assert_eq!(numbers(&due(&mut outbox, 0, &mut outcomes)), [1]);
2068        outbox.disconnected();
2069        let outcomes = answering_unsure(&mut outbox, 0, 1);
2070        assert_eq!(outcomes, Outcomes::default(), "not expired: it was written");
2071        let mut outcomes = Outcomes::default();
2072        assert_eq!(numbers(&due(&mut outbox, 100, &mut outcomes)), [1]);
2073        assert_eq!(outcomes, Outcomes::default());
2074        // Where the server can tell it did not take it: as any not written.
2075        outbox.disconnected();
2076        assert_eq!(answering(&mut outbox, 0), Outcomes::default());
2077        let mut outcomes = Outcomes::default();
2078        assert!(due(&mut outbox, 100, &mut outcomes).is_empty());
2079        assert_eq!(
2080            outcomes.not_delivered,
2081            [("expired".to_owned(), vec!["a".to_owned()])]
2082        );
2083        assert_eq!(outbox.hello()["written"], 0);
2084    }
2085
2086    #[test]
2087    fn a_clear_tells_what_the_server_has_in_hand_by_its_answer() {
2088        let mut outbox = outbox(&Offline::in_memory());
2089        answering(&mut outbox, 0);
2090        add(&mut outbox, "a");
2091        add(&mut outbox, "b");
2092        let mut outcomes = Outcomes::default();
2093        due(&mut outbox, 0, &mut outcomes);
2094        outbox.wrote(2);
2095        add(&mut outbox, "c");
2096        let cleared = outbox.clear();
2097        assert_eq!(
2098            cleared.not_delivered,
2099            [("cleared".to_owned(), vec!["c".to_owned()])],
2100            "c never left"
2101        );
2102        assert_eq!(outbox.room.count(), 0);
2103        assert_eq!(ids(&outbox.acked(1).delivered), ["a"]);
2104        assert_eq!(
2105            outbox.disconnected().not_delivered,
2106            [("cleared".to_owned(), vec!["b".to_owned()])]
2107        );
2108        // Nothing of it goes on the next connection.
2109        answering(&mut outbox, 0);
2110        assert!(due(&mut outbox, 0, &mut outcomes).is_empty());
2111    }
2112
2113    #[test]
2114    fn the_room_refuses_past_its_limits() {
2115        let mut offline = Offline::in_memory();
2116        offline.max_messages = 2;
2117        offline.max_bytes = 100;
2118        let room = Room::new(&offline);
2119        assert!(room.take(10));
2120        assert!(room.take(80));
2121        assert!(!room.take(1), "two at most");
2122        room.give_back(80);
2123        assert!(!room.take(91), "100 bytes at most");
2124        assert!(room.take(90));
2125    }
2126
2127    #[test]
2128    fn crc32_is_zlibs() {
2129        assert_eq!(crc32(&[b"123456789"]), 0xCBF4_3926);
2130        assert_eq!(crc32(&[b"1234", b"56789"]), 0xCBF4_3926);
2131        // Eight bytes at a time, as one at a time: every length and split.
2132        let bytes: Vec<u8> = (0..200u32).map(|n| (n * 37 + 11) as u8).collect();
2133        let one_at_a_time = |data: &[u8]| {
2134            let mut crc = !0u32;
2135            for byte in data {
2136                crc = CRC_TABLES[0][((crc ^ u32::from(*byte)) & 0xFF) as usize] ^ (crc >> 8);
2137            }
2138            !crc
2139        };
2140        for len in 0..bytes.len() {
2141            let data = &bytes[..len];
2142            assert_eq!(crc32(&[data]), one_at_a_time(data), "{len} bytes");
2143            let (a, b) = data.split_at(len / 3);
2144            assert_eq!(crc32(&[a, b]), one_at_a_time(data), "{len} bytes, split");
2145        }
2146    }
2147
2148    #[test]
2149    fn the_number_is_written_in_the_room_before_the_message() {
2150        let text = number_in_place(7, kept(r#"{"t":"x"}"#));
2151        assert_eq!(&*text, r#"{"s":7,"t":"x"}"#);
2152        assert_eq!(&*number_in_place(7, kept("{}")), r#"{"s":7 }"#);
2153        assert_eq!(&*number_in_place(12, kept("{ }")), r#"{"s":12  }"#);
2154        let most = number_in_place(u64::MAX, kept(r#"{"a":1}"#));
2155        assert_eq!(&*most, format!(r#"{{"s":{},"a":1}}"#, u64::MAX));
2156        // Without its room, as a text read back is: numbered all the same.
2157        assert_eq!(
2158            &*number_in_place(3, r#"{"t":"x"}"#.to_owned()),
2159            r#"{"s":3,"t":"x"}"#
2160        );
2161        for text in [&*text, &*most] {
2162            serde_json::from_str::<serde_json::Value>(text).unwrap();
2163        }
2164    }
2165
2166    #[test]
2167    fn numbering_keeps_the_json_whole() {
2168        assert_eq!(numbered(7, r#"{"t":"x"}"#), r#"{"s":7,"t":"x"}"#);
2169        assert_eq!(numbered(7, "{}"), r#"{"s":7}"#);
2170        assert_eq!(numbered(18, r#" { "t":1}"#), r#"{"s":18, "t":1}"#);
2171        assert_eq!(unnumbered(r#"{"s":7,"t":"x"}"#), r#"{"t":"x"}"#);
2172        assert_eq!(unnumbered(r#"{"s":7}"#), "{}");
2173        assert_eq!(numbered(u64::MAX, "{}"), format!(r#"{{"s":{}}}"#, u64::MAX));
2174    }
2175
2176    #[cfg(not(target_arch = "wasm32"))]
2177    mod files {
2178        use super::*;
2179
2180        fn dir() -> std::path::PathBuf {
2181            let dir = std::env::temp_dir().join(format!(
2182                "wse-outbox-{}-{}",
2183                std::process::id(),
2184                fastrand::u64(..)
2185            ));
2186            std::fs::create_dir_all(&dir).unwrap();
2187            dir
2188        }
2189
2190        #[test]
2191        fn a_numbered_message_goes_once_the_file_holds_it_and_the_mark_that_it_may_have() {
2192            let dir = dir();
2193            let path = dir.join("outbox");
2194            let mut outbox = outbox(&Offline::in_file(&path));
2195            answering(&mut outbox, 0);
2196            add(&mut outbox, "a");
2197            let mut outcomes = Outcomes::default();
2198            let mut went = Vec::new();
2199            for _ in 0..1000 {
2200                outbox.due(
2201                    0,
2202                    &mut outcomes,
2203                    |_, _| true,
2204                    |s, _| {
2205                        let file = std::fs::read_to_string(&path).unwrap();
2206                        assert!(
2207                            file.contains(&format!(r#"{{"written":{s}}}"#))
2208                                && file.contains(&format!(r#"{{"add":{{"s":{s},"#)),
2209                            "it went before the file held it: {file}"
2210                        );
2211                        went.push(s);
2212                    },
2213                );
2214                if !went.is_empty() {
2215                    break;
2216                }
2217                std::thread::sleep(Duration::from_millis(2));
2218            }
2219            assert_eq!(went, [1]);
2220            drop(outbox);
2221            let _ = std::fs::remove_dir_all(&dir);
2222        }
2223
2224        #[test]
2225        fn what_is_kept_outlives_the_client_until_the_server_took_it() {
2226            let dir = dir();
2227            let offline = Offline::in_file(dir.join("queue"));
2228            let producer = {
2229                let mut outbox = outbox(&offline);
2230                answering(&mut outbox, 0);
2231                add(&mut outbox, "a");
2232                add(&mut outbox, "b");
2233                add(&mut outbox, "c");
2234                let mut outcomes = Outcomes::default();
2235                due(&mut outbox, 0, &mut outcomes);
2236                outbox.acked(1);
2237                outbox.producer.clone()
2238            };
2239            // The application ended and starts again.
2240            let mut outbox = outbox(&offline);
2241            assert_eq!(outbox.producer, producer, "the same producer");
2242            assert_eq!(outbox.len(), 2);
2243            assert_eq!(outbox.hello()["written"], 3);
2244            assert_eq!(outbox.hello()["node"], "n1");
2245            assert_eq!(outbox.room.count(), 2);
2246            let mut outcomes = Outcomes::default();
2247            answering(&mut outbox, 1);
2248            let out = due(&mut outbox, 0, &mut outcomes);
2249            assert_eq!(
2250                out,
2251                [
2252                    (2, r#"{"s":2,"t":"m","p":{},"id":"b"}"#.to_owned()),
2253                    (3, r#"{"s":3,"t":"m","p":{},"id":"c"}"#.to_owned())
2254                ],
2255                "the text as it was"
2256            );
2257            assert_eq!(add(&mut outbox, "d"), 4, "numbers go on");
2258            drop(outbox);
2259            std::fs::remove_dir_all(&dir).unwrap();
2260        }
2261
2262        #[test]
2263        fn a_number_is_never_used_twice() {
2264            let dir = dir();
2265            let offline = Offline::in_file(dir.join("queue"));
2266            {
2267                let mut outbox = outbox(&offline);
2268                add(&mut outbox, "a");
2269                add(&mut outbox, "b");
2270                outbox.clear();
2271            }
2272            let mut outbox = outbox(&offline);
2273            assert_eq!(outbox.len(), 0);
2274            assert_eq!(add(&mut outbox, "c"), 3);
2275            drop(outbox);
2276            // Written anew at the open: the next number is in its head.
2277            let mut outbox = self::outbox(&offline);
2278            assert_eq!(add(&mut outbox, "d"), 4);
2279            drop(outbox);
2280            std::fs::remove_dir_all(&dir).unwrap();
2281        }
2282
2283        #[test]
2284        fn a_tail_that_does_not_check_is_not_read() {
2285            let dir = dir();
2286            let path = dir.join("queue");
2287            let offline = Offline::in_file(&path);
2288            {
2289                let mut outbox = outbox(&offline);
2290                add(&mut outbox, "a");
2291                add(&mut outbox, "b");
2292            }
2293            // Cut short as the application ended, then bytes that are not
2294            // even text, as a power loss can leave.
2295            let mut bytes = std::fs::read(&path).unwrap();
2296            bytes.truncate(bytes.len() - 5);
2297            bytes.extend_from_slice(&[0xff, 0xfe, 0x00, b'\n']);
2298            std::fs::write(&path, &bytes).unwrap();
2299            let outbox = outbox(&offline);
2300            assert_eq!(outbox.len(), 1, "read up to the line cut short");
2301            drop(outbox);
2302            std::fs::remove_dir_all(&dir).unwrap();
2303        }
2304
2305        #[test]
2306        fn a_file_that_does_not_open_is_said_and_the_outbox_kept_in_memory() {
2307            let dir = dir();
2308            let offline = Offline::in_file(dir.join("queue"));
2309            let first = outbox(&offline);
2310            let said = Arc::new(std::sync::Mutex::new(Vec::new()));
2311            let seen = Arc::clone(&said);
2312            let report: Report = Arc::new(move |operation, why| {
2313                seen.lock().unwrap().push((operation, why));
2314            });
2315            let mut second = Outbox::open(
2316                &offline,
2317                Arc::new(Room::new(&offline)),
2318                report,
2319                Arc::new(|| {}),
2320            );
2321            {
2322                let said = said.lock().unwrap();
2323                assert_eq!(said.len(), 1, "{said:?}");
2324                assert_eq!(said[0].0, "open");
2325                assert!(said[0].1.contains("another client's"), "{said:?}");
2326            }
2327            add(&mut second, "a");
2328            assert_eq!(second.len(), 1, "in memory");
2329            drop((first, second));
2330            std::fs::remove_dir_all(&dir).unwrap();
2331        }
2332
2333        #[test]
2334        fn the_file_is_written_anew_once_what_went_outweighs_what_is_kept() {
2335            let dir = dir();
2336            let path = dir.join("queue");
2337            let mut outbox = outbox(&Offline::in_file(&path));
2338            answering(&mut outbox, 0);
2339            let mut outcomes = Outcomes::default();
2340            for round in 0..300 {
2341                let s = add(&mut outbox, &format!("m{round}"));
2342                due(&mut outbox, 0, &mut outcomes);
2343                outbox.acked(s);
2344            }
2345            drop(outbox);
2346            let lines = std::fs::read_to_string(&path).unwrap().lines().count();
2347            assert!(lines < 80, "{lines} lines");
2348            let outbox = self::outbox(&Offline::in_file(&path));
2349            assert_eq!(outbox.len(), 0);
2350            assert_eq!(outbox.next, 301);
2351            drop(outbox);
2352            std::fs::remove_dir_all(&dir).unwrap();
2353        }
2354
2355        /// The file format all clients share: each line written byte for
2356        /// byte as the vector has it, and the file read to its state.
2357        #[test]
2358        fn the_shared_vector_is_written_and_read_alike() {
2359            let vector: serde_json::Value =
2360                serde_json::from_str(include_str!("../tests/vectors/outbox.json")).unwrap();
2361            let salt = vector["salt"].as_str().unwrap().as_bytes();
2362            let mut whole = Vec::new();
2363            for line in vector["lines"].as_array().unwrap() {
2364                let json = line["json"].as_str().unwrap();
2365                let value: serde_json::Value = serde_json::from_str(json).unwrap();
2366                let mut out = Vec::new();
2367                match line["kind"].as_str().unwrap() {
2368                    "head" => {
2369                        let head = Head {
2370                            wse_outbox: 1,
2371                            salt: value["salt"].as_str().unwrap().into(),
2372                            producer: value["producer"].as_str().unwrap().into(),
2373                            next: value["next"].as_u64().unwrap(),
2374                        };
2375                        file::encode(&mut out, b"", &head);
2376                    }
2377                    "add" => {
2378                        let add = &value["add"];
2379                        let text = json
2380                            .split_once(",\"text\":")
2381                            .and_then(|(_, rest)| rest.strip_suffix("}}"))
2382                            .unwrap();
2383                        let record = Record {
2384                            s: add["s"].as_u64().unwrap(),
2385                            id: add["id"].as_str().unwrap().into(),
2386                            at: add["at"].as_u64().unwrap(),
2387                            until: add["until"].as_u64().unwrap(),
2388                            owned: add["owned"].as_bool().unwrap(),
2389                            requires: add["requires"].as_array().map(|topics| {
2390                                topics
2391                                    .iter()
2392                                    .map(|t| t.as_str().unwrap().to_owned())
2393                                    .collect()
2394                            }),
2395                            text: text.into(),
2396                            size: 0,
2397                            tag: 0,
2398                        };
2399                        file::encode_add(&mut out, salt, &record);
2400                    }
2401                    "node" => file::encode(
2402                        &mut out,
2403                        salt,
2404                        &LineRef::Node(value["node"].as_str().unwrap()),
2405                    ),
2406                    "written" => {
2407                        file::encode(
2408                            &mut out,
2409                            salt,
2410                            &LineRef::Written(value["written"].as_u64().unwrap()),
2411                        );
2412                    }
2413                    "user" => file::encode(&mut out, salt, &LineRef::User(value["user"].as_str())),
2414                    "ack" => file::encode(
2415                        &mut out,
2416                        salt,
2417                        &LineRef::Ack(value["ack"].as_u64().unwrap()),
2418                    ),
2419                    "drop" => {
2420                        let numbers: Vec<u64> = value["drop"]
2421                            .as_array()
2422                            .unwrap()
2423                            .iter()
2424                            .map(|n| n.as_u64().unwrap())
2425                            .collect();
2426                        file::encode(&mut out, salt, &LineRef::Drop(&numbers));
2427                    }
2428                    kind => panic!("a line of kind {kind}"),
2429                }
2430                let expected = format!("{}\n", line["line"].as_str().unwrap());
2431                assert_eq!(String::from_utf8(out.clone()).unwrap(), expected);
2432                whole.extend_from_slice(&out);
2433            }
2434
2435            let read = file::read(&whole).unwrap();
2436            let state = &vector["state"];
2437            assert_eq!(&*read.producer, state["producer"].as_str().unwrap());
2438            assert_eq!(read.node.as_deref(), state["node"].as_str());
2439            assert_eq!(read.written, state["written"].as_u64().unwrap());
2440            assert_eq!(read.next, state["next"].as_u64().unwrap());
2441            assert_eq!(read.user.as_deref(), state["user"].as_str());
2442            type Row<'a> = (u64, &'a str, u64, u64, bool, Option<Vec<String>>, &'a str);
2443            let records: Vec<Row<'_>> = read
2444                .records
2445                .iter()
2446                .map(|r| {
2447                    let requires = r.requires.as_ref().map(|t| t.to_vec());
2448                    (r.s, &*r.id, r.at, r.until, r.owned, requires, &*r.text)
2449                })
2450                .collect();
2451            let expected: Vec<Row<'_>> = state["records"]
2452                .as_array()
2453                .unwrap()
2454                .iter()
2455                .map(|r| {
2456                    (
2457                        r["s"].as_u64().unwrap(),
2458                        r["id"].as_str().unwrap(),
2459                        r["at"].as_u64().unwrap(),
2460                        r["until"].as_u64().unwrap(),
2461                        r["owned"].as_bool().unwrap(),
2462                        r["requires"]
2463                            .as_array()
2464                            .map(|t| t.iter().map(|t| t.as_str().unwrap().to_owned()).collect()),
2465                        r["text"].as_str().unwrap(),
2466                    )
2467                })
2468                .collect();
2469            assert_eq!(records, expected);
2470
2471            for damaged in vector["damaged"].as_array().unwrap() {
2472                let read = file::read(damaged["file"].as_str().unwrap().as_bytes());
2473                let why = damaged["why"].as_str().unwrap();
2474                match damaged["records"].as_array() {
2475                    None => assert!(read.is_none(), "{why}"),
2476                    Some(ids) => {
2477                        let read = read.unwrap();
2478                        let got: Vec<&str> = read.records.iter().map(|r| &*r.id).collect();
2479                        let want: Vec<&str> = ids.iter().map(|id| id.as_str().unwrap()).collect();
2480                        assert_eq!(got, want, "{why}");
2481                        assert_eq!(read.user.as_deref(), damaged["user"].as_str(), "{why}");
2482                    }
2483                }
2484            }
2485        }
2486
2487        /// A rewrite owed within the second after the last one comes on the
2488        /// outbox's deadline, with no further change to wait for.
2489        #[cfg(unix)]
2490        #[test]
2491        fn a_rewrite_owed_within_the_second_comes_on_the_deadline() {
2492            use std::os::unix::fs::PermissionsExt;
2493
2494            let dir = dir();
2495            let path = dir.join("queue");
2496            let said = Arc::new(std::sync::Mutex::new(Vec::new()));
2497            let seen = Arc::clone(&said);
2498            let report: Report = Arc::new(move |operation, why| {
2499                seen.lock().unwrap().push((operation, why));
2500            });
2501            let offline = Offline::in_file(&path);
2502            let mut outbox = Outbox::open(
2503                &offline,
2504                Arc::new(Room::new(&offline)),
2505                report,
2506                Arc::new(|| {}),
2507            );
2508            std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o500)).unwrap();
2509            if std::fs::write(dir.join("probe"), b"").is_ok() {
2510                std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
2511                drop(outbox);
2512                std::fs::remove_dir_all(&dir).unwrap();
2513                return;
2514            }
2515            answering(&mut outbox, 0);
2516            let mut outcomes = Outcomes::default();
2517            // Enough went to write the file anew: that fails (the folder
2518            // takes no file), and so does the rewrite at once after it.
2519            for round in 0..100 {
2520                let s = add(&mut outbox, &format!("m{round}"));
2521                due(&mut outbox, 0, &mut outcomes);
2522                outbox.acked(s);
2523            }
2524            for _ in 0..250 {
2525                if !said.lock().unwrap().is_empty() {
2526                    break;
2527                }
2528                std::thread::sleep(Duration::from_millis(10));
2529            }
2530            add(&mut outbox, "a");
2531            due(&mut outbox, 0, &mut outcomes);
2532            std::thread::sleep(Duration::from_millis(200));
2533            std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
2534            // Within the second after that rewrite: none yet, a deadline for it.
2535            add(&mut outbox, "b");
2536            due(&mut outbox, 0, &mut outcomes);
2537            let at = outbox.deadline().expect("a deadline for the rewrite owed");
2538            assert!(at <= Instant::now() + Duration::from_secs(1));
2539            std::thread::sleep(at.saturating_duration_since(Instant::now()));
2540            outbox.heal(Instant::now());
2541            // On disk with no further change: its file holds both.
2542            let mut both = false;
2543            for _ in 0..250 {
2544                let text = std::fs::read_to_string(&path).unwrap_or_default();
2545                if text.contains(r#""id":"a""#) && text.contains(r#""id":"b""#) {
2546                    both = true;
2547                    break;
2548                }
2549                std::thread::sleep(Duration::from_millis(10));
2550            }
2551            assert!(both, "the file written anew with a and b");
2552            assert_eq!(outbox.heal, None, "nothing owed any more");
2553            drop(outbox);
2554            std::fs::remove_dir_all(&dir).unwrap();
2555        }
2556
2557        /// A file that cannot be written anew says so once, and is written
2558        /// anew with all that is kept once it can.
2559        #[cfg(unix)]
2560        #[test]
2561        fn a_file_that_cannot_be_written_says_so_and_heals() {
2562            use std::os::unix::fs::PermissionsExt;
2563
2564            let dir = dir();
2565            let path = dir.join("queue");
2566            let said = Arc::new(std::sync::Mutex::new(Vec::new()));
2567            let seen = Arc::clone(&said);
2568            let report: Report = Arc::new(move |operation, why| {
2569                seen.lock().unwrap().push((operation, why));
2570            });
2571            let offline = Offline::in_file(&path);
2572            let mut outbox = Outbox::open(
2573                &offline,
2574                Arc::new(Room::new(&offline)),
2575                report,
2576                Arc::new(|| {}),
2577            );
2578            // The directory takes no new file: writing the file anew fails
2579            // (not for root, who writes anywhere).
2580            std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o500)).unwrap();
2581            if std::fs::write(dir.join("probe"), b"").is_ok() {
2582                std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
2583                drop(outbox);
2584                std::fs::remove_dir_all(&dir).unwrap();
2585                return;
2586            }
2587            answering(&mut outbox, 0);
2588            let mut outcomes = Outcomes::default();
2589            for round in 0..100 {
2590                let s = add(&mut outbox, &format!("m{round}"));
2591                due(&mut outbox, 0, &mut outcomes);
2592                outbox.acked(s);
2593            }
2594            add(&mut outbox, "kept");
2595            due(&mut outbox, 0, &mut outcomes);
2596            // Each failure after the first waits for a success to be said.
2597            for _ in 0..250 {
2598                if !said.lock().unwrap().is_empty() {
2599                    break;
2600                }
2601                std::thread::sleep(Duration::from_millis(10));
2602            }
2603            assert_eq!(said.lock().unwrap().len(), 1, "{:?}", said.lock().unwrap());
2604            assert_eq!(said.lock().unwrap()[0].0, "write");
2605            std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
2606            outbox.disconnected();
2607            add(&mut outbox, "after");
2608            drop(outbox);
2609            let outbox = self::outbox(&offline);
2610            assert_eq!(outbox.len(), 2, "kept and after, written anew");
2611            drop(outbox);
2612            std::fs::remove_dir_all(&dir).unwrap();
2613        }
2614    }
2615}