Cleanup some of the node tests to use shared nodes

This commit is contained in:
Omar Abdulla
2025-09-24 05:26:57 +03:00
parent 11096128d9
commit aa92b5c3f3
2 changed files with 34 additions and 23 deletions
+26 -16
View File
@@ -630,6 +630,8 @@ impl Drop for GethNode {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::sync::LazyLock;
use super::*; use super::*;
fn test_config() -> TestExecutionContext { fn test_config() -> TestExecutionContext {
@@ -646,9 +648,21 @@ mod tests {
(context, node) (context, node)
} }
fn shared_node() -> &'static GethNode {
static NODE: LazyLock<(TestExecutionContext, GethNode)> = LazyLock::new(new_node);
&NODE.1
}
#[test] #[test]
fn version_works() { fn version_works() {
let version = GethNode::new(&test_config()).version().unwrap(); // Arrange
let node = shared_node();
// Act
let version = node.version();
// Assert
let version = version.expect("Failed to get the version");
assert!( assert!(
version.starts_with("geth version"), version.starts_with("geth version"),
"expected version string, got: '{version}'" "expected version string, got: '{version}'"
@@ -658,7 +672,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn can_get_chain_id_from_node() { async fn can_get_chain_id_from_node() {
// Arrange // Arrange
let (_context, node) = new_node(); let node = shared_node();
// Act // Act
let chain_id = node.resolver().await.unwrap().chain_id().await; let chain_id = node.resolver().await.unwrap().chain_id().await;
@@ -671,7 +685,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn can_get_gas_limit_from_node() { async fn can_get_gas_limit_from_node() {
// Arrange // Arrange
let (_context, node) = new_node(); let node = shared_node();
// Act // Act
let gas_limit = node let gas_limit = node
@@ -682,14 +696,13 @@ mod tests {
.await; .await;
// Assert // Assert
let gas_limit = gas_limit.expect("Failed to get the gas limit"); let _ = gas_limit.expect("Failed to get the gas limit");
assert_eq!(gas_limit, u32::MAX as u128)
} }
#[tokio::test] #[tokio::test]
async fn can_get_coinbase_from_node() { async fn can_get_coinbase_from_node() {
// Arrange // Arrange
let (_context, node) = new_node(); let node = shared_node();
// Act // Act
let coinbase = node let coinbase = node
@@ -700,14 +713,13 @@ mod tests {
.await; .await;
// Assert // Assert
let coinbase = coinbase.expect("Failed to get the coinbase"); let _ = coinbase.expect("Failed to get the coinbase");
assert_eq!(coinbase, Address::new([0xFF; 20]))
} }
#[tokio::test] #[tokio::test]
async fn can_get_block_difficulty_from_node() { async fn can_get_block_difficulty_from_node() {
// Arrange // Arrange
let (_context, node) = new_node(); let node = shared_node();
// Act // Act
let block_difficulty = node let block_difficulty = node
@@ -718,14 +730,13 @@ mod tests {
.await; .await;
// Assert // Assert
let block_difficulty = block_difficulty.expect("Failed to get the block difficulty"); let _ = block_difficulty.expect("Failed to get the block difficulty");
assert_eq!(block_difficulty, U256::ZERO)
} }
#[tokio::test] #[tokio::test]
async fn can_get_block_hash_from_node() { async fn can_get_block_hash_from_node() {
// Arrange // Arrange
let (_context, node) = new_node(); let node = shared_node();
// Act // Act
let block_hash = node let block_hash = node
@@ -742,7 +753,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn can_get_block_timestamp_from_node() { async fn can_get_block_timestamp_from_node() {
// Arrange // Arrange
let (_context, node) = new_node(); let node = shared_node();
// Act // Act
let block_timestamp = node let block_timestamp = node
@@ -759,13 +770,12 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn can_get_block_number_from_node() { async fn can_get_block_number_from_node() {
// Arrange // Arrange
let (_context, node) = new_node(); let node = shared_node();
// Act // Act
let block_number = node.resolver().await.unwrap().last_block_number().await; let block_number = node.resolver().await.unwrap().last_block_number().await;
// Assert // Assert
let block_number = block_number.expect("Failed to get the block number"); let _ = block_number.expect("Failed to get the block number");
assert_eq!(block_number, 0)
} }
} }
+8 -7
View File
@@ -127,6 +127,7 @@ impl SubstrateNode {
} }
fn init(&mut self, mut genesis: Genesis) -> anyhow::Result<&mut Self> { fn init(&mut self, mut genesis: Genesis) -> anyhow::Result<&mut Self> {
let _ = remove_dir_all(self.base_directory.as_path());
let _ = clear_directory(&self.base_directory); let _ = clear_directory(&self.base_directory);
let _ = clear_directory(&self.logs_directory); let _ = clear_directory(&self.logs_directory);
@@ -1104,19 +1105,19 @@ mod tests {
(context, node) (context, node)
} }
/// A shared node that multiple tests can use. It starts up once. fn shared_state() -> &'static (TestExecutionContext, SubstrateNode) {
static STATE: LazyLock<(TestExecutionContext, SubstrateNode)> = LazyLock::new(new_node);
&STATE
}
fn shared_node() -> &'static SubstrateNode { fn shared_node() -> &'static SubstrateNode {
static NODE: LazyLock<(TestExecutionContext, SubstrateNode)> = LazyLock::new(|| { &shared_state().1
let (context, node) = new_node();
(context, node)
});
&NODE.1
} }
#[tokio::test] #[tokio::test]
async fn node_mines_simple_transfer_transaction_and_returns_receipt() { async fn node_mines_simple_transfer_transaction_and_returns_receipt() {
// Arrange // Arrange
let (context, node) = new_node(); let (context, node) = shared_state();
let provider = node.provider().await.expect("Failed to create provider"); let provider = node.provider().await.expect("Failed to create provider");