Skip to content

Commit

Permalink
Add test to example rpc extension (paradigmxyz#4872)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoopmann authored Oct 2, 2023
1 parent b273eeb commit 3bd6079
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions examples/additional-rpc-namespace-in-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ reth-transaction-pool.workspace = true
clap = { version = "4", features = ["derive"] }
jsonrpsee = { workspace = true, features = ["server", "macros"] }
eyre = "0.6"

[dev-dependencies]
tokio.workspace = true
31 changes: 30 additions & 1 deletion examples/additional-rpc-namespace-in-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ impl RethNodeCommandConfig for RethCliTxpoolExt {
/// trait interface for a custom rpc namespace: `txpool`
///
/// This defines an additional namespace where all methods are configured as trait functions.
#[rpc(server, namespace = "txpoolExt")]
#[cfg_attr(not(test), rpc(server, namespace = "txpoolExt"))]
#[cfg_attr(test, rpc(server, client, namespace = "txpoolExt"))]
pub trait TxpoolExtApi {
/// Returns the number of transactions in the pool.
#[method(name = "transactionCount")]
Expand All @@ -111,3 +112,31 @@ where
Ok(self.pool.pool_size().total)
}
}

#[cfg(test)]
mod tests {
use super::*;
use jsonrpsee::{http_client::HttpClientBuilder, server::ServerBuilder};
use reth_transaction_pool::noop::NoopTransactionPool;

#[tokio::test(flavor = "multi_thread")]
async fn test_call_transaction_count_http() {
let server_addr = start_server().await;
let uri = format!("http://{}", server_addr);
let client = HttpClientBuilder::default().build(&uri).unwrap();
let count = TxpoolExtApiClient::transaction_count(&client).await.unwrap();
assert_eq!(count, 0);
}

async fn start_server() -> std::net::SocketAddr {
let server = ServerBuilder::default().build("127.0.0.1:0").await.unwrap();
let addr = server.local_addr().unwrap();
let pool = NoopTransactionPool::default();
let api = TxpoolExt { pool };
let server_handle = server.start(api.into_rpc());

tokio::spawn(server_handle.stopped());

addr
}
}

0 comments on commit 3bd6079

Please sign in to comment.