AppFlowy/frontend/rust-lib/flowy-revision/tests/revision_test/script.rs

336 lines
11 KiB
Rust
Raw Normal View History

2022-11-02 02:23:54 +00:00
use bytes::Bytes;
2022-11-07 12:22:08 +00:00
use flowy_error::{internal_error, FlowyError, FlowyResult};
2022-11-02 02:23:54 +00:00
use flowy_revision::{
RevisionManager, RevisionMergeable, RevisionObjectDeserializer, RevisionPersistence,
RevisionPersistenceConfiguration, RevisionSnapshot, RevisionSnapshotDiskCache, REVISION_WRITE_INTERVAL_IN_MILLIS,
2022-11-02 02:23:54 +00:00
};
use flowy_revision_persistence::{RevisionChangeset, RevisionDiskCache, SyncRecord};
2022-11-07 12:22:08 +00:00
use flowy_http_model::revision::{Revision, RevisionRange};
use flowy_http_model::util::md5;
2022-11-02 02:23:54 +00:00
use nanoid::nanoid;
2022-11-02 09:15:27 +00:00
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
2022-11-02 02:23:54 +00:00
use std::sync::Arc;
2022-11-02 09:15:27 +00:00
use std::time::Duration;
2022-11-02 02:23:54 +00:00
pub enum RevisionScript {
AddLocalRevision { content: String },
AddLocalRevision2 { content: String },
AddInvalidLocalRevision { bytes: Vec<u8> },
AckRevision { rev_id: i64 },
AssertNextSyncRevisionId { rev_id: Option<i64> },
AssertNumberOfSyncRevisions { num: usize },
AssertNumberOfRevisionsInDisk { num: usize },
AssertNextSyncRevisionContent { expected: String },
WaitWhenWriteToDisk,
2022-11-02 02:23:54 +00:00
}
pub struct RevisionTest {
user_id: String,
object_id: String,
configuration: RevisionPersistenceConfiguration,
2022-11-02 02:23:54 +00:00
rev_manager: Arc<RevisionManager<RevisionConnectionMock>>,
}
impl RevisionTest {
pub async fn new() -> Self {
2022-11-06 01:59:53 +00:00
Self::new_with_configuration(2).await
}
pub async fn new_with_configuration(merge_threshold: i64) -> Self {
2022-11-02 02:23:54 +00:00
let user_id = nanoid!(10);
let object_id = nanoid!(6);
let configuration = RevisionPersistenceConfiguration::new(merge_threshold as usize, false);
let disk_cache = RevisionDiskCacheMock::new(vec![]);
let persistence = RevisionPersistence::new(&user_id, &object_id, disk_cache, configuration.clone());
let compress = RevisionMergeableMock {};
2022-11-02 02:23:54 +00:00
let snapshot = RevisionSnapshotMock {};
let mut rev_manager = RevisionManager::new(&user_id, &object_id, persistence, compress, snapshot);
rev_manager.initialize::<RevisionObjectMockSerde>(None).await.unwrap();
2022-11-02 02:23:54 +00:00
Self {
user_id,
object_id,
configuration,
2022-11-02 02:23:54 +00:00
rev_manager: Arc::new(rev_manager),
}
}
2022-11-06 01:59:53 +00:00
pub async fn new_with_other(old_test: RevisionTest) -> Self {
let records = old_test.rev_manager.get_all_revision_records().unwrap();
let disk_cache = RevisionDiskCacheMock::new(records);
let configuration = old_test.configuration;
let persistence = RevisionPersistence::new(
&old_test.user_id,
&old_test.object_id,
disk_cache,
configuration.clone(),
);
let compress = RevisionMergeableMock {};
let snapshot = RevisionSnapshotMock {};
let mut rev_manager =
RevisionManager::new(&old_test.user_id, &old_test.object_id, persistence, compress, snapshot);
rev_manager.initialize::<RevisionObjectMockSerde>(None).await.unwrap();
Self {
user_id: old_test.user_id,
object_id: old_test.object_id,
configuration,
rev_manager: Arc::new(rev_manager),
}
}
2022-11-02 02:23:54 +00:00
pub async fn run_scripts(&self, scripts: Vec<RevisionScript>) {
for script in scripts {
self.run_script(script).await;
}
}
2022-11-02 09:15:27 +00:00
2022-11-02 02:23:54 +00:00
pub async fn run_script(&self, script: RevisionScript) {
match script {
RevisionScript::AddLocalRevision { content } => {
2022-11-02 09:15:27 +00:00
let object = RevisionObjectMock::new(&content);
let bytes = object.to_bytes();
let md5 = md5(&bytes);
self.rev_manager
.add_local_revision(Bytes::from(bytes), md5)
.await
.unwrap();
2022-11-02 02:23:54 +00:00
}
RevisionScript::AddLocalRevision2 { content } => {
let object = RevisionObjectMock::new(&content);
let bytes = object.to_bytes();
let md5 = md5(&bytes);
self.rev_manager
.add_local_revision(Bytes::from(bytes), md5)
.await
.unwrap();
}
RevisionScript::AddInvalidLocalRevision { bytes } => {
let md5 = md5(&bytes);
self.rev_manager
.add_local_revision(Bytes::from(bytes), md5)
.await
.unwrap();
}
2022-11-02 02:23:54 +00:00
RevisionScript::AckRevision { rev_id } => {
//
self.rev_manager.ack_revision(rev_id).await.unwrap()
}
RevisionScript::AssertNextSyncRevisionId { rev_id } => {
2022-11-02 09:15:27 +00:00
assert_eq!(self.rev_manager.next_sync_rev_id().await, rev_id)
}
2022-11-06 01:59:53 +00:00
RevisionScript::AssertNumberOfSyncRevisions { num } => {
assert_eq!(self.rev_manager.number_of_sync_revisions(), num)
}
RevisionScript::AssertNumberOfRevisionsInDisk { num } => {
assert_eq!(self.rev_manager.number_of_revisions_in_disk(), num)
}
2022-11-02 09:15:27 +00:00
RevisionScript::AssertNextSyncRevisionContent { expected } => {
2022-11-02 02:23:54 +00:00
//
2022-11-02 09:15:27 +00:00
let rev_id = self.rev_manager.next_sync_rev_id().await.unwrap();
let revision = self.rev_manager.get_revision(rev_id).await.unwrap();
2022-11-07 12:22:08 +00:00
let object = RevisionObjectMock::from_bytes(&revision.bytes).unwrap();
2022-11-02 09:15:27 +00:00
assert_eq!(object.content, expected);
}
RevisionScript::WaitWhenWriteToDisk => {
let milliseconds = 2 * REVISION_WRITE_INTERVAL_IN_MILLIS;
2022-11-02 09:15:27 +00:00
tokio::time::sleep(Duration::from_millis(milliseconds)).await;
2022-11-02 02:23:54 +00:00
}
}
}
}
2022-11-02 09:15:27 +00:00
pub struct RevisionDiskCacheMock {
records: RwLock<Vec<SyncRecord>>,
}
2022-11-02 02:23:54 +00:00
impl RevisionDiskCacheMock {
pub fn new(records: Vec<SyncRecord>) -> Self {
2022-11-02 09:15:27 +00:00
Self {
records: RwLock::new(records),
2022-11-02 09:15:27 +00:00
}
2022-11-02 02:23:54 +00:00
}
}
impl RevisionDiskCache<RevisionConnectionMock> for RevisionDiskCacheMock {
type Error = FlowyError;
fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error> {
2022-11-02 09:15:27 +00:00
self.records.write().extend(revision_records);
Ok(())
2022-11-02 02:23:54 +00:00
}
fn get_connection(&self) -> Result<RevisionConnectionMock, Self::Error> {
todo!()
}
fn read_revision_records(
&self,
2022-11-06 01:59:53 +00:00
_object_id: &str,
rev_ids: Option<Vec<i64>>,
2022-11-02 02:23:54 +00:00
) -> Result<Vec<SyncRecord>, Self::Error> {
match rev_ids {
None => Ok(self.records.read().clone()),
Some(rev_ids) => Ok(self
.records
.read()
.iter()
.filter(|record| rev_ids.contains(&record.revision.rev_id))
.cloned()
.collect::<Vec<SyncRecord>>()),
}
2022-11-02 02:23:54 +00:00
}
fn read_revision_records_with_range(
&self,
2022-11-06 01:59:53 +00:00
_object_id: &str,
range: &RevisionRange,
2022-11-02 02:23:54 +00:00
) -> Result<Vec<SyncRecord>, Self::Error> {
let read_guard = self.records.read();
let records = range
.iter()
.flat_map(|rev_id| {
read_guard
.iter()
.find(|record| record.revision.rev_id == rev_id)
.cloned()
})
.collect::<Vec<SyncRecord>>();
Ok(records)
2022-11-02 02:23:54 +00:00
}
fn update_revision_record(&self, changesets: Vec<RevisionChangeset>) -> FlowyResult<()> {
2022-11-02 09:15:27 +00:00
for changeset in changesets {
if let Some(record) = self
.records
.write()
.iter_mut()
.find(|record| record.revision.rev_id == changeset.rev_id)
2022-11-02 09:15:27 +00:00
{
record.state = changeset.state;
}
}
Ok(())
2022-11-02 02:23:54 +00:00
}
2022-11-06 01:59:53 +00:00
fn delete_revision_records(&self, _object_id: &str, rev_ids: Option<Vec<i64>>) -> Result<(), Self::Error> {
2022-11-02 09:15:27 +00:00
match rev_ids {
None => {}
Some(rev_ids) => {
for rev_id in rev_ids {
if let Some(index) = self
.records
.read()
.iter()
.position(|record| record.revision.rev_id == rev_id)
{
self.records.write().remove(index);
}
}
}
}
Ok(())
2022-11-02 02:23:54 +00:00
}
fn delete_and_insert_records(
&self,
2022-11-06 01:59:53 +00:00
_object_id: &str,
_deleted_rev_ids: Option<Vec<i64>>,
_inserted_records: Vec<SyncRecord>,
2022-11-02 02:23:54 +00:00
) -> Result<(), Self::Error> {
todo!()
}
}
pub struct RevisionConnectionMock {}
pub struct RevisionSnapshotMock {}
impl RevisionSnapshotDiskCache for RevisionSnapshotMock {
fn write_snapshot(&self, _rev_id: i64, _data: Vec<u8>) -> FlowyResult<()> {
2022-11-02 02:23:54 +00:00
todo!()
}
fn read_snapshot(&self, _rev_id: i64) -> FlowyResult<Option<RevisionSnapshot>> {
2022-11-02 02:23:54 +00:00
todo!()
}
fn read_last_snapshot(&self) -> FlowyResult<Option<RevisionSnapshot>> {
Ok(None)
}
2022-11-02 02:23:54 +00:00
}
pub struct RevisionMergeableMock {}
2022-11-02 02:23:54 +00:00
impl RevisionMergeable for RevisionMergeableMock {
2022-11-02 02:23:54 +00:00
fn combine_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
2022-11-02 09:15:27 +00:00
let mut object = RevisionObjectMock::new("");
for revision in revisions {
2022-11-07 12:22:08 +00:00
if let Ok(other) = RevisionObjectMock::from_bytes(&revision.bytes) {
object.compose(other)?;
2022-11-07 12:22:08 +00:00
}
2022-11-02 09:15:27 +00:00
}
Ok(Bytes::from(object.to_bytes()))
2022-11-02 02:23:54 +00:00
}
}
#[derive(Serialize, Deserialize)]
pub struct InvalidRevisionObject {
data: String,
}
impl InvalidRevisionObject {
2022-11-07 12:22:08 +00:00
pub fn new() -> Self {
InvalidRevisionObject { data: "".to_string() }
}
2022-11-07 12:22:08 +00:00
pub(crate) fn to_bytes(&self) -> Vec<u8> {
serde_json::to_vec(self).unwrap()
}
2022-11-07 12:22:08 +00:00
// fn from_bytes(bytes: &[u8]) -> Self {
// serde_json::from_slice(bytes).unwrap()
// }
}
2022-11-02 09:15:27 +00:00
#[derive(Serialize, Deserialize)]
pub struct RevisionObjectMock {
content: String,
}
impl RevisionObjectMock {
pub fn new(s: &str) -> Self {
Self { content: s.to_owned() }
}
2022-11-02 02:23:54 +00:00
pub fn compose(&mut self, other: RevisionObjectMock) -> FlowyResult<()> {
2022-11-02 09:15:27 +00:00
self.content.push_str(other.content.as_str());
Ok(())
2022-11-02 09:15:27 +00:00
}
pub fn to_bytes(&self) -> Vec<u8> {
serde_json::to_vec(self).unwrap()
}
2022-11-07 12:22:08 +00:00
pub fn from_bytes(bytes: &[u8]) -> FlowyResult<Self> {
serde_json::from_slice(bytes).map_err(internal_error)
2022-11-02 09:15:27 +00:00
}
}
pub struct RevisionObjectMockSerde();
impl RevisionObjectDeserializer for RevisionObjectMockSerde {
type Output = RevisionObjectMock;
2022-11-07 12:22:08 +00:00
fn deserialize_revisions(_object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output> {
let mut object = RevisionObjectMock::new("");
if revisions.is_empty() {
return Ok(object);
}
for revision in revisions {
2022-11-07 12:22:08 +00:00
if let Ok(revision_object) = RevisionObjectMock::from_bytes(&revision.bytes) {
object.compose(revision_object)?;
2022-11-07 12:22:08 +00:00
}
}
Ok(object)
}
}