pub async fn replay<I>(
envelopes: I,
tx: &Sender<Envelope>,
speed: ReplaySpeed,
) -> Result<ReplayReport, LogError>Expand description
Sends envelopes into a channel, in order, like a connector would.
The difference from a live connector is the one that matters for research: replay waits for
room in the channel ([mpsc::Sender::send]), so a slow consumer slows the replay down and no
event is lost. A live connector cannot wait and applies the loss policy of the subscription
instead. Code that handles Overflow therefore needs its own test: a replay never produces one.
The envelopes keep their source and sub, so the consumer routes them as it did live.
§Errors
The first LogError from envelopes. Envelopes sent before it stay sent.
§Examples
use std::sync::Arc;
use std::time::Instant;
use brokapi_core::{Envelope, Event, Timestamps, UnixNanos};
use brokapi_runtime::record::{ReplaySpeed, replay};
let ts = |n| Timestamps::new(UnixNanos::from_nanos(n), Instant::now());
let log = vec![
Envelope::new(Arc::from("bcs-md"), ts(1), Event::Balances(vec![])),
Envelope::new(Arc::from("bcs-md"), ts(2), Event::Balances(vec![])),
];
let (tx, mut rx) = tokio::sync::mpsc::channel(8);
let report = replay(log.into_iter().map(Ok), &tx, ReplaySpeed::Instant).await?;
assert_eq!(report.sent, 2);
assert!(rx.recv().await.is_some());