//! The `reconcile` binary's exit status: what `scripts/watch.sh` relies on. use std::process::Command; use chrono::DateTime; use engineering_challenge::Side; use engineering_challenge::util::AlignedSbeWriter; use engineering_challenge::util::dtos::challenge_orderbook::{ PriceLevelUpdateDomain, SnapshotBidsEntryDomain, SnapshotDomain, }; /// An output of a symbol's Snapshot and a Delta, after an orphan Delta of /// another symbol if `orphan`; then, if `mismatched`, a Snapshot of the /// symbol that the Delta doesn't lead to. fn output(orphan: bool, mismatched: bool) -> std::path::PathBuf { let at = DateTime::from_timestamp(1_794_390_000, 0).unwrap(); let delta = |symbol: &str| PriceLevelUpdateDomain { backtest_timestamp: at, exchange_timestamp: at, price_level: 500_000, quantity: 2_000_000, side: Side::Buy, symbol: symbol.to_string(), }; let path = std::env::temp_dir().join(format!( "reconcile-exit-{}-{orphan}-{mismatched}.sbe", std::process::id() )); let mut writer = AlignedSbeWriter::create(path.to_str().unwrap(), 64 << 20).unwrap(); if orphan { writer.write_msg(&delta("2")).unwrap(); } writer .write_msg(&SnapshotDomain { received_at: at, exchange_timestamp: at, bids: vec![SnapshotBidsEntryDomain { price_level: 500_000, quantity: 1_000_000, }], asks: Vec::new(), symbol: "1".to_string(), }) .unwrap(); writer.write_msg(&delta("1")).unwrap(); if mismatched { writer .write_msg(&SnapshotDomain { received_at: at, exchange_timestamp: at, bids: vec![SnapshotBidsEntryDomain { price_level: 500_000, quantity: 3_000_000, }], asks: Vec::new(), symbol: "1".to_string(), }) .unwrap(); } writer.flush().unwrap(); path } #[test] fn reconcile_fails_on_an_orphan_delta_or_an_unexplained_mismatch_and_passes_otherwise() { let status = |orphan: bool, mismatched: bool| { let path = output(orphan, mismatched); let output = Command::new(env!("CARGO_BIN_EXE_reconcile")) .arg(&path) .output() .unwrap(); std::fs::remove_file(&path).unwrap(); ( output.status.success(), String::from_utf8(output.stdout).unwrap(), ) }; let (passed, text) = status(false, false); assert!(passed, "{text}"); assert_eq!( text, "1 snapshots, 1 deltas, 0 orphan deltas, 0 mismatches\n" ); let (passed, text) = status(true, false); assert!(!passed, "{text}"); assert!(text.contains("1 orphan deltas"), "{text}"); // A Snapshot no log line explains (no log given): it fails too. let (passed, text) = status(false, true); assert!(!passed, "{text}"); assert!( text.contains("1 mismatches") && text.contains("unexplained: 1 at "), "{text}" ); }