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

Add test to example rpc extension #4872

Merged
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
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
}
}
Loading