Add tests for CompressedCRDTOperation (#2189)

This commit is contained in:
Brendan Allan 2024-03-12 08:39:57 +08:00 committed by GitHub
parent a5f74b19c2
commit d1fa6af7be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 182 additions and 27 deletions

View file

@ -23,13 +23,12 @@ repository = "https://github.com/spacedriveapp/spacedrive"
[workspace.dependencies]
# First party dependencies
prisma-client-rust = { git = "https://github.com/spacedriveapp/prisma-client-rust", rev = "f99d6f5566570f3ab1edecb7a172ad25b03d95af", features = [
"rspc",
"sqlite-create-many",
"migrations",
"sqlite",
], default-features = false }
prisma-client-rust-cli = { git = "https://github.com/spacedriveapp/prisma-client-rust", rev = "f99d6f5566570f3ab1edecb7a172ad25b03d95af", features = [
"rspc",
"specta",
"sqlite-create-many",
"migrations",
"sqlite",

View file

@ -25,15 +25,15 @@ sd-core-sync = { path = "./crates/sync" }
# sd-cloud-api = { path = "../crates/cloud-api" }
sd-file-path-helper = { path = "../crates/file-path-helper" }
sd-crypto = { path = "../crates/crypto", features = [
"sys",
"tokio",
"sys",
"tokio",
], optional = true }
sd-ffmpeg = { path = "../crates/ffmpeg", optional = true }
sd-file-ext = { path = "../crates/file-ext" }
sd-images = { path = "../crates/images", features = [
"rspc",
"serde",
"specta",
"rspc",
"serde",
"specta",
] }
sd-media-metadata = { path = "../crates/media-metadata" }
sd-p2p2 = { path = "../crates/p2p2", features = ["specta"] }
@ -59,17 +59,17 @@ image = { workspace = true }
normpath = { workspace = true, features = ["localization"] }
once_cell = { workspace = true }
pin-project-lite = { workspace = true }
prisma-client-rust = { workspace = true }
prisma-client-rust = { workspace = true, features = ["rspc"] }
regex = { workspace = true }
reqwest = { workspace = true, features = ["json", "native-tls-vendored"] }
rmp-serde = { workspace = true }
rspc = { workspace = true, features = [
"axum",
"uuid",
"chrono",
"tracing",
"alpha",
"unstable",
"axum",
"uuid",
"chrono",
"tracing",
"alpha",
"unstable",
] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
@ -79,12 +79,12 @@ strum_macros = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = [
"sync",
"rt-multi-thread",
"io-util",
"macros",
"time",
"process",
"sync",
"rt-multi-thread",
"io-util",
"macros",
"time",
"process",
] }
tokio-stream = { workspace = true, features = ["fs"] }
tokio-util = { workspace = true, features = ["io"] }
@ -111,7 +111,7 @@ itertools = "0.12.0"
libc = "0.2.153"
mini-moka = "0.10.2"
notify = { git = "https://github.com/notify-rs/notify.git", rev = "c3929ed114fbb0bc7457a9a498260461596b00ca", default-features = false, features = [
"macos_fsevent",
"macos_fsevent",
] }
rmpv = { workspace = true }
serde-hashkey = "0.4.5"
@ -144,10 +144,10 @@ plist = "1"
[target.'cfg(target_os = "ios")'.dependencies]
icrate = { version = "0.1.0", features = [
"Foundation",
"Foundation_NSFileManager",
"Foundation_NSString",
"Foundation_NSNumber",
"Foundation",
"Foundation_NSFileManager",
"Foundation_NSString",
"Foundation_NSNumber",
] }
[dev-dependencies]

View file

@ -12,6 +12,6 @@ rmp-serde = "1.1.2"
rmpv = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
specta = { workspace = true, features = ["uuid", "uhlc"] }
specta = { workspace = true, features = ["uuid", "uhlc", "serde_json"] }
uhlc = { workspace = true }
uuid = { workspace = true, features = ["serde", "v4"] }

View file

@ -8,7 +8,9 @@ pub type CompressedCRDTOperationsForModel = Vec<(rmpv::Value, Vec<CompressedCRDT
/// Stores a bunch of CRDTOperations in a more memory-efficient form for sending to the cloud.
#[derive(Serialize, Deserialize)]
pub struct CompressedCRDTOperations(Vec<(Uuid, Vec<(String, CompressedCRDTOperationsForModel)>)>);
pub struct CompressedCRDTOperations(
pub(self) Vec<(Uuid, Vec<(String, CompressedCRDTOperationsForModel)>)>,
);
impl CompressedCRDTOperations {
pub fn new(ops: Vec<CRDTOperation>) -> Self {
@ -109,3 +111,157 @@ impl From<CRDTOperation> for CompressedCRDTOperation {
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn compress() {
let instance = Uuid::new_v4();
let uncompressed = vec![
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "FilePath".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "FilePath".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "FilePath".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "Object".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "Object".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "FilePath".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
CRDTOperation {
instance,
timestamp: NTP64(0),
id: Uuid::new_v4(),
model: "FilePath".to_string(),
record_id: rmpv::Value::Nil,
data: CRDTOperationData::Create,
},
];
let CompressedCRDTOperations(compressed) = CompressedCRDTOperations::new(uncompressed);
assert_eq!(&compressed[0].1[0].0, "FilePath");
assert_eq!(&compressed[0].1[1].0, "Object");
assert_eq!(&compressed[0].1[2].0, "FilePath");
assert_eq!(compressed[0].1[0].1[0].1.len(), 3);
assert_eq!(compressed[0].1[1].1[0].1.len(), 2);
assert_eq!(compressed[0].1[2].1[0].1.len(), 2);
}
#[test]
fn into_ops() {
let compressed = CompressedCRDTOperations(vec![(
Uuid::new_v4(),
vec![
(
"FilePath".to_string(),
vec![(
rmpv::Value::Nil,
vec![
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
],
)],
),
(
"Object".to_string(),
vec![(
rmpv::Value::Nil,
vec![
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
],
)],
),
(
"FilePath".to_string(),
vec![(
rmpv::Value::Nil,
vec![
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
CompressedCRDTOperation {
id: Uuid::new_v4(),
timestamp: NTP64(0),
data: CRDTOperationData::Create,
},
],
)],
),
],
)]);
let uncompressed = compressed.into_ops();
assert_eq!(uncompressed.len(), 7);
assert_eq!(uncompressed[2].model, "FilePath");
assert_eq!(uncompressed[4].model, "Object");
assert_eq!(uncompressed[6].model, "FilePath");
}
}