♻️ refactor: extract TestingServer

main
_ 2021-03-06 19:25:12 +00:00
parent 221732a616
commit 8b845b55da
1 changed files with 48 additions and 20 deletions

View File

@ -37,15 +37,27 @@ struct TestingConfig {
server_name: &'static str, server_name: &'static str,
api_key: &'static str, api_key: &'static str,
proxy_port: u16,
relay_port: u16, relay_port: u16,
} }
impl TestingConfig {
fn relay_url (&self) -> String {
format! ("http://127.0.0.1:{}", self.proxy_port)
}
}
struct TestingRelay { struct TestingRelay {
state: Arc <ptth_relay::RelayState>, state: Arc <ptth_relay::RelayState>,
task: tokio::task::JoinHandle <Result <(), ptth_relay::RelayError>>, task: tokio::task::JoinHandle <Result <(), ptth_relay::RelayError>>,
stop_tx: oneshot::Sender <()>, stop_tx: oneshot::Sender <()>,
} }
struct TestingServer {
task: tokio::task::JoinHandle <Result <(), ptth_server::errors::ServerError>>,
stop_tx: oneshot::Sender <()>,
}
impl TestingRelay { impl TestingRelay {
async fn new (testing_config: &TestingConfig) -> Self { async fn new (testing_config: &TestingConfig) -> Self {
use ptth_relay::*; use ptth_relay::*;
@ -69,14 +81,14 @@ impl TestingRelay {
let state = Arc::new (RelayState::try_from (cfg).expect ("Can't create relay state")); let state = Arc::new (RelayState::try_from (cfg).expect ("Can't create relay state"));
let (stop_tx, stop_relay_rx) = oneshot::channel (); let (stop_tx, stop_rx) = oneshot::channel ();
let task = spawn ({ let task = spawn ({
let state = state.clone (); let state = state.clone ();
async move { async move {
run_relay ( run_relay (
state, state,
Arc::new (load_templates (&PathBuf::new ())?), Arc::new (load_templates (&PathBuf::new ())?),
stop_relay_rx, stop_rx,
None None
).await ).await
} }
@ -98,6 +110,35 @@ impl TestingRelay {
} }
} }
impl TestingServer {
async fn new (testing_config: &TestingConfig) -> Self {
let config_file = ptth_server::ConfigFile {
name: testing_config.server_name.into (),
api_key: testing_config.api_key.into (),
relay_url: format! ("{}/7ZSFUKGV", testing_config.relay_url ()),
file_server_root: None,
};
let (stop_tx, stop_rx) = oneshot::channel ();
let task = {
spawn (async move {
ptth_server::run_server (config_file, stop_rx, None, None).await
})
};
Self {
task,
stop_tx,
}
}
async fn graceful_shutdown (self) {
self.stop_tx.send (()).expect ("Couldn't shut down server");
self.task.await.expect ("Couldn't join server").expect ("Server error");
info! ("Server stopped");
}
}
#[test] #[test]
fn end_to_end () { fn end_to_end () {
use ptth_relay::key_validity::BlakeHashWrapper; use ptth_relay::key_validity::BlakeHashWrapper;
@ -232,8 +273,11 @@ fn debug_proxy () {
let testing_config = TestingConfig { let testing_config = TestingConfig {
server_name, server_name,
api_key, api_key,
proxy_port,
relay_port, relay_port,
}; };
let relay_url = testing_config.relay_url ();
let testing_relay = TestingRelay::new (&testing_config).await; let testing_relay = TestingRelay::new (&testing_config).await;
@ -246,21 +290,7 @@ fn debug_proxy () {
// Start server // Start server
let relay_url = format! ("http://127.0.0.1:{}", proxy_port); let testing_server = TestingServer::new (&testing_config).await;
let config_file = ptth_server::ConfigFile {
name: server_name.into (),
api_key: api_key.into (),
relay_url: format! ("{}/7ZSFUKGV", relay_url),
file_server_root: None,
};
let (stop_server_tx, stop_server_rx) = oneshot::channel ();
let task_server = {
spawn (async move {
ptth_server::run_server (config_file, stop_server_rx, None, None).await
})
};
wait_for_any_server (&testing_relay.state).await; wait_for_any_server (&testing_relay.state).await;
@ -304,9 +334,7 @@ fn debug_proxy () {
info! ("Shutting down end-to-end test"); info! ("Shutting down end-to-end test");
stop_server_tx.send (()).expect ("Couldn't shut down server"); testing_server.graceful_shutdown ().await;
task_server.await.expect ("Couldn't join server").expect ("Server error");
info! ("Server stopped");
stop_proxy_tx.send (()).expect ("Couldn't shut down proxy"); stop_proxy_tx.send (()).expect ("Couldn't shut down proxy");
task_proxy.await.expect ("Couldn't join proxy").expect ("Proxy error"); task_proxy.await.expect ("Couldn't join proxy").expect ("Proxy error");