Expand description
In-memory backend for tests and local development.
Contract:
MemoryBackend::new();Clone-able handle over sharedArcstate.publishwithdelayusestokio::time::sleepin a spawned task (so it is compatible withtokio::time::pause()/advance()in tests).consumehonoursprefetch(at mostprefetchun-acked deliveries per consumer).retryre-publishesnextafterdelay, then acks the original.deferholdsnextfordelayand then inserts it by priority, then acks the original. The hold is atokio::time::sleeptoo, so it is virtual-time friendly.- Every enqueue (plain, delayed or deferred) goes through one priority insertion:
the envelope lands behind every pending envelope whose
priorityis greater than or equal to its own and ahead of the rest, so equal priorities stay FIFO. A hold ends inside that same critical section, so no envelope is ever counted by bothdeferredandpending. dead_lettermoves the envelope into an inspectabledead_letters(queue)list with reason.- Test helpers:
pending(queue) -> usize,deferred(queue) -> usize,dead_letters(queue) -> Vec<(Envelope, String)>,acked(queue) -> Vec<Envelope>. closeends all consumer streams. Afterwardsdeclare,publish,defer,retryanddead_letterall fail withError::ShutDown; a plainackstill records. A delayed publish or a deferral whose timer fires after the close is dropped, exactly like a broker that went away mid-wait.
Implementation notes:
- Each consumer owns a
tokio::sync::Semaphorewithprefetchpermits. A permit is moved into everyDeliveryhanded out and released on ack / retry / dead-letter, which is what caps the number of outstanding deliveries. The semaphore is dropped from the queue when the consumer task ends. - Dropping a stream requeues every message that was produced for it but not handed out yet, so a message can never vanish just because a consumer went away.
- A delivery that is dropped without being settled releases its permit but the message is not requeued (unlike a real broker). Tests should settle deliveries.
Structsยง
- Memory
Backend - An in-memory
Backend. Cloning gives another handle onto the same state.