Skip to content

Commit

Permalink
remove repeated code
Browse files Browse the repository at this point in the history
  • Loading branch information
ManojJiSharma committed Sep 20, 2024
1 parent 680d651 commit 783a32c
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 96 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"chains/polygon/rosetta-testing-polygon",
"chains/binance",
"chains/avalanche",
"chains/utils",
]
resolver = "2"

Expand Down
1 change: 1 addition & 0 deletions chains/avalanche/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sha3 = "0.10"
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
tracing = "0.1.40"
url = "2.4"
utils = { path = "../utils" }

[dev-dependencies]
serial_test = "3.1.1"
33 changes: 1 addition & 32 deletions chains/avalanche/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ mod tests {
use serial_test::serial;
use sha3::Digest;
use std::{collections::BTreeMap, future::Future, path::Path};
use utils::run_test;

/// Account used to fund other testing accounts.
const FUNDING_ACCOUNT_PRIVATE_KEY: [u8; 32] =
Expand All @@ -68,38 +69,6 @@ mod tests {
}
}

/// Run the test in another thread while sending txs to force binance to mine new blocks
/// # Panic
/// Panics if the future panics
async fn run_test<Fut: Future<Output = ()> + Send + 'static>(future: Fut) {
// Guarantee that only one test is incrementing blocks at a time
static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

// Run the test in another thread
let test_handler = tokio::spawn(future);

// Acquire Lock
let guard = LOCK.lock().await;

// Check if the test is finished after acquiring the lock
if test_handler.is_finished() {
// Release lock
drop(guard);

// Now is safe to panic
if let Err(err) = test_handler.await {
std::panic::resume_unwind(err.into_panic());
}
return;
}

// Now is safe to panic
if let Err(err) = test_handler.await {
// Resume the panic on the main task
std::panic::resume_unwind(err.into_panic());
}
}

#[tokio::test]
#[serial]
async fn network_status() {
Expand Down
1 change: 1 addition & 0 deletions chains/binance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ rosetta-core.workspace = true
rosetta-server-ethereum.workspace = true
sha3 = "0.10"
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
utils = { path = "../utils" }
33 changes: 1 addition & 32 deletions chains/binance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod tests {
use rosetta_server_ethereum::MaybeWsEthereumClient;
use sha3::Digest;
use std::{collections::BTreeMap, future::Future, path::Path};
use utils::run_test;

/// Binance rpc url
const BINANCE_RPC_WS_URL: &str = "ws://127.0.0.1:8546";
Expand All @@ -62,38 +63,6 @@ mod tests {
}
}

/// Run the test in another thread while sending txs to force binance to mine new blocks
/// # Panic
/// Panics if the future panics
async fn run_test<Fut: Future<Output = ()> + Send + 'static>(future: Fut) {
// Guarantee that only one test is incrementing blocks at a time
static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

// Run the test in another thread
let test_handler: tokio::task::JoinHandle<()> = tokio::spawn(future);

// Acquire Lock
let guard = LOCK.lock().await;

// Check if the test is finished after acquiring the lock
if test_handler.is_finished() {
// Release lock
drop(guard);

// Now is safe to panic
if let Err(err) = test_handler.await {
std::panic::resume_unwind(err.into_panic());
}
return;
}

// Now is safe to panic
if let Err(err) = test_handler.await {
// Resume the panic on the main task
std::panic::resume_unwind(err.into_panic());
}
}

#[tokio::test]
async fn network_status() {
run_test(async move {
Expand Down
1 change: 1 addition & 0 deletions chains/polygon/rosetta-testing-polygon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ rosetta-core.workspace = true
rosetta-server-ethereum.workspace = true
sha3 = "0.10"
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
utils = { path = "../../utils" }
33 changes: 1 addition & 32 deletions chains/polygon/rosetta-testing-polygon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod tests {
use rosetta_server_ethereum::MaybeWsEthereumClient;
use sha3::Digest;
use std::{collections::BTreeMap, future::Future, path::Path};
use utils::run_test;

/// Polygon rpc url
const POLYGON_RPC_WS_URL: &str = "ws://127.0.0.1:8546";
Expand All @@ -62,38 +63,6 @@ mod tests {
}
}

/// Run the test in another thread while sending txs to force polygon to mine new blocks
/// # Panic
/// Panics if the future panics
async fn run_test<Fut: Future<Output = ()> + Send + 'static>(future: Fut) {
// Guarantee that only one test is incrementing blocks at a time
static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

// Run the test in another thread
let test_handler: tokio::task::JoinHandle<()> = tokio::spawn(future);

// Acquire Lock
let guard = LOCK.lock().await;

// Check if the test is finished after acquiring the lock
if test_handler.is_finished() {
// Release lock
drop(guard);

// Now is safe to panic
if let Err(err) = test_handler.await {
std::panic::resume_unwind(err.into_panic());
}
return;
}

// Now is safe to panic
if let Err(err) = test_handler.await {
// Resume the panic on the main task
std::panic::resume_unwind(err.into_panic());
}
}

#[ignore = "No Polygon CI"]
#[tokio::test]
async fn network_status() {
Expand Down
10 changes: 10 additions & 0 deletions chains/utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "utils"
version = "0.1.0"
edition = "2021"
license = "MIT"
repository = "https://github.com/analog-labs/chain-connectors"
description = "utils for rosetta test."

[dependencies]
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
33 changes: 33 additions & 0 deletions chains/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::future::Future;

/// Run the test in another thread while sending txs to force binance to mine new blocks
/// # Panic
/// Panics if the future panics
pub async fn run_test<Fut: Future<Output = ()> + Send + 'static>(future: Fut) {
// Guarantee that only one test is incrementing blocks at a time
static LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

// Run the test in another thread
let test_handler = tokio::spawn(future);

// Acquire Lock
let guard = LOCK.lock().await;

// Check if the test is finished after acquiring the lock
if test_handler.is_finished() {
// Release lock
drop(guard);

// Now is safe to panic
if let Err(err) = test_handler.await {
std::panic::resume_unwind(err.into_panic());
}
return;
}

// Now is safe to panic
if let Err(err) = test_handler.await {
// Resume the panic on the main task
std::panic::resume_unwind(err.into_panic());
}
}

0 comments on commit 783a32c

Please sign in to comment.