♻️ refactor: extract TestingServer
parent
221732a616
commit
8b845b55da
68
src/tests.rs
68
src/tests.rs
|
@ -37,15 +37,27 @@ struct TestingConfig {
|
|||
server_name: &'static str,
|
||||
api_key: &'static str,
|
||||
|
||||
proxy_port: u16,
|
||||
relay_port: u16,
|
||||
}
|
||||
|
||||
impl TestingConfig {
|
||||
fn relay_url (&self) -> String {
|
||||
format! ("http://127.0.0.1:{}", self.proxy_port)
|
||||
}
|
||||
}
|
||||
|
||||
struct TestingRelay {
|
||||
state: Arc <ptth_relay::RelayState>,
|
||||
task: tokio::task::JoinHandle <Result <(), ptth_relay::RelayError>>,
|
||||
stop_tx: oneshot::Sender <()>,
|
||||
}
|
||||
|
||||
struct TestingServer {
|
||||
task: tokio::task::JoinHandle <Result <(), ptth_server::errors::ServerError>>,
|
||||
stop_tx: oneshot::Sender <()>,
|
||||
}
|
||||
|
||||
impl TestingRelay {
|
||||
async fn new (testing_config: &TestingConfig) -> Self {
|
||||
use ptth_relay::*;
|
||||
|
@ -69,14 +81,14 @@ impl TestingRelay {
|
|||
|
||||
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 state = state.clone ();
|
||||
async move {
|
||||
run_relay (
|
||||
state,
|
||||
Arc::new (load_templates (&PathBuf::new ())?),
|
||||
stop_relay_rx,
|
||||
stop_rx,
|
||||
None
|
||||
).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]
|
||||
fn end_to_end () {
|
||||
use ptth_relay::key_validity::BlakeHashWrapper;
|
||||
|
@ -232,8 +273,11 @@ fn debug_proxy () {
|
|||
let testing_config = TestingConfig {
|
||||
server_name,
|
||||
api_key,
|
||||
|
||||
proxy_port,
|
||||
relay_port,
|
||||
};
|
||||
let relay_url = testing_config.relay_url ();
|
||||
|
||||
let testing_relay = TestingRelay::new (&testing_config).await;
|
||||
|
||||
|
@ -246,21 +290,7 @@ fn debug_proxy () {
|
|||
|
||||
// Start server
|
||||
|
||||
let relay_url = format! ("http://127.0.0.1:{}", proxy_port);
|
||||
|
||||
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
|
||||
})
|
||||
};
|
||||
let testing_server = TestingServer::new (&testing_config).await;
|
||||
|
||||
wait_for_any_server (&testing_relay.state).await;
|
||||
|
||||
|
@ -304,9 +334,7 @@ fn debug_proxy () {
|
|||
|
||||
info! ("Shutting down end-to-end test");
|
||||
|
||||
stop_server_tx.send (()).expect ("Couldn't shut down server");
|
||||
task_server.await.expect ("Couldn't join server").expect ("Server error");
|
||||
info! ("Server stopped");
|
||||
testing_server.graceful_shutdown ().await;
|
||||
|
||||
stop_proxy_tx.send (()).expect ("Couldn't shut down proxy");
|
||||
task_proxy.await.expect ("Couldn't join proxy").expect ("Proxy error");
|
||||
|
|
Loading…
Reference in New Issue