diff --git a/services/commtest/tests/backup/pull_backup.rs b/services/commtest/tests/backup/pull_backup.rs index 52fca6107..810e01c04 100644 --- a/services/commtest/tests/backup/pull_backup.rs +++ b/services/commtest/tests/backup/pull_backup.rs @@ -1,126 +1,132 @@ #[path = "./backup_utils.rs"] mod backup_utils; #[path = "../lib/tools.rs"] mod tools; -use tonic::Request; use std::io::{Error as IOError, ErrorKind}; +use tonic::Request; use crate::backup_utils::{ proto::pull_backup_response::Data, proto::pull_backup_response::Data::*, proto::pull_backup_response::Id, proto::pull_backup_response::Id::*, proto::PullBackupRequest, BackupServiceClient, }; use crate::backup_utils::{BackupData, Item}; use crate::tools::{Error, ATTACHMENT_DELIMITER}; #[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_item.id.clone(); let mut result = BackupData { user_id: String::new(), device_id: String::new(), backup_item: Item::new(String::new(), Vec::new(), Vec::new()), log_items: 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; let mut current_id: String = String::new(); while let Some(response) = inbound.message().await? { let response_data: Option = response.data; let id: Option = response.id; let mut backup_id: Option = None; let mut log_id: Option = None; match id { Some(BackupId(id)) => backup_id = Some(id), Some(LogId(id)) => log_id = Some(id), None => {} }; match response_data { Some(CompactionChunk(chunk)) => { assert_eq!( state, State::Compaction, "invalid state, expected compaction, got {:?}", state ); - current_id = backup_id.ok_or(IOError::new(ErrorKind::Other, "backup id expected but not received"))?; + current_id = backup_id.ok_or(IOError::new( + ErrorKind::Other, + "backup id expected but not received", + ))?; println!( "compaction (id {}), pushing chunk (size: {})", current_id, chunk.len() ); result.backup_item.chunks_sizes.push(chunk.len()) } Some(LogChunk(chunk)) => { if state == State::Compaction { state = State::Log; } assert_eq!(state, State::Log, "invalid state, expected compaction"); - let log_id = log_id.ok_or(IOError::new(ErrorKind::Other, "log id expected but not received"))?; + let log_id = log_id.ok_or(IOError::new( + ErrorKind::Other, + "log id expected but not received", + ))?; if log_id != current_id { result.log_items.push(Item::new( log_id.clone(), Vec::new(), Vec::new(), )); current_id = log_id; } let log_items_size = result.log_items.len() - 1; result.log_items[log_items_size] .chunks_sizes .push(chunk.len()); println!("log (id {}) chunk size {}", current_id, chunk.len()); } Some(AttachmentHolders(holders)) => { let holders_split: Vec<&str> = holders.split(ATTACHMENT_DELIMITER).collect(); if state == State::Compaction { println!("attachments for the backup: {}", holders); for holder in holders_split { if holder.len() == 0 { continue; } result .backup_item .attachments_holders .push(holder.to_string()); } } else if state == State::Log { println!("attachments for the log: {}", holders); for holder in holders_split { if holder.len() == 0 { continue; } let log_items_size = result.log_items.len() - 1; result.log_items[log_items_size] .attachments_holders .push(holder.to_string()) } } } None => {} } } Ok(result) } diff --git a/services/commtest/tests/backup_test.rs b/services/commtest/tests/backup_test.rs index 386f96489..dc4ded4bb 100644 --- a/services/commtest/tests/backup_test.rs +++ b/services/commtest/tests/backup_test.rs @@ -1,163 +1,160 @@ #[path = "./backup/add_attachments.rs"] mod add_attachments; #[path = "./backup/backup_utils.rs"] mod backup_utils; #[path = "./backup/create_new_backup.rs"] mod create_new_backup; #[path = "./backup/pull_backup.rs"] mod pull_backup; #[path = "./backup/send_log.rs"] mod send_log; #[path = "./lib/tools.rs"] mod tools; use backup_utils::{BackupData, Item}; use bytesize::ByteSize; use tools::Error; use std::env; use backup_utils::BackupServiceClient; #[tokio::test] async fn backup_test() -> Result<(), Error> { let port = env::var("COMM_SERVICES_PORT_BACKUP") .expect("port env var expected but not received"); let mut client = BackupServiceClient::connect(format!("http://localhost:{}", port)).await?; let attachments_fill_size: u64 = 500; let mut backup_data = BackupData { user_id: "user0000".to_string(), device_id: "device0000".to_string(), backup_item: Item::new( String::new(), vec![ByteSize::mib(1).as_u64() as usize; 6], vec![ "holder0".to_string(), "holder1".to_string(), "holder2".to_string(), ], ), log_items: vec![ // the item that almost hits the DB limit, we're going to later add a long // list of attachments, so that causes it to exceed the limit. // In this case its data should be moved to the S3 Item::new( String::new(), vec![ *tools::DYNAMO_DB_ITEM_SIZE_LIMIT - ByteSize::b(attachments_fill_size / 2).as_u64() as usize, ], vec!["holder0".to_string(), "holder1".to_string()], ), // just a small item Item::new( String::new(), vec![ByteSize::b(100).as_u64() as usize], vec!["holder0".to_string()], ), // a big item that should be placed in the S3 right away Item::new( String::new(), - vec![ - *tools::GRPC_CHUNK_SIZE_LIMIT, - *tools::GRPC_CHUNK_SIZE_LIMIT, - ], + vec![*tools::GRPC_CHUNK_SIZE_LIMIT, *tools::GRPC_CHUNK_SIZE_LIMIT], vec![ "holder0".to_string(), "holder1".to_string(), "holder2".to_string(), ], ), ], }; backup_data.backup_item.id = create_new_backup::run(&mut client, &backup_data).await?; println!("backup id in main: {}", backup_data.backup_item.id); add_attachments::run(&mut client, &backup_data, None).await?; for log_index in 0..backup_data.log_items.len() { backup_data.log_items[log_index].id = send_log::run(&mut client, &backup_data, log_index).await?; add_attachments::run(&mut client, &backup_data, Some(log_index)).await?; } let result = pull_backup::run(&mut client, &backup_data).await?; // check backup size let expected: usize = backup_data.backup_item.chunks_sizes.iter().sum(); let from_result: usize = result.backup_item.chunks_sizes.iter().sum(); assert_eq!( from_result, expected, "backup sizes do not match, expected {}, got {}", expected, from_result ); // check backup attachments let expected: usize = backup_data.backup_item.attachments_holders.len(); let from_result: usize = result.backup_item.attachments_holders.len(); assert_eq!( from_result, expected, "backup: number of attachments holders do not match, expected {}, got {}", expected, from_result ); // check number of logs let expected: usize = backup_data.log_items.len(); let from_result: usize = result.log_items.len(); assert_eq!( expected, from_result, "number of logs do not match, expected {}, got {}", expected, from_result ); // check log sizes for i in 0..backup_data.log_items.len() { let expected: usize = backup_data.log_items[i].chunks_sizes.iter().sum(); let from_result: usize = result.log_items[i].chunks_sizes.iter().sum(); assert_eq!( from_result, expected, "log number {} sizes do not match, expected {}, got {}", i, expected, from_result ); } // push so many attachments that the log item's data will have to be moved // from the db to the s3 let mut attachments_size = 0; let mut i = backup_data.log_items[0].attachments_holders.len(); let mut new_attachments: Vec = Vec::new(); while attachments_size < (attachments_fill_size as usize) { let att = format!("holder{}", i); attachments_size += att.len(); new_attachments.push(att); i += 1; } let mut old_attachments = backup_data.log_items[0].attachments_holders.clone(); backup_data.log_items[0].attachments_holders = new_attachments; add_attachments::run(&mut client, &backup_data, Some(0)).await?; backup_data.log_items[0] .attachments_holders .append(&mut old_attachments); let result = pull_backup::run(&mut client, &backup_data).await?; // check logs attachments for i in 0..backup_data.log_items.len() { let expected: usize = backup_data.log_items[i].attachments_holders.len(); let from_result: usize = result.log_items[i].attachments_holders.len(); assert_eq!( from_result, expected, "after attachment add: log {}: number of attachments holders do not match, expected {}, got {}", i, expected, from_result ); } Ok(()) } diff --git a/services/commtest/tests/lib/tools.rs b/services/commtest/tests/lib/tools.rs index 95d0e7dac..4dabe0917 100644 --- a/services/commtest/tests/lib/tools.rs +++ b/services/commtest/tests/lib/tools.rs @@ -1,33 +1,35 @@ use bytesize::ByteSize; use lazy_static::lazy_static; #[allow(dead_code)] pub fn generate_nbytes( number_of_bytes: usize, predefined_byte_value: Option, ) -> Vec { let byte_value = predefined_byte_value.unwrap_or(b'A'); return vec![byte_value; number_of_bytes]; } #[derive( Debug, derive_more::Display, derive_more::From, derive_more::Error, )] pub enum Error { #[display(...)] Proto(std::io::Error), #[display(...)] Tonic(tonic::transport::Error), #[display(...)] TonicStatus(tonic::Status), } pub const GRPC_METADATA_SIZE_BYTES: usize = 5; lazy_static! { - pub static ref DYNAMO_DB_ITEM_SIZE_LIMIT: usize = ByteSize::kib(400).as_u64() as usize; - pub static ref GRPC_CHUNK_SIZE_LIMIT: usize = (ByteSize::mib(4).as_u64() as usize) - GRPC_METADATA_SIZE_BYTES; + pub static ref DYNAMO_DB_ITEM_SIZE_LIMIT: usize = + ByteSize::kib(400).as_u64() as usize; + pub static ref GRPC_CHUNK_SIZE_LIMIT: usize = + (ByteSize::mib(4).as_u64() as usize) - GRPC_METADATA_SIZE_BYTES; } #[allow(dead_code)] pub const ATTACHMENT_DELIMITER: &str = ";";