use std::sync::Arc;
use tokio::sync::{mpsc, oneshot};
#[derive(Debug)]
pub struct GapFillCommand {
pub symbol: Arc<str>,
pub from_tid: i64,
pub to_tid: i64,
pub response_tx: oneshot::Sender<GapFillResult>,
}
#[derive(Debug, Clone)]
pub struct GapFillResult {
pub trades_fetched: usize,
pub bars_produced: usize,
pub duration_ms: u64,
pub partial: bool,
}
pub type GapFillSender = mpsc::Sender<GapFillCommand>;
pub type GapFillReceiver = mpsc::Receiver<GapFillCommand>;
pub fn gap_fill_channel(buffer: usize) -> (GapFillSender, GapFillReceiver) {
mpsc::channel(buffer)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_gap_fill_channel_send_receive() {
let (tx, mut rx) = gap_fill_channel(16);
let (response_tx, response_rx) = oneshot::channel();
let cmd = GapFillCommand {
symbol: Arc::from("BTCUSDT"),
from_tid: 100,
to_tid: 200,
response_tx,
};
tx.send(cmd).await.unwrap();
let received = rx.recv().await.unwrap();
assert_eq!(&*received.symbol, "BTCUSDT");
assert_eq!(received.from_tid, 100);
assert_eq!(received.to_tid, 200);
received
.response_tx
.send(GapFillResult {
trades_fetched: 100,
bars_produced: 5,
duration_ms: 250,
partial: false,
})
.unwrap();
let result = response_rx.await.unwrap();
assert_eq!(result.trades_fetched, 100);
assert_eq!(result.bars_produced, 5);
assert!(!result.partial);
}
#[test]
fn test_gap_fill_channel_creation() {
let (tx, _rx) = gap_fill_channel(32);
let _tx2 = tx.clone();
}
}