integration test structure

This commit is contained in:
17ms 2023-05-01 01:39:58 +03:00
parent 4aa5734b7f
commit db388dc013

View File

@ -1,4 +1,4 @@
use contego::{common::Message, connector::Connector, listener::Listener}; use contego::{common::Message, connector::Connector, crypto, listener::Listener};
use ntest::timeout; use ntest::timeout;
use rand::{distributions::Alphanumeric, thread_rng, Rng}; use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{ use std::{
@ -6,7 +6,7 @@ use std::{
io::{BufWriter, Write}, io::{BufWriter, Write},
net::SocketAddr, net::SocketAddr,
path::PathBuf, path::PathBuf,
sync::Arc, str::FromStr,
thread, thread,
}; };
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -14,53 +14,49 @@ use tokio_test::block_on;
#[test] #[test]
#[timeout(2000)] #[timeout(2000)]
/// Tests communication between GUI and individual handlers by mocking GUI signals. /// Tests communication between UI and individual handlers by mocking signals.
fn filesync_signals() { fn filesync_signals() {
let testdata = vec![ let (testdata, paths) = write_testfiles();
("1.txt", generate_data()),
("2.txt", generate_data()),
("3.txt", generate_data()),
];
let mut paths = Vec::new();
for file in &testdata {
let filepath = String::from("./tests/data/") + file.0;
let mut writer = BufWriter::new(File::create(filepath.clone()).unwrap());
paths.push(filepath);
writer.write_all(file.1.as_bytes()).unwrap();
}
let output_path = PathBuf::from("./tests/output/"); let output_path = PathBuf::from("./tests/output/");
let server_addr = SocketAddr::from(([127, 0, 0, 1], 9191)); let addr = SocketAddr::from(([127, 0, 0, 1], 9191));
let key = crypto::keygen();
let c_key = key.clone();
let (kill_server_tx, server_rx) = mpsc::channel::<Message>(10); let (kstx, srx) = mpsc::channel::<Message>(10);
let (server_tx, mut local_server_rx) = mpsc::channel::<Message>(10); let (stx, mut lsrx) = mpsc::channel::<Message>(10);
let (local_client_tx, client_rx) = mpsc::channel::<Message>(10); let (lctx, crx) = mpsc::channel::<Message>(10);
let (client_tx, mut local_client_rx) = mpsc::channel::<Message>(10); let (ctx, mut lcrx) = mpsc::channel::<Message>(10);
let server_handle = thread::spawn(move || { let server_handle = thread::spawn(move || {
let listener = Listener::new(server_addr, String::from("xyz"), 8192usize); let listener = Listener::new(addr, key, 8192usize).unwrap();
block_on(Arc::new(listener).start(server_tx, server_rx, paths)).unwrap(); block_on(listener.start(stx, srx, paths)).unwrap();
}); });
let server_channel_handle = thread::spawn(move || { let server_channel_handle = thread::spawn(move || {
block_on(local_server_rx.recv()).unwrap(); // ClientConnect block_on(lsrx.recv()).unwrap(); // ClientConnect
block_on(local_server_rx.recv()).unwrap(); // ConnectionReady block_on(lsrx.recv()).unwrap(); // ConnectionReady
block_on(local_server_rx.recv()).unwrap(); // ClientDisconnect block_on(lsrx.recv()).unwrap(); // ClientDisconnect
block_on(kill_server_tx.send(Message::Shutdown)).unwrap(); block_on(kstx.send(Message::Shutdown)).unwrap();
}); });
let client_handle = thread::spawn(move || { let client_handle = thread::spawn(move || {
let output_path = output_path.clone(); let output_path = output_path.clone();
let connector = Connector::new(server_addr, String::from("xyz"), output_path); let connector = Connector::new(addr, c_key, output_path);
block_on(connector.connect(client_tx, client_rx)).unwrap() block_on(connector.connect(ctx, crx)).unwrap()
}); });
let client_channel_handle = thread::spawn(move || { let client_channel_handle = thread::spawn(move || {
block_on(local_client_rx.recv()).unwrap(); // Metadata(HashMap) let metadata = block_on(lcrx.recv()).unwrap(); // Metadata(HashMap)
block_on(local_client_tx.send(Message::ClientReqAll)).unwrap();
block_on(local_client_tx.send(Message::Shutdown)).unwrap(); if let Message::Metadata(inner) = metadata {
assert_eq!(inner.len(), 3);
for (filename, _) in inner {
block_on(lctx.send(Message::ClientReq(filename))).unwrap();
}
}
block_on(lctx.send(Message::Shutdown)).unwrap();
}); });
client_handle.join().unwrap(); client_handle.join().unwrap();
@ -74,6 +70,24 @@ fn filesync_signals() {
} }
} }
fn write_testfiles() -> (Vec<(&'static str, String)>, Vec<PathBuf>) {
let mut paths = Vec::new();
let testdata = vec![
("1.txt", generate_data()),
("2.txt", generate_data()),
("3.txt", generate_data()),
];
for file in &testdata {
let filepath = PathBuf::from_str("./tests/data/").unwrap().join(file.0);
let mut writer = BufWriter::new(File::create(filepath.clone()).unwrap());
paths.push(filepath);
writer.write_all(file.1.as_bytes()).unwrap();
}
(testdata, paths)
}
fn generate_data() -> String { fn generate_data() -> String {
thread_rng() thread_rng()
.sample_iter(&Alphanumeric) .sample_iter(&Alphanumeric)