Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test-utils): move test utils into its own library #41

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["rust", "relay-client"]
members = ["rust", "relay-client", "test-utils"]

[workspace.package]
edition = "2021"
Expand All @@ -11,6 +11,8 @@ rust-version = "1.82.0" # See rust-versio
[workspace.dependencies]
# first party
orb-relay-messages = { path = "rust" }
orb-relay-client = { path = "relay-client" }
orb-relay-test-utils = { path = "test-utils" }

# third party
async-stream = "0.3.6"
Expand All @@ -30,3 +32,4 @@ derive_more = { version = "0.99" }
tracing = "0.1"
bon = "3.3.2"
secrecy = "0.10.3"
pollster = "0.4.0"
1 change: 1 addition & 0 deletions relay-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rust-version.workspace = true

[dependencies]
orb-relay-messages = { workspace = true, features = ["client"] }
orb-relay-test-utils = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tokio-util.workspace = true
tonic = { workspace = true, features = ["tls-roots"] }
Expand Down
4 changes: 1 addition & 3 deletions relay-client/tests/ask.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/*
* Tests whether a client A can ask a message from client B
*/
mod test_server;

use orb_relay_client::{Amount, Auth, Client, ClientOpts, QoS, SendMessage};
use orb_relay_messages::{
prost_types::Any,
Expand All @@ -11,8 +9,8 @@ use orb_relay_messages::{
ConnectResponse, Entity, RelayPayload,
},
};
use orb_relay_test_utils::{IntoRes, TestServer};
use std::time::Duration;
use test_server::{IntoRes, TestServer};
use tokio::time;

#[tokio::test]
Expand Down
4 changes: 1 addition & 3 deletions relay-client/tests/connect_attempts.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/*
* tests RelayClient attempts to connect over and over again until it reaches maximum determined number of attempts
*/
mod test_server;

use orb_relay_client::{Amount, Auth, Client, ClientOpts};
use orb_relay_messages::relay::{
entity::EntityType, relay_connect_request::Msg, ConnectRequest, ConnectResponse,
};
use orb_relay_test_utils::{IntoRes, TestServer};
use std::time::{Duration, Instant};
use test_server::{IntoRes, TestServer};
use tokio::time;

#[tokio::test]
Expand Down
4 changes: 1 addition & 3 deletions relay-client/tests/e2e_reply.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod test_server;

use orb_relay_client::{Amount, Auth, Client, ClientOpts, QoS, SendMessage};
use orb_relay_messages::relay::{
entity::EntityType, relay_connect_request::Msg, ConnectRequest, ConnectResponse,
};
use orb_relay_test_utils::{IntoRes, TestServer};
use std::time::Duration;
use test_server::{IntoRes, TestServer};
use tokio::task;

struct NoState;
Expand Down
4 changes: 1 addition & 3 deletions relay-client/tests/heartbeat.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod test_server;

use orb_relay_client::{Amount, Auth, Client, ClientOpts};
use orb_relay_messages::relay::{
entity::EntityType, relay_connect_request::Msg, ConnectRequest, ConnectResponse,
};
use orb_relay_test_utils::{IntoRes, TestServer};
use std::time::Duration;
use test_server::{IntoRes, TestServer};
use tokio::time;

#[tokio::test]
Expand Down
4 changes: 1 addition & 3 deletions relay-client/tests/send.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/*
* Tests whether a client A can send a message to client B
*/
mod test_server;

use orb_relay_client::{Amount, Auth, Client, ClientOpts, QoS, SendMessage};
use orb_relay_messages::{
prost_types::Any,
Expand All @@ -11,8 +9,8 @@ use orb_relay_messages::{
ConnectResponse, Entity, RelayPayload,
},
};
use orb_relay_test_utils::{IntoRes, TestServer};
use std::time::Duration;
use test_server::{IntoRes, TestServer};
use tokio::time;

#[tokio::test]
Expand Down
4 changes: 1 addition & 3 deletions relay-client/tests/stops.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod test_server;

use orb_relay_client::{Amount, Auth, Client, ClientOpts, Err, SendMessage};
use orb_relay_messages::relay::{
entity::EntityType, relay_connect_request::Msg, ConnectRequest, ConnectResponse,
};
use orb_relay_test_utils::{IntoRes, TestServer};
use std::time::Duration;
use test_server::{IntoRes, TestServer};
use tokio::time;

#[tokio::test]
Expand Down
20 changes: 20 additions & 0 deletions test-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "orb-relay-test-utils"
version = "0.0.1"
description = """
test-utils for orb-relay-client
"""
edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true

[dependencies]
orb-relay-messages = { workspace = true, features = ["client", "server"] }

async-stream.workspace = true
tokio = { workspace = true, fetures = ["full"] }
tonic = { workspace = true, features = ["tls-roots"] }
tokio-stream.workspace = true
flume.workspace = true
pollster.workspace = true
7 changes: 3 additions & 4 deletions relay-client/tests/test_server.rs → test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ impl ConnectedClients {
clients.insert(key, tx.clone());
}

#[allow(dead_code)] // wtf, this is actually used lol...
pub fn send(&self, payload: RelayPayload) {
let clients = self.clients.clone();
task::spawn(async move {
Expand All @@ -41,7 +40,6 @@ impl ConnectedClients {
});
}

#[allow(dead_code)]
pub fn stop(&self, entity: &Entity) {
let clients = self.clients.clone();
let entity_key = entity_key(entity);
Expand Down Expand Up @@ -151,7 +149,6 @@ where
/// separate tokio task and will be shut down when this struct is dropped.
pub struct TestServer<S> {
addr: SocketAddr,
#[allow(dead_code)] // not dead code but clippy complains eh
state: Arc<Mutex<S>>,
shutdown: flume::Sender<()>,
}
Expand All @@ -170,14 +167,16 @@ where
///
/// The handler receives mutable access to the server state and each incoming request message.
/// It can modify the state and returns an optional response message.
/// One can also send messages to other clients connected to the server through `ConnectedClients`
/// which is also provided through the closure.
///
/// The server's state can be accessed at any time by calling `self.state()`, which returns
/// a MutexGuard containing the state.
///
/// # Example
/// ```ignore
/// let initial_state = 0;
/// let sv = TestServer::new(initial_state, |state, conn_req| {
/// let sv = TestServer::new(initial_state, |state, conn_req, clients| {
/// *state += 1;
/// match conn_req {
/// Msg::ConnectRequest(ConnectRequest { client_id, .. }) => ConnectResponse {
Expand Down