Skip to content

Commit

Permalink
Remove SimulacrumApiTestEnvDefinition type, rename InitializedSimulac…
Browse files Browse the repository at this point in the history
…rumEnv type
  • Loading branch information
tomxey committed Oct 28, 2024
1 parent b368379 commit c81f663
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
24 changes: 10 additions & 14 deletions crates/iota-indexer/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,29 @@ impl ApiTestSetup {
}
}

pub struct SimulacrumApiTestEnvDefinition {
pub unique_env_name: String,
pub env_initializer: Box<dyn Fn() -> Simulacrum>,
}

pub struct InitializedSimulacrumEnv {
pub struct SimulacrumTestSetup {
pub runtime: Runtime,
pub sim: Arc<Simulacrum>,
pub store: PgIndexerStore,
/// Indexer RPC Client
pub client: HttpClient,
}

impl SimulacrumApiTestEnvDefinition {
pub fn get_or_init_env<'a>(
&self,
initialized_env_container: &'a OnceLock<InitializedSimulacrumEnv>,
) -> &'a InitializedSimulacrumEnv {
impl SimulacrumTestSetup {
pub fn get_or_init<'a>(
unique_env_name: &str,
env_initializer: impl Fn() -> Simulacrum,
initialized_env_container: &'a OnceLock<SimulacrumTestSetup>,
) -> &'a SimulacrumTestSetup {
initialized_env_container.get_or_init(|| {
let runtime = tokio::runtime::Runtime::new().unwrap();
let sim = Arc::new((self.env_initializer)());
let db_name = format!("simulacrum_env_db_{}", self.unique_env_name);
let sim = Arc::new(env_initializer());
let db_name = format!("simulacrum_env_db_{}", unique_env_name);
let (_, store, _, client) = runtime.block_on(
start_simulacrum_rest_api_with_read_write_indexer(sim.clone(), Some(&db_name)),
);

InitializedSimulacrumEnv {
SimulacrumTestSetup {
runtime,
sim,
store,
Expand Down
39 changes: 18 additions & 21 deletions crates/iota-indexer/tests/rpc-tests/extended_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,24 @@ use iota_json_rpc_types::{
TransactionBlockBytes,
};
use iota_types::{
IOTA_FRAMEWORK_ADDRESS,
base_types::{IotaAddress, ObjectID},
gas_coin::GAS,
quorum_driver_types::ExecuteTransactionRequestType,
storage::ReadStore,
IOTA_FRAMEWORK_ADDRESS,
};
use simulacrum::Simulacrum;
use test_cluster::TestCluster;

use crate::common::{
indexer_wait_for_checkpoint, ApiTestSetup, InitializedSimulacrumEnv,
SimulacrumApiTestEnvDefinition,
};
use crate::common::{ApiTestSetup, SimulacrumTestSetup, indexer_wait_for_checkpoint};

static EXTENDED_API_SHARED_SIMULACRUM_INITIALIZED_ENV: OnceLock<InitializedSimulacrumEnv> =
static EXTENDED_API_SHARED_SIMULACRUM_INITIALIZED_ENV: OnceLock<SimulacrumTestSetup> =
OnceLock::new();

fn get_or_init_shared_extended_api_simulacrum_env() -> &'static InitializedSimulacrumEnv {
let extended_api_env = SimulacrumApiTestEnvDefinition {
unique_env_name: "extended_api".to_string(),
env_initializer: Box::new(|| {
fn get_or_init_shared_extended_api_simulacrum_env() -> &'static SimulacrumTestSetup {
SimulacrumTestSetup::get_or_init(
"extended_api",
|| {
let mut sim = Simulacrum::new();

execute_simulacrum_transactions(&mut sim, 15);
Expand All @@ -47,14 +44,14 @@ fn get_or_init_shared_extended_api_simulacrum_env() -> &'static InitializedSimul
add_checkpoints(&mut sim, 300);

sim
}),
};
extended_api_env.get_or_init_env(&EXTENDED_API_SHARED_SIMULACRUM_INITIALIZED_ENV)
},
&EXTENDED_API_SHARED_SIMULACRUM_INITIALIZED_ENV,
)
}

#[test]
fn get_epochs() {
let InitializedSimulacrumEnv {
let SimulacrumTestSetup {
runtime,
sim,
store,
Expand Down Expand Up @@ -91,7 +88,7 @@ fn get_epochs() {

#[test]
fn get_epochs_descending() {
let InitializedSimulacrumEnv {
let SimulacrumTestSetup {
runtime,
sim,
store,
Expand All @@ -118,7 +115,7 @@ fn get_epochs_descending() {

#[test]
fn get_epochs_paging() {
let InitializedSimulacrumEnv {
let SimulacrumTestSetup {
runtime,
sim,
store,
Expand Down Expand Up @@ -160,7 +157,7 @@ fn get_epochs_paging() {

#[test]
fn get_epoch_metrics() {
let InitializedSimulacrumEnv {
let SimulacrumTestSetup {
runtime,
sim,
store,
Expand Down Expand Up @@ -197,7 +194,7 @@ fn get_epoch_metrics() {

#[test]
fn get_epoch_metrics_descending() {
let InitializedSimulacrumEnv {
let SimulacrumTestSetup {
runtime,
sim,
store,
Expand Down Expand Up @@ -227,7 +224,7 @@ fn get_epoch_metrics_descending() {

#[test]
fn get_epoch_metrics_paging() {
let InitializedSimulacrumEnv {
let SimulacrumTestSetup {
runtime,
sim,
store,
Expand Down Expand Up @@ -269,7 +266,7 @@ fn get_epoch_metrics_paging() {

#[test]
fn get_current_epoch() {
let InitializedSimulacrumEnv {
let SimulacrumTestSetup {
runtime,
sim,
store,
Expand Down Expand Up @@ -397,7 +394,7 @@ fn get_all_epoch_address_metrics() {

#[test]
fn get_total_transactions() {
let InitializedSimulacrumEnv {
let SimulacrumTestSetup {
runtime,
sim,
store,
Expand Down

0 comments on commit c81f663

Please sign in to comment.