|
|
@@ -480,3 +480,186 @@ async fn projection_matches_under_concurrent_commits() {
|
|
|
|
|
|
assert_projection_matches(&ledger, &[1, 2, 3, 4, 5, 6, 7, 8, 9, 99]).await;
|
|
|
}
|
|
|
+
|
|
|
+/// An as-of read is bounded above by `as_of`: with no cache point anywhere, it
|
|
|
+/// folds the history up to that instant and the later commits are simply absent.
|
|
|
+/// Each batch is separated by a real millisecond, since two commits inside one
|
|
|
+/// millisecond share a stamp and would make the boundary ambiguous.
|
|
|
+#[tokio::test]
|
|
|
+async fn balance_as_of_excludes_later_commits() {
|
|
|
+ let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
+ no_overdraft(&ledger, 1).await;
|
|
|
+ no_overdraft(&ledger, 2).await;
|
|
|
+ overdraft(&ledger, 99).await;
|
|
|
+
|
|
|
+ deposit(&ledger, 1, 1000).await;
|
|
|
+ pay(&ledger, 1, 2, 250).await;
|
|
|
+
|
|
|
+ let as_of = unix_millis_now();
|
|
|
+ tokio::time::sleep(std::time::Duration::from_millis(3)).await;
|
|
|
+ deposit(&ledger, 1, 500).await;
|
|
|
+
|
|
|
+ assert_eq!(
|
|
|
+ ledger
|
|
|
+ .balance_as_of(&account(1), &usd(), as_of)
|
|
|
+ .await
|
|
|
+ .unwrap(),
|
|
|
+ Cent::from(750),
|
|
|
+ "the as-of read picked up a transfer committed after it"
|
|
|
+ );
|
|
|
+ assert_eq!(
|
|
|
+ ledger.balance(&account(1), &usd()).await.unwrap(),
|
|
|
+ Cent::from(1250)
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+/// The as-of read takes the closest cache point at or before `as_of` and folds
|
|
|
+/// only the window between that point's watermark and `as_of`, so the two
|
|
|
+/// derivations (pinned snapshot plus bounded tail, and the hand-computed total)
|
|
|
+/// agree while later commits stay out. The `assert_ne!` guards the test itself:
|
|
|
+/// a snapshot that already held the as-of balance would leave the tail empty and
|
|
|
+/// quietly degrade this to a case the other tests cover.
|
|
|
+#[tokio::test]
|
|
|
+async fn balance_as_of_folds_a_bounded_tail_onto_its_cache_point() {
|
|
|
+ let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
+ no_overdraft(&ledger, 1).await;
|
|
|
+ no_overdraft(&ledger, 2).await;
|
|
|
+ overdraft(&ledger, 99).await;
|
|
|
+
|
|
|
+ deposit(&ledger, 1, 1000).await;
|
|
|
+ pay(&ledger, 1, 2, 250).await;
|
|
|
+
|
|
|
+ let snapshot = ledger.balance_exact(&account(1), &usd()).await.unwrap();
|
|
|
+ let watermark = unix_millis_now();
|
|
|
+ ledger
|
|
|
+ .store()
|
|
|
+ .append_balance_projection(&account(1), &usd(), snapshot, watermark)
|
|
|
+ .await
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ tokio::time::sleep(std::time::Duration::from_millis(3)).await;
|
|
|
+ deposit(&ledger, 1, 500).await;
|
|
|
+
|
|
|
+ let as_of = unix_millis_now();
|
|
|
+ tokio::time::sleep(std::time::Duration::from_millis(3)).await;
|
|
|
+ deposit(&ledger, 1, 300).await;
|
|
|
+
|
|
|
+ assert_eq!(
|
|
|
+ ledger
|
|
|
+ .balance_as_of(&account(1), &usd(), as_of)
|
|
|
+ .await
|
|
|
+ .unwrap(),
|
|
|
+ Cent::from(1250),
|
|
|
+ "snapshot plus bounded tail diverged from the total at as_of"
|
|
|
+ );
|
|
|
+ assert_eq!(
|
|
|
+ ledger.balance(&account(1), &usd()).await.unwrap(),
|
|
|
+ Cent::from(1550)
|
|
|
+ );
|
|
|
+ assert_ne!(
|
|
|
+ snapshot,
|
|
|
+ Cent::from(1250),
|
|
|
+ "the snapshot already held the as-of balance; the tail was empty"
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+/// A cache point whose watermark is above `as_of` covers transfers the read must
|
|
|
+/// not see, so the read falls back to the history below `as_of` instead.
|
|
|
+#[tokio::test]
|
|
|
+async fn balance_as_of_ignores_a_cache_point_above_it() {
|
|
|
+ let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
+ no_overdraft(&ledger, 1).await;
|
|
|
+ no_overdraft(&ledger, 2).await;
|
|
|
+ overdraft(&ledger, 99).await;
|
|
|
+
|
|
|
+ deposit(&ledger, 1, 1000).await;
|
|
|
+ pay(&ledger, 1, 2, 250).await;
|
|
|
+
|
|
|
+ let as_of = unix_millis_now();
|
|
|
+ tokio::time::sleep(std::time::Duration::from_millis(3)).await;
|
|
|
+ deposit(&ledger, 1, 500).await;
|
|
|
+
|
|
|
+ let full = ledger.balance_exact(&account(1), &usd()).await.unwrap();
|
|
|
+ ledger
|
|
|
+ .store()
|
|
|
+ .append_balance_projection(&account(1), &usd(), full, unix_millis_now())
|
|
|
+ .await
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ assert_eq!(
|
|
|
+ ledger
|
|
|
+ .balance_as_of(&account(1), &usd(), as_of)
|
|
|
+ .await
|
|
|
+ .unwrap(),
|
|
|
+ Cent::from(750),
|
|
|
+ "the as-of read used a cache point covering later commits"
|
|
|
+ );
|
|
|
+ assert_eq!(ledger.balance(&account(1), &usd()).await.unwrap(), full);
|
|
|
+}
|
|
|
+
|
|
|
+/// An as-of read appends no cache point, even past the interval that makes the
|
|
|
+/// everyday read append one: a new point's watermark comes from the current
|
|
|
+/// clock, so it would describe a window the as-of read never looked at.
|
|
|
+#[tokio::test]
|
|
|
+async fn balance_as_of_appends_no_cache_point() {
|
|
|
+ let ledger = Arc::new(Ledger::new(InMemoryStore::new()).with_projection_config(
|
|
|
+ ProjectionConfig {
|
|
|
+ grace_ms: 0,
|
|
|
+ snapshot_interval: 1,
|
|
|
+ },
|
|
|
+ ));
|
|
|
+ no_overdraft(&ledger, 1).await;
|
|
|
+ no_overdraft(&ledger, 2).await;
|
|
|
+ overdraft(&ledger, 99).await;
|
|
|
+
|
|
|
+ deposit(&ledger, 1, 1000).await;
|
|
|
+ pay(&ledger, 1, 2, 250).await;
|
|
|
+
|
|
|
+ let _ = ledger
|
|
|
+ .balance_as_of(&account(1), &usd(), unix_millis_now())
|
|
|
+ .await
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ for _ in 0..1000 {
|
|
|
+ tokio::task::yield_now().await;
|
|
|
+ }
|
|
|
+ assert!(
|
|
|
+ ledger
|
|
|
+ .store()
|
|
|
+ .get_closest_balance_projection(&account(1), &usd(), i64::MAX)
|
|
|
+ .await
|
|
|
+ .unwrap()
|
|
|
+ .is_none(),
|
|
|
+ "an as-of read appended a cache point"
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+/// Both ends of the history: before the first commit an account holds nothing,
|
|
|
+/// and an `as_of` past every commit is the everyday balance.
|
|
|
+#[tokio::test]
|
|
|
+async fn balance_as_of_outside_the_committed_history() {
|
|
|
+ let ledger = Arc::new(Ledger::new(InMemoryStore::new()));
|
|
|
+ no_overdraft(&ledger, 1).await;
|
|
|
+ no_overdraft(&ledger, 2).await;
|
|
|
+ overdraft(&ledger, 99).await;
|
|
|
+
|
|
|
+ let before = unix_millis_now();
|
|
|
+ tokio::time::sleep(std::time::Duration::from_millis(3)).await;
|
|
|
+ deposit(&ledger, 1, 1000).await;
|
|
|
+ pay(&ledger, 1, 2, 250).await;
|
|
|
+
|
|
|
+ assert_eq!(
|
|
|
+ ledger
|
|
|
+ .balance_as_of(&account(1), &usd(), before)
|
|
|
+ .await
|
|
|
+ .unwrap(),
|
|
|
+ Cent::from(0)
|
|
|
+ );
|
|
|
+ assert_eq!(
|
|
|
+ ledger
|
|
|
+ .balance_as_of(&account(1), &usd(), i64::MAX)
|
|
|
+ .await
|
|
|
+ .unwrap(),
|
|
|
+ ledger.balance(&account(1), &usd()).await.unwrap()
|
|
|
+ );
|
|
|
+}
|