Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Feb 23, 2024
1 parent 8ad9421 commit 67c54dc
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

79 changes: 69 additions & 10 deletions lib-xps/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
pub mod rpc;
pub mod types;
#[cfg(test)]
mod util;

use anyhow::Result;
use ethers::{
abi::Address,
providers::{Provider, Ws},
};
use ethers::{abi::Address, providers::Middleware};
use jsonrpsee::{server::Server, RpcModule};
use std::str::FromStr;
use xps_types::{CONVERSATION, DID_ETH_REGISTRY};
Expand All @@ -15,18 +13,16 @@ pub use crate::rpc::{XpsClient, XpsMethods, XpsServer};
use crate::types::GatewayContext;

/// Entrypoint for the xps Gateway
pub async fn run<P: AsRef<str>>(host: String, port: u16, provider: P) -> Result<()> {
crate::util::init_logging();

pub async fn run<P>(host: String, port: u16, provider: P) -> Result<()>
where
P: Middleware + 'static,
{
let server_addr = format!("{}:{}", host, port);

// a port of 0 allows the OS to choose an open port
let server = Server::builder().build(server_addr).await?;
let addr = server.local_addr()?;

let registry_contract = Address::from_str(DID_ETH_REGISTRY)?;
let conversation_contract = Address::from_str(CONVERSATION)?;
let provider = Provider::<Ws>::connect(provider.as_ref()).await.unwrap();

let context = GatewayContext::new(registry_contract, conversation_contract, provider).await?;
let mut methods = RpcModule::new(());
Expand Down Expand Up @@ -58,3 +54,66 @@ fn build_rpc_api<M: Send + Sync + 'static>(mut rpc_api: RpcModule<M>) -> RpcModu

rpc_api
}

#[cfg(test)]
mod tests {
use super::*;
use ethers::{prelude::Provider, types::U64};
use jsonrpsee::{core::client::ClientT, ws_client::WsClientBuilder};

#[tokio::test]
async fn test_run() -> Result<()> {
let (provider, mock) = Provider::mocked();
// chainID
mock.push(U64::from(0x1)).unwrap();
let port = 43594;
let handle = tokio::spawn(async move {
match run("127.0.0.1".to_string(), 43594, provider).await {
Err(e) => log::error!("Error running server: {e}"),
Ok(_) => log::info!("Server Stopped"),
}
});

// give the server some time to start
tokio::time::sleep(std::time::Duration::from_millis(10)).await;

let client = WsClientBuilder::default()
.build(&format!("ws://127.0.0.1:{port}"))
.await?;

#[derive(Debug, serde::Deserialize)]
struct Methods {
methods: Vec<String>,
}

let methods = client
.request::<Methods, Vec<()>>("rpc_methods", vec![])
.await?;

assert_eq!(
methods.methods,
vec![
"rpc_methods",
"xps_balance",
"xps_fetchKeyPackages",
"xps_grantInstallation",
"xps_nonce",
"xps_revokeInstallation",
"xps_sendMessage",
"xps_status",
"xps_walletAddress",
]
);

handle.abort();
Ok(())
}

#[test]
fn test_build_api() {
let methods = RpcModule::new(());
let methods = build_rpc_api(methods);
let methods: Vec<String> = methods.method_names().map(String::from).collect();
assert_eq!(methods, vec!["rpc_methods",]);
}
}
10 changes: 1 addition & 9 deletions lib-xps/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
//! Internal Utility functions for use in crate
#[cfg(test)]
use std::sync::Once;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};

#[cfg(test)]
static INIT: Once = Once::new();

pub(crate) fn init_logging() {
let fmt = fmt::layer().compact();
Registry::default().with(env()).with(fmt).init()
}

#[cfg(test)]
#[ctor::ctor]
fn __init_test_logging() {
INIT.call_once(|| {
Expand All @@ -22,6 +14,6 @@ fn __init_test_logging() {

/// Try to get the logging environment from the `RUST_LOG` environment variable.
/// If it is not set, use the default of `info`.
fn env() -> EnvFilter {
pub fn env() -> EnvFilter {
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"))
}
3 changes: 3 additions & 0 deletions xps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ edition = "2021"
[dependencies]
anyhow.workspace = true
tokio.workspace = true
ethers = { workspace = true, features = ["ws"] }
tracing.workspace = true
tracing-subscriber.workspace = true
lib-xps = { path = "../lib-xps" }
clap = { version = "4.4.18", features = ["derive"] }
16 changes: 15 additions & 1 deletion xps/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use anyhow::Result;
use clap::Parser;
use ethers::providers::{Provider, Ws};
use lib_xps::run;
use tracing_subscriber::{
filter::LevelFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry,
};

#[derive(Parser, Debug)]
#[command(name = "xps", version = "0.1.0", about = "XMTP Postal Service")]
Expand All @@ -19,11 +23,21 @@ struct Args {

#[tokio::main]
async fn main() -> Result<()> {
init_logging();

Check warning on line 26 in xps/src/main.rs

View check run for this annotation

Codecov / codecov/patch

xps/src/main.rs#L26

Added line #L26 was not covered by tests
let args = Args::parse();
crate::run(args.host, args.port, args.endpoint).await?;
let provider = Provider::<Ws>::connect(&args.endpoint).await?;
crate::run(args.host, args.port, provider).await?;

Check warning on line 29 in xps/src/main.rs

View check run for this annotation

Codecov / codecov/patch

xps/src/main.rs#L28-L29

Added lines #L28 - L29 were not covered by tests
Ok(())
}

fn init_logging() {
let fmt = fmt::layer().compact();
let env = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();
Registry::default().with(env).with(fmt).init()
}

Check warning on line 39 in xps/src/main.rs

View check run for this annotation

Codecov / codecov/patch

xps/src/main.rs#L33-L39

Added lines #L33 - L39 were not covered by tests

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 67c54dc

Please sign in to comment.