mod common;
use common::{Capture, fixture, wait_until};
use ek_data::{
Civil, DtNs, EVENT_LOG, EVENT_TICK, Exchange, LogData, TickData, format_civil, parse_civil,
trading_day, try_civil_to_nanos,
};
use ek_event::Event;
use std::sync::Arc;
use std::time::Duration;
fn fx_tick(sym: &str, exchange: Exchange, dt: DtNs) -> TickData {
TickData {
gateway_name: "xt".into(),
symbol: sym.into(),
exchange,
datetime: dt,
last_price: 3520.2,
..Default::default()
}
}
#[test]
fn f10_civil_roundtrip_over_bus() {
let t0 = fixture::t0();
let eng = ek_event::EventEngine::new();
let cap = Capture::<TickData>::new();
cap.register_on(&eng, format!("{EVENT_TICK}CFFEX.IF2506"));
eng.start();
let tick = fx_tick("IF2506", Exchange::CFFEX, t0);
eng.put(Event::with_data(
format!("{EVENT_TICK}CFFEX.IF2506"),
Arc::new(tick.clone()),
))
.unwrap();
assert!(wait_until(Duration::from_secs(2), || cap.len() == 1));
eng.stop();
let seen = cap.last().unwrap().datetime;
assert_eq!(seen, t0, "进流出流同一 nanos 整数(零信息损耗)");
assert_eq!(format_civil(seen), "2024-12-25 09:30:01.500000000");
assert_eq!(parse_civil(&format_civil(seen)).unwrap(), t0);
assert!(trading_day(seen) > 0);
}
#[test]
fn f10_boundary_nanos_survives_bus() {
let eng = ek_event::EventEngine::new();
let cap = Capture::<TickData>::new();
cap.register_on(&eng, format!("{EVENT_TICK}CFFEX.IF2506"));
eng.start();
let edge = fx_tick("IF2506", Exchange::CFFEX, ek_data::DTNS_MAX);
eng.put(Event::with_data(
format!("{EVENT_TICK}CFFEX.IF2506"),
Arc::new(edge),
))
.unwrap();
assert!(wait_until(Duration::from_secs(2), || cap.len() == 1));
eng.stop();
assert_eq!(cap.last().unwrap().datetime, ek_data::DTNS_MAX);
let c = ek_data::civil_from_nanos(ek_data::DTNS_MAX);
assert_eq!(c.year, 2262);
}
#[test]
fn f12_1_wrong_payload_silently_ignored() {
let eng = ek_event::EventEngine::new();
let cap = Capture::<TickData>::new();
cap.register_on(&eng, format!("{EVENT_TICK}CFFEX.IF2506"));
eng.start();
let tick = fx_tick("IF2506", Exchange::CFFEX, fixture::t0());
eng.put(Event::with_data(
format!("{EVENT_TICK}CFFEX.IF2506"),
Arc::new(LogData {
gateway_name: "xt".into(),
msg: "wrong".into(),
level: 20,
}),
))
.unwrap();
eng.put(Event::with_data(
format!("{EVENT_TICK}CFFEX.IF2506"),
Arc::new(42i64),
))
.unwrap();
eng.put(Event::new(format!("{EVENT_TICK}CFFEX.IF2506")))
.unwrap();
eng.put(Event::with_data(
format!("{EVENT_TICK}CFFEX.IF2506"),
Arc::new(tick),
))
.unwrap();
assert!(
wait_until(Duration::from_secs(2), || cap.len() == 1),
"只收对形的一帧"
);
eng.stop();
}
#[test]
fn f12_2_wrong_key_isolated() {
let eng = ek_event::EventEngine::new();
let tick_cap = Capture::<TickData>::new();
tick_cap.register_on(&eng, format!("{EVENT_TICK}CFFEX.IF2506"));
let log_cap = Capture::<LogData>::new();
log_cap.register_on(&eng, EVENT_LOG);
eng.start();
let tick = fx_tick("IF2506", Exchange::CFFEX, fixture::t0());
eng.put(Event::with_data(EVENT_LOG, Arc::new(tick.clone())))
.unwrap();
eng.put(Event::with_data(
format!("{EVENT_TICK}CFFEX.IF2509"),
Arc::new(tick),
))
.unwrap();
eng.put(Event::with_data(
"eBar.CFFEX.IF2506",
Arc::new("bar-ish".to_string()),
))
.unwrap();
let settle = common::drained(&eng, 1);
assert!(wait_until(Duration::from_secs(2), || settle.len() == 1));
eng.stop();
assert_eq!(tick_cap.len(), 0, "tick 订阅未收任何错键流量");
assert_eq!(log_cap.len(), 0, "log 订阅不收 tick 载荷");
}
#[test]
fn f12_3_invalid_civil_rejected_before_bus() {
let bad = Civil {
year: 2023,
month: 2,
day: 29,
hour: 0,
minute: 0,
second: 0,
nanos: 0,
};
assert!(try_civil_to_nanos(bad).is_none(), "平年 2 月 29 日被拒");
let good = Civil {
year: 2024,
month: 2,
day: 29,
hour: 0,
minute: 0,
second: 0,
nanos: 0,
};
assert!(try_civil_to_nanos(good).is_some(), "闰年 2 月 29 日合法");
}