more test
parent
c34ad15ba1
commit
b5b794817c
|
|
@ -294,6 +294,7 @@ impl Events {
|
||||||
Ok(f) => {
|
Ok(f) => {
|
||||||
for line in std::io::BufReader::new(f).lines() {
|
for line in std::io::BufReader::new(f).lines() {
|
||||||
let line = line.unwrap();
|
let line = line.unwrap();
|
||||||
|
let line = line.trim();
|
||||||
if line.len() > 0 {
|
if line.len() > 0 {
|
||||||
let delta = match serde_json::from_str(&line) {
|
let delta = match serde_json::from_str(&line) {
|
||||||
Ok(v) => Ok(v),
|
Ok(v) => Ok(v),
|
||||||
|
|
@ -307,7 +308,9 @@ impl Events {
|
||||||
Err(msg) => Err(format!("failed to read {}: {}", &log, msg)),
|
Err(msg) => Err(format!("failed to read {}: {}", &log, msg)),
|
||||||
}?;
|
}?;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.sort_by(|a, b| a.ts.cmp(&b.ts));
|
result.sort_by(|a, b| a.ts.cmp(&b.ts));
|
||||||
|
|
||||||
Ok(Events(result))
|
Ok(Events(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -356,12 +359,12 @@ mod test_events {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_events() {
|
fn test_events_oplog_to_snapshot_one() {
|
||||||
let d = tempdir::TempDir::new("").unwrap();
|
let d = tempdir::TempDir::new("").unwrap();
|
||||||
test_file(&d, "plain", "- persisted\n- stage only");
|
test_file(&d, "plain", "- persisted\n- stage only");
|
||||||
test_file(
|
test_file(
|
||||||
&d,
|
&d,
|
||||||
".plain.log",
|
".plain.some_host",
|
||||||
r#"
|
r#"
|
||||||
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
|
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
|
||||||
"#,
|
"#,
|
||||||
|
|
@ -370,12 +373,57 @@ mod test_events {
|
||||||
let events = Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap();
|
let events = Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap();
|
||||||
assert_eq!(1, events.0.len(), "events: {:?}", events);
|
assert_eq!(1, events.0.len(), "events: {:?}", events);
|
||||||
|
|
||||||
assert!(false, "not impl");
|
let snapshot = events.snapshot().unwrap();
|
||||||
|
assert_eq!(1, snapshot.len());
|
||||||
|
assert_eq!(
|
||||||
|
serde_yaml::Value::String("persisted".to_string()),
|
||||||
|
snapshot[0].0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_events_oplog_to_snapshot_complex() {
|
||||||
|
let d = tempdir::TempDir::new("").unwrap();
|
||||||
|
test_file(&d, "plain", "- ignored");
|
||||||
|
test_file(
|
||||||
|
&d,
|
||||||
|
".plain.some_host",
|
||||||
|
r#"
|
||||||
|
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
|
||||||
|
{"ts":3, "patch":{"op":"add", "path":"/-", "value":"persisted 3"}}
|
||||||
|
{"ts":2, "patch":{"op":"add", "path":"/-", "value":"persisted 2"}}
|
||||||
|
{"ts":4, "patch":{"op":"add", "path":"/-", "value":"persisted 4"}}
|
||||||
|
{"ts":5, "patch":{"op":"add", "path":"/-", "value":"persisted 5"}}
|
||||||
|
{"ts":6, "patch":{"op":"replace", "path":"/4", "value":"persisted 5'"}}
|
||||||
|
{"ts":7, "patch":{"op":"remove", "path":"/3"}}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
let events = Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap();
|
||||||
|
|
||||||
|
let snapshot = events.snapshot().unwrap();
|
||||||
|
assert_eq!(4, snapshot.len());
|
||||||
|
assert_eq!(
|
||||||
|
serde_yaml::Value::String("persisted".to_string()),
|
||||||
|
snapshot[0].0
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
serde_yaml::Value::String("persisted 2".to_string()),
|
||||||
|
snapshot[1].0
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
serde_yaml::Value::String("persisted 3".to_string()),
|
||||||
|
snapshot[2].0
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
serde_yaml::Value::String("persisted 5'".to_string()),
|
||||||
|
snapshot[3].0
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_file(d: &tempdir::TempDir, fname: &str, content: &str) {
|
fn test_file(d: &tempdir::TempDir, fname: &str, content: &str) {
|
||||||
let mut f = std::fs::File::create(d.path().join("plain")).unwrap();
|
let mut f = std::fs::File::create(d.path().join(&fname)).unwrap();
|
||||||
writeln!(f, "- persisted\n- stage only").unwrap();
|
writeln!(f, "{}", &content).unwrap();
|
||||||
f.sync_all().unwrap();
|
f.sync_all().unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue