From ea13ef10d8506597bd945c56ad0a7cdd60af735b Mon Sep 17 00:00:00 2001 From: erhant Date: Fri, 19 Jun 2026 16:27:01 +0300 Subject: [PATCH] refactor: drastically shorten (480s to 160s) the test time by adding block `wait` helper --- .../tests/indexer_ffi_block_batching.rs | 15 +++-------- .../tests/indexer_ffi_helpers/mod.rs | 25 +++++++++++++++++++ .../tests/indexer_test_run_ffi.rs | 16 +++--------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/integration_tests/tests/indexer_ffi_block_batching.rs b/integration_tests/tests/indexer_ffi_block_batching.rs index 52f68027..c244fbb0 100644 --- a/integration_tests/tests/indexer_ffi_block_batching.rs +++ b/integration_tests/tests/indexer_ffi_block_batching.rs @@ -6,7 +6,6 @@ use anyhow::Result; use indexer_ffi::api::types::FfiOption; -use integration_tests::L2_TO_L1_TIMEOUT; use log::info; #[path = "indexer_ffi_helpers/mod.rs"] @@ -18,17 +17,11 @@ fn indexer_ffi_block_batching() -> Result<()> { // duration of the test; the indexer was started on that runtime. let (_ctx, indexer_ffi, _indexer_dir) = indexer_ffi_helpers::setup()?; - // WAIT + // WAIT: poll until the indexer has finalized at least two blocks (so the + // chain-consistency check below verifies at least one block link), returning + // early instead of sleeping for the full timeout. info!("Waiting for indexer to parse blocks"); - std::thread::sleep(L2_TO_L1_TIMEOUT); - - let last_block_indexer_ffi_res = - unsafe { indexer_ffi_helpers::query_last_block(&raw const indexer_ffi) }; - - assert!(last_block_indexer_ffi_res.error.is_ok()); - assert!(last_block_indexer_ffi_res.is_some); - - let last_block_indexer = last_block_indexer_ffi_res.block_id; + let last_block_indexer = indexer_ffi_helpers::wait_for_indexer_ffi_block(&indexer_ffi, 2)?; info!("Last block on indexer FFI now is {last_block_indexer}"); diff --git a/integration_tests/tests/indexer_ffi_helpers/mod.rs b/integration_tests/tests/indexer_ffi_helpers/mod.rs index 35bf7ef3..73d7bf57 100644 --- a/integration_tests/tests/indexer_ffi_helpers/mod.rs +++ b/integration_tests/tests/indexer_ffi_helpers/mod.rs @@ -84,3 +84,28 @@ pub fn setup() -> Result<(BlockingTestContext, IndexerServiceFFI, TempDir)> { let (indexer_ffi, indexer_dir) = setup_indexer_ffi(&runtime, ctx.ctx().bedrock_addr())?; Ok((ctx, indexer_ffi, indexer_dir)) } + +/// Poll the indexer FFI until its last finalized block id reaches `min_block_id` +/// or until [`integration_tests::L2_TO_L1_TIMEOUT`] elapses. +/// +/// This avoids blindly sleeping for the full timeout: the indexer typically +/// catches up in a fraction of that time, so we return as soon as it does and +/// only use the timeout as a ceiling. Returns the last observed block id. +pub fn wait_for_indexer_ffi_block(indexer: &IndexerServiceFFI, min_block_id: u64) -> Result { + let start = std::time::Instant::now(); + loop { + // SAFETY: `indexer` is a valid reference for the duration of the call. + let res = unsafe { query_last_block(std::ptr::from_ref(indexer)) }; + if res.error.is_ok() && res.is_some && res.block_id >= min_block_id { + return Ok(res.block_id); + } + if start.elapsed() >= integration_tests::L2_TO_L1_TIMEOUT { + anyhow::bail!( + "Indexer FFI did not reach block {min_block_id} within {:?}. Last observed block id: {}", + integration_tests::L2_TO_L1_TIMEOUT, + res.block_id + ); + } + std::thread::sleep(std::time::Duration::from_secs(2)); + } +} diff --git a/integration_tests/tests/indexer_test_run_ffi.rs b/integration_tests/tests/indexer_test_run_ffi.rs index 0a5f4133..56945d30 100644 --- a/integration_tests/tests/indexer_test_run_ffi.rs +++ b/integration_tests/tests/indexer_test_run_ffi.rs @@ -1,11 +1,9 @@ #![expect( clippy::tests_outside_test_module, - clippy::undocumented_unsafe_blocks, reason = "We don't care about these in tests" )] use anyhow::Result; -use integration_tests::L2_TO_L1_TIMEOUT; use log::info; #[path = "indexer_ffi_helpers/mod.rs"] @@ -17,16 +15,10 @@ fn indexer_test_run_ffi() -> Result<()> { // duration of the test; the indexer was started on that runtime. let (_ctx, indexer_ffi, _indexer_dir) = indexer_ffi_helpers::setup()?; - // RUN OBSERVATION - std::thread::sleep(L2_TO_L1_TIMEOUT); - - let last_block_indexer_ffi_res = - unsafe { indexer_ffi_helpers::query_last_block(&raw const indexer_ffi) }; - - assert!(last_block_indexer_ffi_res.error.is_ok()); - assert!(last_block_indexer_ffi_res.is_some); - - let last_block_indexer_ffi = last_block_indexer_ffi_res.block_id; + // RUN OBSERVATION: poll until the indexer has finalized at least one block, + // returning early instead of sleeping for the full timeout. + let last_block_indexer_ffi = + indexer_ffi_helpers::wait_for_indexer_ffi_block(&indexer_ffi, 1)?; info!("Last block on indexer FFI now is {last_block_indexer_ffi}");