diff --git a/services/commtest/src/main.rs b/services/commtest/src/main.rs --- a/services/commtest/src/main.rs +++ b/services/commtest/src/main.rs @@ -6,6 +6,7 @@ pub mod backup_utils; mod create_new_backup; +mod pull_backup; mod send_log; pub mod tools; @@ -29,8 +30,8 @@ vec![100], vec![ByteSize::kb(400).as_u64() as usize], vec![ - ByteSize::kb(500).as_u64() as usize, - ByteSize::kb(100).as_u64() as usize, + ByteSize::mb(4).as_u64() as usize, + ByteSize::mb(4).as_u64() as usize, ], ], }; @@ -46,6 +47,51 @@ .map_err(|err| -> String { format!("send log: {}", err) })?; } + let result = pull_backup::run(&mut client, &backup_data) + .await + .map_err(|err| -> String { format!("pull backup: {}", err) })?; + + // check backup size + let expected: usize = backup_data.backup_chunk_sizes.iter().sum(); + let from_result: usize = result.backup_chunk_sizes.iter().sum(); + if from_result != expected { + return Err( + format!( + "backup sizes do not match, expected {}, got {}", + expected, from_result + ) + .into(), + ); + } + + // check number of logs + let expected: usize = backup_data.logs_sizes.len(); + let from_result: usize = result.logs_sizes.len(); + if expected != from_result { + return Err( + format!( + "number of logs do not match, expected {}, got {}", + expected, from_result + ) + .into(), + ); + } + + // check log sizes + for i in 0..backup_data.logs_sizes.len() { + let expected: usize = backup_data.logs_sizes[i].iter().sum(); + let from_result: usize = result.logs_sizes[i].iter().sum(); + if from_result != expected { + return Err( + format!( + "log number {} sizes do not match, expected {}, got {}", + i, expected, from_result + ) + .into(), + ); + } + } + println!("tested"); Ok(()) } diff --git a/services/commtest/src/pull_backup.rs b/services/commtest/src/pull_backup.rs new file mode 100644 --- /dev/null +++ b/services/commtest/src/pull_backup.rs @@ -0,0 +1,70 @@ +use crate::backup_utils::{ + proto::pull_backup_response::Data, proto::pull_backup_response::Data::*, + proto::PullBackupRequest, BackupServiceClient, +}; + +use crate::backup_utils::BackupData; +use tonic::Request; + +#[derive(PartialEq, Debug)] +enum State { + Compaction, + Log, +} + +pub async fn run( + client: &mut BackupServiceClient, + backup_data: &BackupData, +) -> Result> { + println!("pull backup"); + let cloned_user_id = backup_data.user_id.clone(); + let cloned_backup_id = backup_data.backup_id.clone(); + + let mut result = BackupData { + user_id: String::new(), + device_id: String::new(), + backup_id: String::new(), + backup_chunk_sizes: Vec::new(), + logs_sizes: Vec::new(), + }; + + let response = client + .pull_backup(Request::new(PullBackupRequest { + user_id: cloned_user_id, + backup_id: cloned_backup_id, + })) + .await?; + let mut inbound = response.into_inner(); + let mut state: State = State::Compaction; + while let Some(response) = inbound.message().await? { + let r: Option = response.data; + if let Some(CompactionChunk(cc)) = r { + if state != State::Compaction { + return Err( + format!("invalid state, expected compaction, got {:?}", state).into(), + ); + } + println!( + "comp chunk no. {} size {}", + response.chunk_counter, + cc.len() + ); + result.backup_chunk_sizes.push(cc.len()) + } else if let Some(LogChunk(lc)) = r { + if state == State::Compaction { + state = State::Log; + } + if state != State::Log { + return Err("invalid state, expected compaction".into()); + } + if response.chunk_counter == 0 { + result.logs_sizes.push(Vec::new()); + } + let size = result.logs_sizes.len(); + result.logs_sizes[size - 1].push(lc.len()); + + println!("log chunk no. {} size {}", response.chunk_counter, lc.len()); + } + } + Ok(result) +}