Clear resting sports orders once at scheduled kickoff
This commit is contained in:
@@ -2312,3 +2312,83 @@ fn replayed_tick_changes_preserve_valid_orders_and_release_only_residuals() {
|
||||
assert_eq!(page["events"][0]["seq"], original["tick_seq"]);
|
||||
assert_eq!(page["has_more"], false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kickoff_clears_three_books_once_and_returns_only_remaining_reserves() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("football.db");
|
||||
let mut exchange = Exchange::open(&path).unwrap();
|
||||
setup(&mut exchange);
|
||||
for line in include_str!("../../../docs/examples/football.jsonl").lines() {
|
||||
let mut command: Value = serde_json::from_str(line).unwrap();
|
||||
if command["op"] == "event" {
|
||||
command["scheduled_at_ms"] = json!(i64::MAX);
|
||||
}
|
||||
exchange.execute(command).unwrap();
|
||||
}
|
||||
let mut other = Exchange::open(&path).unwrap();
|
||||
exchange
|
||||
.execute(json!({"op":"open","id":"full-open","market":"full-2-1"}))
|
||||
.unwrap();
|
||||
exchange
|
||||
.execute(
|
||||
json!({"op":"split","id":"full-split","market":"full-2-1","account":"alice","qty":4}),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut place = |id, account, side, price, qty, market: &str| {
|
||||
let mut cmd = order(id, account, side, price, qty);
|
||||
cmd["market"] = json!(market);
|
||||
exchange.execute(cmd).unwrap()
|
||||
};
|
||||
place("ask-full", "alice", "sell", 60, 4, "full-2-1");
|
||||
place("fill-one", "bob", "buy", 60, 1, "full-2-1");
|
||||
place("bid-half", "bob", "buy", 40, 2, "half-1-0");
|
||||
place("bid-goals", "bob", "buy", 30, 3, "over-25");
|
||||
place("unrelated", "bob", "buy", 20, 1, "e");
|
||||
let book = json!({"op":"book","market":"full-2-1","outcome":"YES"});
|
||||
assert_eq!(
|
||||
other.execute(book.clone()).unwrap()["asks"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.len(),
|
||||
1
|
||||
);
|
||||
let reschedule = json!({"op":"reschedule_sports","id":"early","event":"cup-a-b",
|
||||
"previous_ms":i64::MAX,"scheduled_at_ms":1});
|
||||
let receipt = exchange.execute(reschedule.clone()).unwrap();
|
||||
assert_eq!(other.execute(book.clone()).unwrap()["asks"], json!([]));
|
||||
let s = state(&mut other);
|
||||
assert_eq!(s["accounts"][1]["cash"], 1920);
|
||||
let positions = s["positions"].as_array().unwrap();
|
||||
assert!(positions.iter().any(|p| p["account"] == "alice"
|
||||
&& p["market"] == "full-2-1"
|
||||
&& p["outcome"] == "YES"
|
||||
&& p["quantity"] == 3));
|
||||
assert_eq!(s["orders"][4]["status"], "live");
|
||||
let catalog = json!({"op":"catalog","event":"cup-a-b"});
|
||||
let meta = other.execute(catalog.clone()).unwrap()["event"]["kickoff"].clone();
|
||||
assert_eq!(meta["cancelled_orders"], 3);
|
||||
assert!(meta["cleared_at_ms"].as_i64().unwrap() >= 1);
|
||||
let mut live = order("in-play", "alice", "sell", 60, 3);
|
||||
live["market"] = json!("full-2-1");
|
||||
other.execute(live).unwrap();
|
||||
drop(exchange);
|
||||
drop(other);
|
||||
let mut recovered = Exchange::open(path).unwrap();
|
||||
assert_eq!(recovered.execute(reschedule.clone()).unwrap(), receipt);
|
||||
let mut stale = reschedule;
|
||||
stale["id"] = json!("late-retry");
|
||||
assert!(recovered.execute(stale).is_err());
|
||||
assert_eq!(
|
||||
recovered.execute(catalog).unwrap()["event"]["kickoff"],
|
||||
meta
|
||||
);
|
||||
assert_eq!(
|
||||
recovered.execute(book).unwrap()["asks"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.len(),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user