From 29b1e8ce589976ae81985cdc2fe0cf31115be07e Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Sat, 17 Sep 2022 12:37:49 -0500 Subject: [PATCH] refactor: move common test code into module --- tests/common/mod.rs | 29 +++++++++++++++++++++++++++++ tests/integration_test.rs | 32 ++------------------------------ 2 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 tests/common/mod.rs diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..03e8a5d --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,29 @@ +use log::*; +use nostr_rs_relay::config; +use nostr_rs_relay::server::start_server; +use std::sync::mpsc as syncmpsc; +use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; +use std::thread; +use std::thread::JoinHandle; + +pub struct Relay { + pub handle: JoinHandle<()>, + pub shutdown_tx: MpscSender<()>, +} + +pub fn start_relay(port: u16) -> Relay { + let _ = env_logger::try_init(); + info!("Starting up from main"); + // replace default settings + let mut settings = config::Settings::default(); + settings.database.in_memory = true; + settings.network.port = port; + let (shutdown_tx, shutdown_rx): (MpscSender<()>, MpscReceiver<()>) = syncmpsc::channel(); + let handle = thread::spawn(|| { + let _ = start_server(settings, shutdown_rx); + }); + return Relay { + handle, + shutdown_tx, + }; +} diff --git a/tests/integration_test.rs b/tests/integration_test.rs index dec657b..b77864f 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -1,39 +1,11 @@ -use console_subscriber::ConsoleLayer; -use log::*; -use nostr_rs_relay::config; -use nostr_rs_relay::server::start_server; -use std::sync::mpsc as syncmpsc; -use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; use std::thread; -use std::thread::JoinHandle; use std::time::Duration; -struct Relay { - handle: JoinHandle<()>, - shutdown_tx: MpscSender<()>, -} - -fn start_relay(port: u16) -> Relay { - ConsoleLayer::builder().with_default_env().init(); - let _ = env_logger::try_init(); - info!("Starting up from main"); - // replace default settings - let mut settings = config::Settings::default(); - settings.database.in_memory = true; - settings.network.port = port; - let (shutdown_tx, shutdown_rx): (MpscSender<()>, MpscReceiver<()>) = syncmpsc::channel(); - let handle = thread::spawn(|| { - let _ = start_server(settings, shutdown_rx); - }); - return Relay { - handle, - shutdown_tx, - }; -} +mod common; #[test] fn startup() { - let relay = start_relay(8080); + let relay = common::start_relay(8080); // just make sure we can startup and shut down. // if we send a shutdown message before the server is listening, // we will get a SendError. Keep sending until someone is