aboutsummaryrefslogtreecommitdiffstats
path: root/src/remake/records.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/remake/records.rs')
-rw-r--r--src/remake/records.rs91
1 files changed, 45 insertions, 46 deletions
diff --git a/src/remake/records.rs b/src/remake/records.rs
index f22b1e5..40b563a 100644
--- a/src/remake/records.rs
+++ b/src/remake/records.rs
@@ -499,6 +499,7 @@ pub(crate) fn decode_hex32(hex: &str) -> Option<[u8; 32]> {
#[cfg(test)]
mod tests {
use super::*;
+ use crate::document::testing::change;
#[test]
fn retention_covers_until_every_limit_passed() {
@@ -570,16 +571,6 @@ mod tests {
fn round_trip_through_the_document() {
let mut doc = Document::new();
let p = proposal(10_000);
- write_proposal(&mut doc, "c-1", &p).unwrap();
- write_abort_request(
- &mut doc,
- "c-1",
- &AbortRequest {
- by: "x".into(),
- at: UnixMillis(5),
- },
- )
- .unwrap();
let decision = Decision::Committed {
to_epoch: EpochRecord {
generation: 1,
@@ -589,14 +580,25 @@ mod tests {
snapshot_bytes: 123,
decided_at: UnixMillis(11_000),
};
- write_decision(&mut doc, "c-1", &decision).unwrap();
let vote = |remake: &str| Vote {
remake: remake.into(),
at: UnixMillis(7),
};
- write_vote(&mut doc, "node-b", &vote("c-1")).unwrap();
- write_vote(&mut doc, "node-c", &vote("other")).unwrap();
- doc.commit();
+ change(&mut doc, |doc| {
+ write_proposal(doc, "c-1", &p).unwrap();
+ write_abort_request(
+ doc,
+ "c-1",
+ &AbortRequest {
+ by: "x".into(),
+ at: UnixMillis(5),
+ },
+ )
+ .unwrap();
+ write_decision(doc, "c-1", &decision).unwrap();
+ write_vote(doc, "node-b", &vote("c-1")).unwrap();
+ write_vote(doc, "node-c", &vote("other")).unwrap();
+ });
let all = load_all(&doc);
assert_eq!(all.len(), 1);
@@ -617,14 +619,11 @@ mod tests {
remake: remake.into(),
at: UnixMillis(at),
};
- write_vote(&mut a, "node-a", &vote("x", 1)).unwrap();
- a.commit();
+ change(&mut a, |a| write_vote(a, "node-a", &vote("x", 1)).unwrap());
let mut b = a.fork();
// node-b votes in both, never seeing its other vote (a lost vote).
- write_vote(&mut a, "node-b", &vote("x", 2)).unwrap();
- write_vote(&mut b, "node-b", &vote("y", 3)).unwrap();
- a.commit();
- b.commit();
+ change(&mut a, |a| write_vote(a, "node-b", &vote("x", 2)).unwrap());
+ change(&mut b, |b| write_vote(b, "node-b", &vote("y", 3)).unwrap());
a.merge(&mut b).unwrap();
let register = load_register(&a);
@@ -634,8 +633,7 @@ mod tests {
assert_eq!(twice, [vote("x", 2), vote("y", 3)]);
// Seen, a vote is replaced.
- write_vote(&mut a, "node-b", &vote("z", 4)).unwrap();
- a.commit();
+ change(&mut a, |a| write_vote(a, "node-b", &vote("z", 4)).unwrap());
assert_eq!(load_register(&a)["node-b"], [vote("z", 4)]);
}
@@ -686,8 +684,9 @@ mod tests {
#[test]
fn disagreeing_decisions_are_contested() {
let mut base = Document::new();
- write_proposal(&mut base, "c-1", &proposal(10_000)).unwrap();
- base.commit();
+ change(&mut base, |base| {
+ write_proposal(base, "c-1", &proposal(10_000)).unwrap()
+ });
let (mut a, mut b, mut c) = (base.fork(), base.fork(), base.fork());
let committed = Decision::Committed {
to_epoch: EpochRecord {
@@ -698,21 +697,20 @@ mod tests {
snapshot_bytes: 1,
decided_at: UnixMillis(2),
};
- write_decision(&mut a, "c-1", &committed).unwrap();
- write_decision(
- &mut b,
- "c-1",
- &Decision::Aborted {
- reason: "test".into(),
- decided_at: UnixMillis(3),
- },
- )
- .unwrap();
+ change(&mut a, |a| write_decision(a, "c-1", &committed).unwrap());
+ change(&mut b, |b| {
+ write_decision(
+ b,
+ "c-1",
+ &Decision::Aborted {
+ reason: "test".into(),
+ decided_at: UnixMillis(3),
+ },
+ )
+ .unwrap()
+ });
// The same decision, concurrently: they agree.
- write_decision(&mut c, "c-1", &committed).unwrap();
- a.commit();
- b.commit();
- c.commit();
+ change(&mut c, |c| write_decision(c, "c-1", &committed).unwrap());
let mut agreeing = a.fork();
agreeing.merge(&mut c).unwrap();
assert!(!load(&agreeing, "c-1").unwrap().contested);
@@ -726,14 +724,15 @@ mod tests {
#[test]
fn unreadable_records_are_skipped() {
let mut doc = Document::new();
- store::write_path(
- &mut doc,
- &record_path("broken", ""),
- &serde_json::json!({ "proposal": "not an object" }),
- )
- .unwrap();
- write_proposal(&mut doc, "fine", &proposal(1)).unwrap();
- doc.commit();
+ change(&mut doc, |doc| {
+ store::write_path(
+ doc,
+ &record_path("broken", ""),
+ &serde_json::json!({ "proposal": "not an object" }),
+ )
+ .unwrap();
+ write_proposal(doc, "fine", &proposal(1)).unwrap();
+ });
let all = load_all(&doc);
assert_eq!(all.len(), 1);
assert_eq!(all[0].0, "fine");