Nathan.fooo 8c225fe547
Feat/restore revision (#1549)
* chore: write snapshot

* chore: add tests

* chore: sync close

* chore: restore from snapshot

* chore: delete invalid revisions after restored from snapshot

* chore: create default view if it fail to deserialize view's revisions when there is no snapshot

* chore: auto generate snapshot

Co-authored-by: nathan <nathan@appflowy.io>
2022-12-09 09:19:47 +08:00

256 lines
6.6 KiB
Rust

use crate::revision_test::script::{RevisionScript::*, RevisionTest};
#[tokio::test]
async fn revision_sync_test() {
let test = RevisionTest::new().await;
let rev_id = 1;
test.run_script(AddLocalRevision {
content: "123".to_string(),
})
.await;
test.run_script(AssertNextSyncRevisionId { rev_id: Some(rev_id) }).await;
test.run_script(AckRevision { rev_id }).await;
test.run_script(AssertNextSyncRevisionId { rev_id: None }).await;
}
#[tokio::test]
async fn revision_compress_2_revisions_with_2_threshold_test() {
let test = RevisionTest::new_with_configuration(2).await;
test.run_script(AddLocalRevision2 {
content: "123".to_string(),
})
.await;
test.run_script(AddLocalRevision2 {
content: "456".to_string(),
})
.await;
test.run_scripts(vec![
AssertNextSyncRevisionId { rev_id: Some(1) },
AssertNextSyncRevisionContent {
expected: "123456".to_string(),
},
AckRevision { rev_id: 1 },
AssertNextSyncRevisionId { rev_id: None },
])
.await;
}
#[tokio::test]
async fn revision_compress_4_revisions_with_threshold_2_test() {
let test = RevisionTest::new_with_configuration(2).await;
let rev_id_1 = 1;
test.run_script(AddLocalRevision {
content: "1".to_string(),
})
.await;
let rev_id_2 = 2;
test.run_script(AddLocalRevision {
content: "2".to_string(),
})
.await;
test.run_script(AddLocalRevision {
content: "3".to_string(),
})
.await;
test.run_script(AddLocalRevision {
content: "4".to_string(),
})
.await;
// rev_id_2,rev_id_3,rev_id4 will be merged into rev_id_1
test.run_scripts(vec![
AssertNumberOfSyncRevisions { num: 2 },
AssertNextSyncRevisionId { rev_id: Some(rev_id_1) },
AssertNextSyncRevisionContent {
expected: "12".to_string(),
},
AckRevision { rev_id: rev_id_1 },
AssertNextSyncRevisionId { rev_id: Some(rev_id_2) },
AssertNextSyncRevisionContent {
expected: "34".to_string(),
},
])
.await;
}
#[tokio::test]
async fn revision_compress_8_revisions_with_threshold_4_test() {
let test = RevisionTest::new_with_configuration(4).await;
let rev_id_1 = 1;
test.run_script(AddLocalRevision {
content: "1".to_string(),
})
.await;
test.run_script(AddLocalRevision {
content: "2".to_string(),
})
.await;
test.run_script(AddLocalRevision {
content: "3".to_string(),
})
.await;
test.run_script(AddLocalRevision {
content: "4".to_string(),
})
.await;
let rev_id_a = 2;
test.run_script(AddLocalRevision {
content: "a".to_string(),
})
.await;
test.run_script(AddLocalRevision {
content: "b".to_string(),
})
.await;
test.run_script(AddLocalRevision {
content: "c".to_string(),
})
.await;
test.run_script(AddLocalRevision {
content: "d".to_string(),
})
.await;
test.run_scripts(vec![
AssertNumberOfSyncRevisions { num: 2 },
AssertNextSyncRevisionId { rev_id: Some(rev_id_1) },
AssertNextSyncRevisionContent {
expected: "1234".to_string(),
},
AckRevision { rev_id: rev_id_1 },
AssertNextSyncRevisionId { rev_id: Some(rev_id_a) },
AssertNextSyncRevisionContent {
expected: "abcd".to_string(),
},
AckRevision { rev_id: rev_id_a },
AssertNextSyncRevisionId { rev_id: None },
])
.await;
}
#[tokio::test]
async fn revision_merge_per_5_revision_test() {
let test = RevisionTest::new_with_configuration(5).await;
for i in 0..20 {
let content = format!("{}", i);
test.run_script(AddLocalRevision { content }).await;
}
test.run_scripts(vec![
AssertNumberOfSyncRevisions { num: 4 },
AssertNextSyncRevisionContent {
expected: "01234".to_string(),
},
AckRevision { rev_id: 1 },
AssertNextSyncRevisionContent {
expected: "56789".to_string(),
},
AckRevision { rev_id: 2 },
AssertNextSyncRevisionContent {
expected: "1011121314".to_string(),
},
AckRevision { rev_id: 3 },
AssertNextSyncRevisionContent {
expected: "1516171819".to_string(),
},
AckRevision { rev_id: 4 },
AssertNextSyncRevisionId { rev_id: None },
])
.await;
}
#[tokio::test]
async fn revision_merge_per_100_revision_test() {
let test = RevisionTest::new_with_configuration(100).await;
for i in 0..1000 {
let content = format!("{}", i);
test.run_script(AddLocalRevision { content }).await;
}
test.run_scripts(vec![AssertNumberOfSyncRevisions { num: 10 }]).await;
}
#[tokio::test]
async fn revision_merge_per_100_revision_test2() {
let test = RevisionTest::new_with_configuration(100).await;
for i in 0..50 {
test.run_script(AddLocalRevision {
content: format!("{}", i),
})
.await;
}
test.run_scripts(vec![AssertNumberOfSyncRevisions { num: 50 }]).await;
}
#[tokio::test]
async fn revision_merge_per_1000_revision_test() {
let test = RevisionTest::new_with_configuration(1000).await;
for i in 0..100000 {
test.run_script(AddLocalRevision {
content: format!("{}", i),
})
.await;
}
test.run_scripts(vec![AssertNumberOfSyncRevisions { num: 100 }]).await;
}
#[tokio::test]
async fn revision_compress_revision_test() {
let test = RevisionTest::new_with_configuration(2).await;
test.run_scripts(vec![
AddLocalRevision2 {
content: "1".to_string(),
},
AddLocalRevision2 {
content: "2".to_string(),
},
AddLocalRevision2 {
content: "3".to_string(),
},
AddLocalRevision2 {
content: "4".to_string(),
},
AssertNumberOfSyncRevisions { num: 2 },
])
.await;
}
#[tokio::test]
async fn revision_compress_revision_while_recv_ack_test() {
let test = RevisionTest::new_with_configuration(2).await;
test.run_scripts(vec![
AddLocalRevision2 {
content: "1".to_string(),
},
AckRevision { rev_id: 1 },
AddLocalRevision2 {
content: "2".to_string(),
},
AckRevision { rev_id: 2 },
AddLocalRevision2 {
content: "3".to_string(),
},
AckRevision { rev_id: 3 },
AddLocalRevision2 {
content: "4".to_string(),
},
AssertNumberOfSyncRevisions { num: 4 },
])
.await;
}