fix: tests running

This commit is contained in:
Pravdyvy 2026-04-13 13:34:01 +03:00
parent 1ae7192c7a
commit 5f86e597d5
3 changed files with 92 additions and 23 deletions

View File

@ -18,10 +18,6 @@ unsafe extern "C" {
fn start_indexer(config_path: *const c_char, port: u16) -> InitializedIndexerServiceFFIResult;
}
#[expect(
clippy::integer_division_remainder_used,
reason = "Generated by select! macro, can't be easily rewritten to avoid this lint"
)]
fn main() -> Result<()> {
env_logger::init();

View File

@ -1,5 +1,5 @@
use std::{
ffi::{CString, c_char}, fs::File, io::Write, net::SocketAddr, path::PathBuf
ffi::{CString, c_char}, fs::File, io::Write, net::SocketAddr, path::PathBuf, sync::Arc
};
use anyhow::{Context, Result, bail};
@ -9,7 +9,7 @@ use log::{debug, error, warn};
use nssa::AccountId;
use sequencer_core::indexer_client::{IndexerClient, IndexerClientTrait};
use sequencer_service::SequencerHandle;
use sequencer_service_rpc::{SequencerClient, SequencerClientBuilder};
use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder};
use tempfile::TempDir;
use testcontainers::compose::DockerCompose;
use wallet::{WalletCore, config::WalletConfigOverrides};
@ -41,11 +41,10 @@ pub struct TestContextFFI {
_temp_indexer_dir: TempDir,
_temp_sequencer_dir: TempDir,
_temp_wallet_dir: TempDir,
_runtime: tokio::runtime::Runtime,
}
impl TestContextBuilder {
pub fn build_ffi(self, runtime: tokio::runtime::Runtime) -> Result<TestContextFFI> {
pub fn build_ffi(self, runtime: Arc<tokio::runtime::Runtime>) -> Result<TestContextFFI> {
TestContextFFI::new_configured(
self.sequencer_partial_config.unwrap_or_default(),
self.initial_data.unwrap_or_else(|| {
@ -58,7 +57,7 @@ impl TestContextBuilder {
impl TestContextFFI {
/// Create new test context.
pub fn new(runtime: tokio::runtime::Runtime) -> Result<Self> {
pub fn new(runtime: Arc<tokio::runtime::Runtime>) -> Result<Self> {
Self::builder().build_ffi(runtime)
}
@ -70,18 +69,28 @@ impl TestContextFFI {
fn new_configured(
sequencer_partial_config: config::SequencerPartialConfig,
initial_data: config::InitialData,
runtime: tokio::runtime::Runtime
runtime: Arc<tokio::runtime::Runtime>
) -> Result<Self> {
// Ensure logger is initialized only once
*LOGGER;
debug!("Test context setup");
println!("Hello 3");
runtime.block_on(async { println!("Hello 3.5") });
println!("Hello 4");
let (bedrock_compose, bedrock_addr) = runtime.block_on(Self::setup_bedrock_node())?;
println!("Hello 5");
let (indexer_ffi, temp_indexer_dir) = Self::setup_indexer_ffi(bedrock_addr, &initial_data)
.context("Failed to setup Indexer")?;
println!("Hello 6");
let (sequencer_handle, temp_sequencer_dir) = runtime.block_on(Self::setup_sequencer(
sequencer_partial_config,
bedrock_addr,
@ -90,11 +99,15 @@ impl TestContextFFI {
))
.context("Failed to setup Sequencer")?;
println!("Hello 7");
let (wallet, temp_wallet_dir, wallet_password) =
runtime.block_on(Self::setup_wallet(sequencer_handle.addr(), &initial_data)
)
.context("Failed to setup wallet")?;
println!("Hello 8");
let sequencer_url = config::addr_to_url(config::UrlProtocol::Http, sequencer_handle.addr())
.context("Failed to convert sequencer addr to URL")?;
let indexer_url =
@ -107,6 +120,8 @@ impl TestContextFFI {
)
.context("Failed to create indexer client")?;
println!("Hello 9");
Ok(Self {
sequencer_client,
indexer_client,
@ -118,7 +133,6 @@ impl TestContextFFI {
_temp_indexer_dir: temp_indexer_dir,
_temp_sequencer_dir: temp_sequencer_dir,
_temp_wallet_dir: temp_wallet_dir,
_runtime: runtime,
})
}
@ -339,12 +353,15 @@ impl TestContextFFI {
.collect()
}
pub fn get_last_block_sequencer(&self) -> Result<u64> {
Ok(self._runtime.block_on(self.sequencer_client.get_last_finalized_block_id())?)
pub fn get_last_block_sequencer(&self, runtime: Arc<tokio::runtime::Runtime>) -> Result<u64> {
let res = runtime.block_on(self.sequencer_client.get_last_block_id()).unwrap();
println!("Hello 11.5");
Ok(res)
}
pub fn get_last_block_indexer(&self) -> Result<u64> {
Ok(self._runtime.block_on(self.indexer_client.get_last_finalized_block_id())?)
pub fn get_last_block_indexer(&self, runtime: Arc<tokio::runtime::Runtime>) -> Result<u64> {
Ok(runtime.block_on(self.indexer_client.get_last_finalized_block_id())?)
}
}
@ -361,7 +378,6 @@ impl Drop for TestContextFFI {
indexer_client: _,
wallet: _,
wallet_password: _,
_runtime: _,
} = self;
let sequencer_handle = sequencer_handle
@ -377,12 +393,16 @@ impl Drop for TestContextFFI {
);
}
println!("Hello 14");
let indexer_handle = unsafe { indexer_ffi.handle() };
if !indexer_handle.is_healthy() {
error!("Indexer handle has unexpectedly stopped before TestContext drop");
}
println!("Hello 15");
let container = bedrock_compose
.service(BEDROCK_SERVICE_WITH_OPEN_PORT)
.unwrap_or_else(|| {
@ -400,5 +420,50 @@ impl Drop for TestContextFFI {
container.id()
);
}
println!("Hello 16");
}
}
/// A test context with ffi to be used in normal #[test] tests.
pub struct BlockingTestContextFFI {
ctx: Option<TestContextFFI>,
runtime: Arc<tokio::runtime::Runtime>,
}
impl BlockingTestContextFFI {
pub fn new() -> Result<Self> {
let runtime = tokio::runtime::Runtime::new().unwrap();
let runtime_wrapped = Arc::new(runtime);
let ctx = TestContextFFI::new(runtime_wrapped.clone())?;
Ok(Self {
ctx: Some(ctx),
runtime: runtime_wrapped,
})
}
pub const fn ctx(&self) -> &TestContextFFI {
self.ctx.as_ref().expect("TestContext is set")
}
pub fn runtime(&self) -> Arc<tokio::runtime::Runtime> {
self.runtime.clone()
}
}
impl Drop for BlockingTestContextFFI {
fn drop(&mut self) {
let Self { ctx, runtime } = self;
println!("Hello 20");
// Ensure async cleanup of TestContext by blocking on its drop in the runtime.
runtime.block_on(async {
if let Some(ctx) = ctx.take() {
drop(ctx);
}
});
println!("Hello 21");
}
}

View File

@ -7,7 +7,7 @@ use std::time::Duration;
use anyhow::Result;
use indexer_service_rpc::RpcClient as _;
use integration_tests::{TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, format_public_account_id, test_context_ffi::TestContextFFI};
use integration_tests::{TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, format_public_account_id, test_context_ffi::BlockingTestContextFFI};
use log::info;
use wallet::cli::{Command, programs::native_token_transfer::AuthTransferSubcommand};
@ -150,17 +150,25 @@ async fn indexer_state_consistency() -> Result<()> {
#[test]
fn indexer_test_run_ffi() -> Result<()> {
println!("Hello 1");
let runtime = tokio::runtime::Runtime::new()?;
println!("Hello 2");
let _ctx = TestContextFFI::new(runtime)?;
let blocking_ctx = BlockingTestContextFFI::new()?;
let runtime_wrapped = blocking_ctx.runtime();
log::info!("Hello 3");
// RUN OBSERVATION
std::thread::sleep(std::time::Duration::from_millis(L2_TO_L1_TIMEOUT_MILLIS));
runtime_wrapped.block_on(async {
tokio::time::sleep(std::time::Duration::from_millis(L2_TO_L1_TIMEOUT_MILLIS)).await;
});
let last_block_seq = _ctx.get_last_block_sequencer()?;
let last_block_indexer = _ctx.get_last_block_indexer()?;
println!("Hello 11");
let last_block_seq = blocking_ctx.ctx().get_last_block_sequencer(runtime_wrapped.clone())?;
println!("Hello 12");
let last_block_indexer = blocking_ctx.ctx().get_last_block_indexer(runtime_wrapped)?;
println!("Hello 13");
println!("Last block on ind now is {last_block_indexer}");
println!("Last block on seq now is {last_block_seq}");