From 80e1ca79bcc84774463788c62773e14b664e08ae Mon Sep 17 00:00:00 2001 From: jbesraa Date: Wed, 4 Sep 2024 13:32:23 +0300 Subject: [PATCH] Add integration test for `PoolSv2` --- .github/workflows/test.yaml | 4 ++ roles/Cargo.lock | 1 + roles/tests-integration/Cargo.toml | 1 + roles/tests-integration/tests/common/mod.rs | 2 +- .../tests/pool_integration.rs | 66 +++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 roles/tests-integration/tests/pool_integration.rs diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5b669ebc0b..a2f63deb48 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -40,6 +40,10 @@ jobs: cargo build --manifest-path=roles/Cargo.toml cargo build --manifest-path=utils/Cargo.toml + - name: Roles Integration Tests + run: | + cargo test --manifest-path=roles/Cargo.toml --verbose --test '*' -- --nocapture + - name: Run sv1-client-and-server example run: | cargo run --manifest-path=examples/sv1-client-and-server/Cargo.toml --bin client_and_server -- 60 diff --git a/roles/Cargo.lock b/roles/Cargo.lock index abd477e217..2415c32c52 100644 --- a/roles/Cargo.lock +++ b/roles/Cargo.lock @@ -1358,6 +1358,7 @@ dependencies = [ "minreq", "pool_sv2", "tar", + "tokio", ] [[package]] diff --git a/roles/tests-integration/Cargo.toml b/roles/tests-integration/Cargo.toml index 279f8b133c..4f04e9906f 100644 --- a/roles/tests-integration/Cargo.toml +++ b/roles/tests-integration/Cargo.toml @@ -10,6 +10,7 @@ minreq = { version = "2.12.0", features = ["https"] } tar = "0.4.41" pool_sv2 = { version = "0.1.0", path = "../pool" } key-utils = { version = "1.0.0", path = "../../utils/key-utils" } +tokio = { version = "1.0.1", features = ["full"] } [lib] path = "tests/common/mod.rs" diff --git a/roles/tests-integration/tests/common/mod.rs b/roles/tests-integration/tests/common/mod.rs index 3fbaf25420..dccf0ff23d 100644 --- a/roles/tests-integration/tests/common/mod.rs +++ b/roles/tests-integration/tests/common/mod.rs @@ -6,7 +6,7 @@ use std::{ env, fs::{create_dir_all, File}, io::{BufReader, Read}, - net::TcpListener, + net::{SocketAddr, TcpListener}, path::{Path, PathBuf}, }; use tar::Archive; diff --git a/roles/tests-integration/tests/pool_integration.rs b/roles/tests-integration/tests/pool_integration.rs new file mode 100644 index 0000000000..7c80cc8672 --- /dev/null +++ b/roles/tests-integration/tests/pool_integration.rs @@ -0,0 +1,66 @@ +use std::{net::SocketAddr, str::FromStr, time::Duration}; + +use tokio::time::sleep; + +use crate::common::{get_available_port, is_port_open}; + +mod common; + +#[tokio::test] +async fn success_pool_template_provider_connection() { + let template_provider_port = get_available_port().unwrap(); + let template_provider = common::TemplateProvider::start(template_provider_port); + template_provider.generate_blocks(16); + let pool_port = get_available_port().unwrap(); + let pool_listening_address = SocketAddr::from_str(&format!("127.0.0.1:{}", pool_port)).unwrap(); + let is_pool_port_open = is_port_open(pool_listening_address); + assert_eq!(is_pool_port_open, false); + let pool = common::TestPoolSv2::new( + pool_listening_address, + None, + Some(SocketAddr::from_str(&format!("127.0.0.1:{}", template_provider_port)).unwrap()), + ); + let state = pool.state().await.safe_lock(|s| s.clone()).unwrap(); + assert_eq!(state, pool_sv2::PoolState::Initial); + let _pool = pool.clone(); + tokio::task::spawn(async move { + let _ = _pool.start().await; + }); + // Wait for the pool to start. + sleep(Duration::from_secs(1)).await; + loop { + if is_port_open(pool_listening_address) { + break; + } + } + let state = pool.state().await.safe_lock(|s| s.clone()).unwrap(); + assert_eq!(state, pool_sv2::PoolState::Running); + template_provider.stop(); +} + +#[tokio::test] +async fn pool_bad_coinbase_output() { + let template_provider_port = get_available_port().unwrap(); + let template_provider = common::TemplateProvider::start(template_provider_port); + template_provider.generate_blocks(16); + let pool_port = get_available_port().unwrap(); + let pool_listening_address = SocketAddr::from_str(&format!("127.0.0.1:{}", pool_port)).unwrap(); + let is_pool_port_open = is_port_open(pool_listening_address); + assert_eq!(is_pool_port_open, false); + let coinbase_output = vec![pool_sv2::mining_pool::CoinbaseOutput::new( + "P2PK".to_string(), + "04466d7fcae563e5cb09a0d1870bb580344804617879a14949cf22285f1bae3f276728176c3c6431f8eeda4538dc37c865e2784f3a9e77d044f33e407797e1278".to_string(), + )]; + let pool = common::TestPoolSv2::new( + pool_listening_address, + Some(coinbase_output), + Some(SocketAddr::from_str(&format!("127.0.0.1:{}", template_provider_port)).unwrap()), + ) + .clone(); + let state = pool.state().await.safe_lock(|s| s.clone()).unwrap(); + assert_eq!(state, pool_sv2::PoolState::Initial); + assert!(pool.start().await.is_err()); + let state = pool.state().await.safe_lock(|s| s.clone()).unwrap(); + assert_eq!(state, pool_sv2::PoolState::Initial); + template_provider.stop(); +}