mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
Add test to example rpc extension (#4872)
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -21,6 +21,7 @@ dependencies = [
|
||||
"jsonrpsee",
|
||||
"reth",
|
||||
"reth-transaction-pool",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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")]
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user