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 ce760ed commit 4fbf950
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 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 @@ -67,33 +67,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
37 changes: 17 additions & 20 deletions crates/iota-indexer/tests/rpc-tests/extended_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ use simulacrum::Simulacrum;
use tempfile::tempdir;
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 {
fn get_or_init_shared_extended_api_simulacrum_env() -> &'static SimulacrumTestSetup {
let data_ingestion_path = tempdir().unwrap().into_path();
let extended_api_env = SimulacrumApiTestEnvDefinition {
unique_env_name: "extended_api".to_string(),
env_initializer: Box::new(|| {
SimulacrumTestSetup::get_or_init(
"extended_api",
|| {
let mut sim = Simulacrum::new();
sim.set_data_ingestion_path(data_ingestion_path.clone());

Expand All @@ -50,14 +47,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 @@ -94,7 +91,7 @@ fn get_epochs() {

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

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

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

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

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

#[test]
fn get_current_epoch() {
let InitializedSimulacrumEnv {
let SimulacrumTestSetup {
runtime,
sim,
store,
Expand Down Expand Up @@ -400,7 +397,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 4fbf950

Please sign in to comment.