End-to-end unit test (#90)
* First implementation of create_torrent_file * Test harness for e2e preparing * Saving * Continuing test harness * Continuing test harness * Continuing test harness * All servers are running * Full e2e harness done * Test e2e harness working fine * Remove lints * injecting faults * The e2e test runs continuously * e2e test * Add a test for create_torrent * Nothing * Nothing, just tweaking the numberes * Update tokio, remove custom tempfile shim
This commit is contained in:
parent
5d6ecb8065
commit
2778d46bb3
13 changed files with 634 additions and 20 deletions
66
crates/librqbit/src/tests/test_util.rs
Normal file
66
crates/librqbit/src/tests/test_util.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
use std::{io::Write, path::Path};
|
||||
|
||||
use librqbit_core::Id20;
|
||||
use rand::RngCore;
|
||||
use tempfile::TempDir;
|
||||
|
||||
pub fn create_new_file_with_random_content(path: &Path, mut size: usize) {
|
||||
let mut file = std::fs::OpenOptions::new()
|
||||
.create_new(true)
|
||||
.write(true)
|
||||
.open(path)
|
||||
.unwrap();
|
||||
|
||||
eprintln!("creating temp file {:?}", path);
|
||||
|
||||
const BUF_SIZE: usize = 8192 * 16;
|
||||
let mut rng = rand::rngs::OsRng;
|
||||
let mut write_buf = [0; BUF_SIZE];
|
||||
while size > 0 {
|
||||
rng.fill_bytes(&mut write_buf[..]);
|
||||
let written = file.write(&write_buf[..size.min(BUF_SIZE)]).unwrap();
|
||||
size -= written;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_default_random_dir_with_torrents(
|
||||
num_files: usize,
|
||||
file_size: usize,
|
||||
tempdir_prefix: Option<&str>,
|
||||
) -> TempDir {
|
||||
let dir = TempDir::with_prefix(tempdir_prefix.unwrap_or("rqbit_test")).unwrap();
|
||||
dbg!(dir.path());
|
||||
for f in 0..num_files {
|
||||
create_new_file_with_random_content(&dir.path().join(&format!("{f}.data")), file_size);
|
||||
}
|
||||
dir
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TestPeerMetadata {
|
||||
pub server_id: u8,
|
||||
pub max_random_sleep_ms: u8,
|
||||
}
|
||||
|
||||
impl TestPeerMetadata {
|
||||
pub fn as_peer_id(&self) -> Id20 {
|
||||
let mut peer_id = Id20::default();
|
||||
peer_id.0[0] = self.server_id;
|
||||
peer_id.0[1] = self.max_random_sleep_ms;
|
||||
peer_id
|
||||
}
|
||||
|
||||
pub fn from_peer_id(peer_id: Id20) -> Self {
|
||||
Self {
|
||||
server_id: peer_id.0[0],
|
||||
max_random_sleep_ms: peer_id.0[1],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn disconnect_probability(&self) -> f64 {
|
||||
if self.server_id % 2 == 0 {
|
||||
return 0.05f64;
|
||||
}
|
||||
0f64
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue