Skip to content

Commit

Permalink
Fix and refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Feb 29, 2024
1 parent 239ee97 commit 9ee5ae0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
23 changes: 22 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#![allow(dead_code)] // Needed because this module is imported as module by many test crates

use std::sync::Arc;
use std::time::Duration;

use ethers::core::k256::ecdsa::SigningKey;
use ethers::middleware::SignerMiddleware;
use ethers::providers::{Http, Middleware, Provider};
use ethers::signers::{LocalWallet, Signer};
use ethers::types::{Address, H160};
use ethers::types::{Address, H160, U256};
use postgres_docker_utils::DockerContainerGuard;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::layer::SubscriberExt;
Expand Down Expand Up @@ -104,3 +105,23 @@ pub async fn setup_provider(

Ok(provider)
}

pub async fn await_balance(
provider: &Provider<Http>,
expected_balance: U256,
address: Address,
) -> eyre::Result<()> {
for _ in 0..50 {
let balance = provider.get_balance(address, None).await?;

tracing::info!(?balance, ?expected_balance, "Checking balance");

if balance >= expected_balance {
return Ok(());
} else {
tokio::time::sleep(Duration::from_secs(5)).await;
}
}

eyre::bail!("Balance did not reach expected value");
}
14 changes: 2 additions & 12 deletions tests/send_many_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,7 @@ async fn send_many_txs() -> eyre::Result<()> {
}

let expected_balance = value * num_transfers;
for _ in 0..50 {
let balance = provider.get_balance(ARBITRARY_ADDRESS, None).await?;
await_balance(&provider, expected_balance, ARBITRARY_ADDRESS).await?;

tracing::info!(?balance, ?expected_balance, "Checking balance");

if balance == expected_balance {
return Ok(());
} else {
tokio::time::sleep(Duration::from_secs(5)).await;
}
}

panic!("Transactions were not sent")
Ok(())
}
29 changes: 27 additions & 2 deletions tests/send_too_many_txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ async fn send_too_many_txs() -> eyre::Result<()> {
let (_service, client) =
ServiceBuilder::default().build(&anvil, &db_url).await?;

let CreateApiKeyResponse { api_key: _api_key } =
let CreateApiKeyResponse { api_key } =
client.create_relayer_api_key(DEFAULT_RELAYER_ID).await?;

let CreateRelayerResponse {
relayer_id: secondary_relayer_id,
address: _secondary_relayer_address,
address: secondary_relayer_address,
} = client
.create_relayer(&CreateRelayerRequest {
name: "Secondary Relayer".to_string(),
Expand Down Expand Up @@ -88,5 +88,30 @@ async fn send_too_many_txs() -> eyre::Result<()> {
result
);

// Accumulate total value + gas budget
let send_value = value * (MAX_QUEUED_TXS + 1);
let total_required_value = send_value + parse_units("1", "ether")?;

client
.send_tx(
&api_key,
&SendTxRequest {
to: secondary_relayer_address,
value: total_required_value,
data: None,
gas_limit: U256::from(21_000),
priority: TransactionPriority::Regular,
tx_id: None,
},
)
.await?;

tracing::info!("Waiting for secondary relayer balance");
await_balance(&provider, total_required_value, secondary_relayer_address)
.await?;

tracing::info!("Waiting for queued up txs to be processed");
await_balance(&provider, send_value, ARBITRARY_ADDRESS).await?;

Ok(())
}

0 comments on commit 9ee5ae0

Please sign in to comment.