Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ngkuru committed Aug 7, 2023
1 parent e7ac2e5 commit c729973
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 139 deletions.
10 changes: 5 additions & 5 deletions crates/aptos-api-tester/src/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use once_cell::sync::Lazy;
use prometheus::{register_histogram_vec, Histogram, HistogramVec};

pub static API_TEST_SUCCESS: Lazy<HistogramVec> = Lazy::new(|| {
pub const API_TEST_SUCCESS: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
"api_test_success",
"Number of user flows which succesfully passed",
Expand All @@ -16,7 +16,7 @@ pub fn test_success(test_name: &str, network_name: &str, run_id: &str) -> Histog
API_TEST_SUCCESS.with_label_values(&[test_name, network_name, run_id])
}

pub static API_TEST_FAIL: Lazy<HistogramVec> = Lazy::new(|| {
pub const API_TEST_FAIL: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
"api_test_fail",
"Number of user flows which failed checks",
Expand All @@ -29,7 +29,7 @@ pub fn test_fail(test_name: &str, network_name: &str, run_id: &str) -> Histogram
API_TEST_FAIL.with_label_values(&[test_name, network_name, run_id])
}

pub static API_TEST_ERROR: Lazy<HistogramVec> = Lazy::new(|| {
pub const API_TEST_ERROR: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!("api_test_error", "Number of user flows which crashed", &[
"test_name",
"network_name",
Expand All @@ -42,7 +42,7 @@ pub fn test_error(test_name: &str, network_name: &str, run_id: &str) -> Histogra
API_TEST_ERROR.with_label_values(&[test_name, network_name, run_id])
}

pub static API_TEST_LATENCY: Lazy<HistogramVec> = Lazy::new(|| {
pub const API_TEST_LATENCY: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
"api_test_latency",
"Time it takes to complete a user flow",
Expand All @@ -55,7 +55,7 @@ pub fn test_latency(test_name: &str, network_name: &str, run_id: &str, result: &
API_TEST_LATENCY.with_label_values(&[test_name, network_name, run_id, result])
}

pub static API_TEST_STEP_LATENCY: Lazy<HistogramVec> = Lazy::new(|| {
pub const API_TEST_STEP_LATENCY: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
"api_test_step_latency",
"Time it takes to complete a user flow step",
Expand Down
2 changes: 1 addition & 1 deletion crates/aptos-api-tester/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use futures::future::join_all;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::runtime::{Builder, Runtime};

const STACK_SIZE: usize = 4 * 1024 * 1024;
static STACK_SIZE: usize = 4 * 1024 * 1024;

async fn test_flows(runtime: &Runtime, network_name: NetworkName) -> Result<()> {
let run_id = SystemTime::now()
Expand Down
37 changes: 3 additions & 34 deletions crates/aptos-api-tester/src/tests/coin_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
persistent_check, time_fn,
utils::{
create_account, create_and_fund_account, emit_step_metrics, get_client, get_faucet_client,
NetworkName, TestFailure, TestName, FUND_AMOUNT,
NetworkName, TestFailure, TestName, FUND_AMOUNT, check_balance,
},
};
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -149,8 +149,8 @@ async fn check_account_data(
account: AccountAddress,
receiver: AccountAddress,
) -> Result<(), TestFailure> {
check_balance(client, account, U64(FUND_AMOUNT)).await?;
check_balance(client, receiver, U64(0)).await?;
check_balance(TestName::CoinTransfer, client, account, U64(FUND_AMOUNT)).await?;
check_balance(TestName::CoinTransfer, client, receiver, U64(0)).await?;

Ok(())
}
Expand Down Expand Up @@ -268,34 +268,3 @@ async fn check_account_balance_at_version(

Ok(())
}

// Utils

async fn check_balance(
client: &Client,
address: AccountAddress,
expected: U64,
) -> Result<(), TestFailure> {
// actual
let actual = match client.get_account_balance(address).await {
Ok(response) => response.into_inner().coin.value,
Err(e) => {
info!(
"test: coin_transfer part: check_account_data ERROR: {}, with error {:?}",
ERROR_NO_BALANCE, e
);
return Err(e.into());
},
};

// compare
if expected != actual {
info!(
"test: coin_transfer part: check_account_data FAIL: {}, expected {:?}, got {:?}",
FAIL_WRONG_BALANCE, expected, actual
);
return Err(TestFailure::Fail(FAIL_WRONG_BALANCE));
}

Ok(())
}
37 changes: 8 additions & 29 deletions crates/aptos-api-tester/src/tests/new_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
use crate::{
fail_message::{
ERROR_COULD_NOT_CREATE_ACCOUNT, ERROR_COULD_NOT_FUND_ACCOUNT, ERROR_NO_ACCOUNT_DATA,
ERROR_NO_BALANCE, FAIL_WRONG_ACCOUNT_DATA, FAIL_WRONG_BALANCE,
FAIL_WRONG_ACCOUNT_DATA,
},
persistent_check, time_fn,
utils::{
create_account, emit_step_metrics, get_client, get_faucet_client, NetworkName, TestFailure,
TestName, FUND_AMOUNT,
check_balance, create_account, emit_step_metrics, get_client, get_faucet_client,
NetworkName, TestFailure, TestName, FUND_AMOUNT,
},
};
use aptos_api_types::U64;
Expand Down Expand Up @@ -92,7 +92,10 @@ async fn setup(
return Err(e.into());
},
};
info!("test: new_account part: setup creating account: {}", account.address());
info!(
"test: new_account part: setup creating account: {}",
account.address()
);

Ok((client, faucet_client, account))
}
Expand Down Expand Up @@ -145,29 +148,5 @@ async fn check_account_balance(
client: &Client,
address: AccountAddress,
) -> Result<(), TestFailure> {
// expected
let expected = U64(FUND_AMOUNT);

// actual
let actual = match client.get_account_balance(address).await {
Ok(response) => response.into_inner().coin.value,
Err(e) => {
info!(
"test: new_account part: check_account_balance ERROR: {}, with error {:?}",
ERROR_NO_BALANCE, e
);
return Err(e.into());
},
};

// compare
if expected != actual {
info!(
"test: new_account part: check_account_balance FAIL: {}, expected {:?}, got {:?}",
FAIL_WRONG_BALANCE, expected, actual
);
return Err(TestFailure::Fail(FAIL_WRONG_BALANCE));
}

Ok(())
check_balance(TestName::NewAccount, client, address, U64(FUND_AMOUNT)).await
}
43 changes: 7 additions & 36 deletions crates/aptos-api-tester/src/tests/nft_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
use crate::{
fail_message::{
ERROR_COULD_NOT_CREATE_TRANSACTION, ERROR_COULD_NOT_FINISH_TRANSACTION,
ERROR_COULD_NOT_FUND_ACCOUNT, ERROR_NO_BALANCE, ERROR_NO_COLLECTION_DATA,
ERROR_NO_TOKEN_BALANCE, ERROR_NO_TOKEN_DATA, FAIL_WRONG_BALANCE,
FAIL_WRONG_COLLECTION_DATA, FAIL_WRONG_TOKEN_BALANCE, FAIL_WRONG_TOKEN_DATA,
ERROR_COULD_NOT_FUND_ACCOUNT, ERROR_NO_COLLECTION_DATA, ERROR_NO_TOKEN_BALANCE,
ERROR_NO_TOKEN_DATA, FAIL_WRONG_COLLECTION_DATA, FAIL_WRONG_TOKEN_BALANCE,
FAIL_WRONG_TOKEN_DATA,
},
persistent_check, time_fn,
utils::{
create_and_fund_account, emit_step_metrics, get_client, get_faucet_client, NetworkName,
TestFailure, TestName, FUND_AMOUNT,
check_balance, create_and_fund_account, emit_step_metrics, get_client, get_faucet_client,
NetworkName, TestFailure, TestName, FUND_AMOUNT,
},
};
use aptos_api_types::U64;
Expand Down Expand Up @@ -222,8 +222,8 @@ async fn check_account_data(
account: AccountAddress,
receiver: AccountAddress,
) -> Result<(), TestFailure> {
check_balance(client, account, U64(FUND_AMOUNT)).await?;
check_balance(client, receiver, U64(FUND_AMOUNT)).await?;
check_balance(TestName::NftTransfer, client, account, U64(FUND_AMOUNT)).await?;
check_balance(TestName::NftTransfer, client, receiver, U64(FUND_AMOUNT)).await?;

Ok(())
}
Expand Down Expand Up @@ -541,35 +541,6 @@ fn token_data(address: AccountAddress) -> TokenData {
}
}

async fn check_balance(
client: &Client,
address: AccountAddress,
expected: U64,
) -> Result<(), TestFailure> {
// actual
let actual = match client.get_account_balance(address).await {
Ok(response) => response.into_inner().coin.value,
Err(e) => {
info!(
"test: nft_transfer part: check_account_data ERROR: {}, with error {:?}",
ERROR_NO_BALANCE, e
);
return Err(e.into());
},
};

// compare
if expected != actual {
info!(
"test: nft_transfer part: check_account_data FAIL: {}, expected {:?}, got {:?}",
FAIL_WRONG_BALANCE, expected, actual
);
return Err(TestFailure::Fail(FAIL_WRONG_BALANCE));
}

Ok(())
}

async fn check_token_balance(
token_client: &TokenClient<'_>,
address: AccountAddress,
Expand Down
37 changes: 4 additions & 33 deletions crates/aptos-api-tester/src/tests/publish_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crate::{
ERROR_COULD_NOT_BUILD_PACKAGE, ERROR_COULD_NOT_CREATE_TRANSACTION,
ERROR_COULD_NOT_FINISH_TRANSACTION, ERROR_COULD_NOT_FUND_ACCOUNT,
ERROR_COULD_NOT_SERIALIZE, ERROR_NO_BYTECODE, ERROR_NO_MESSAGE, ERROR_NO_METADATA,
ERROR_NO_MODULE, FAIL_WRONG_MESSAGE, FAIL_WRONG_MODULE, ERROR_NO_BALANCE, FAIL_WRONG_BALANCE,
ERROR_NO_MODULE, FAIL_WRONG_MESSAGE, FAIL_WRONG_MODULE,
},
persistent_check, time_fn,
utils::{
create_and_fund_account, emit_step_metrics, get_client, get_faucet_client, NetworkName,
TestFailure, TestName, FUND_AMOUNT,
check_balance, create_and_fund_account, emit_step_metrics, get_client, get_faucet_client,
NetworkName, TestFailure, TestName, FUND_AMOUNT,
},
};
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -152,7 +152,7 @@ async fn setup(network_name: NetworkName) -> Result<(Client, LocalAccount), Test
}

async fn check_account_data(client: &Client, account: AccountAddress) -> Result<(), TestFailure> {
check_balance(client, account, U64(FUND_AMOUNT)).await?;
check_balance(TestName::PublishModule, client, account, U64(FUND_AMOUNT)).await?;

Ok(())
}
Expand Down Expand Up @@ -373,35 +373,6 @@ async fn check_message(client: &Client, address: AccountAddress) -> Result<(), T

// Utils

async fn check_balance(
client: &Client,
address: AccountAddress,
expected: U64,
) -> Result<(), TestFailure> {
// actual
let actual = match client.get_account_balance(address).await {
Ok(response) => response.into_inner().coin.value,
Err(e) => {
info!(
"test: nft_transfer part: check_account_data ERROR: {}, with error {:?}",
ERROR_NO_BALANCE, e
);
return Err(e.into());
},
};

// compare
if expected != actual {
info!(
"test: nft_transfer part: check_account_data FAIL: {}, expected {:?}, got {:?}",
FAIL_WRONG_BALANCE, expected, actual
);
return Err(TestFailure::Fail(FAIL_WRONG_BALANCE));
}

Ok(())
}

async fn get_message(client: &Client, address: AccountAddress) -> Option<String> {
let resource = match client
.get_account_resource(
Expand Down
41 changes: 40 additions & 1 deletion crates/aptos-api-tester/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

use crate::{
counters::{test_error, test_fail, test_latency, test_step_latency, test_success},
fail_message::{ERROR_NO_BALANCE, FAIL_WRONG_BALANCE},
tests::{coin_transfer, new_account, nft_transfer, publish_module},
time_fn,
};
use anyhow::Result;
use aptos_api_types::U64;
use aptos_logger::info;
use aptos_rest_client::{error::RestError, Client, FaucetClient};
use aptos_sdk::types::LocalAccount;
use aptos_types::account_address::AccountAddress;
use once_cell::sync::Lazy;
use std::env;
use url::Url;

pub const FUND_AMOUNT: u64 = 100_000_000;
pub static FUND_AMOUNT: u64 = 100_000_000;

// network urls
const DEVNET_NODE_URL: Lazy<Url> =
Expand Down Expand Up @@ -139,6 +142,42 @@ pub async fn create_and_fund_account(faucet_client: &FaucetClient) -> Result<Loc
Ok(account)
}

/// Check account balance.
pub async fn check_balance(
test_name: TestName,
client: &Client,
address: AccountAddress,
expected: U64,
) -> Result<(), TestFailure> {
// actual
let actual = match client.get_account_balance(address).await {
Ok(response) => response.into_inner().coin.value,
Err(e) => {
info!(
"test: {} part: check_account_data ERROR: {}, with error {:?}",
&test_name.to_string(),
ERROR_NO_BALANCE,
e
);
return Err(e.into());
},
};

// compare
if expected != actual {
info!(
"test: {} part: check_account_data FAIL: {}, expected {:?}, got {:?}",
&test_name.to_string(),
FAIL_WRONG_BALANCE,
expected,
actual
);
return Err(TestFailure::Fail(FAIL_WRONG_BALANCE));
}

Ok(())
}

// metrics helpers

/// Emit metrics based on test result.
Expand Down

0 comments on commit c729973

Please sign in to comment.