From c81ec74ff8def17f348cfd50edf01dd3f54f19a3 Mon Sep 17 00:00:00 2001 From: Shreyan Gupta Date: Wed, 12 Jun 2024 16:18:59 -0700 Subject: [PATCH] [test_loop] test_loop_sync_actor_maker implementation in TestLoopV2 (#11522) Part 3 This PR has the implementation of sync_actor_maker function for test loop. We don't have control over when the sync_actor_maker function is called during execution so we use an adhoc callback event to register the newly created sync_actor with testloop. ### Other PRs [Part 1](https://github.com/near/nearcore/pull/11521): [test_loop] Introduce TestLoopV2 [Part 2](https://github.com/near/nearcore/pull/11520): [test_loop] Change ClientQueries trait requirements in TestLoop util [Part 3](https://github.com/near/nearcore/pull/11522): [test_loop] test_loop_sync_actor_maker implementation in TestLoopV2 [Part 4](https://github.com/near/nearcore/pull/11523): [test_loop] Introduce TestLoopPeerManagerActor for handling network messages across clients [Part 5](https://github.com/near/nearcore/pull/11524): [test_loop] Convert current tests to use TestLoopV2 [Part 6](https://github.com/near/nearcore/pull/11525): [test_loop] Cleanup old test loop code [Part 7](https://github.com/near/nearcore/pull/11528): [test_loop] Better visualizer support for TestLoopV2 [Part 8](https://github.com/near/nearcore/pull/11539): [test_loop] Simple TestLoopEnvBuilder --- .../src/test_utils/test_loop/sync_actor.rs | 37 +++++++++++++++++-- .../multinode_stateless_validators.rs | 4 +- .../features/multinode_test_loop_example.rs | 4 +- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/chain/client/src/test_utils/test_loop/sync_actor.rs b/chain/client/src/test_utils/test_loop/sync_actor.rs index 66a43e8726f..639835e0669 100644 --- a/chain/client/src/test_utils/test_loop/sync_actor.rs +++ b/chain/client/src/test_utils/test_loop/sync_actor.rs @@ -1,19 +1,48 @@ use crate::sync::adapter::SyncActorHandler; use crate::sync::sync_actor::SyncActor; use crate::SyncMessage; -use near_async::messaging::{IntoSender, Sender}; -use near_async::test_loop::delay_sender::DelaySender; +use near_async::messaging::{IntoSender, LateBoundSender, Sender}; +use near_async::test_loop::data::TestLoopData; +use near_async::test_loop::delay_sender::DelaySender as DelaySenderOld; use near_async::test_loop::event_handler::LoopEventHandler; +use near_async::test_loop::DelaySender; use near_network::state_sync::StateSyncResponse; use near_network::types::PeerManagerMessageRequest; use near_primitives::shard_layout::ShardUId; use std::collections::HashMap; use std::sync::{Arc, Mutex}; +pub fn test_loop_sync_actor_maker( + sender: DelaySender, +) -> Arc< + dyn Fn(ShardUId, Sender, Sender) -> SyncActorHandler + + Send + + Sync, +> { + // This is a closure that will be called by SyncAdapter to create SyncActor. + // Since we don't have too much control over when the closure is called, we need to use the CallbackEvent + // to register the SyncActor in the TestLoopData. + // TestLoop and TestLoopData can not cross the closure boundary and be moved while the PendingEventsSender can. + Arc::new(move |shard_uid, client_sender, network_sender| { + let sync_actor = SyncActor::new(shard_uid, client_sender, network_sender); + let sync_actor_adapter = LateBoundSender::new(); + let sync_actor_adapter_clone = sync_actor_adapter.clone(); + let callback = move |data: &mut TestLoopData| { + data.register_actor(sync_actor, Some(sync_actor_adapter)); + }; + sender.send(format!("Register SyncActor {:?}", shard_uid), Box::new(callback)); + SyncActorHandler { + client_sender: sync_actor_adapter_clone.as_sender(), + network_sender: sync_actor_adapter_clone.as_sender(), + shutdown: Mutex::new(Box::new(move || {})), + } + }) +} + pub type TestSyncActors = Arc>>; -pub fn test_loop_sync_actor_maker( - sender: DelaySender, +pub fn test_loop_sync_actor_maker_old( + sender: DelaySenderOld, sync_actors: TestSyncActors, ) -> Arc< dyn Fn(ShardUId, Sender, Sender) -> SyncActorHandler diff --git a/integration-tests/src/tests/client/features/multinode_stateless_validators.rs b/integration-tests/src/tests/client/features/multinode_stateless_validators.rs index 1a848ac4cda..f17c5f817a2 100644 --- a/integration-tests/src/tests/client/features/multinode_stateless_validators.rs +++ b/integration-tests/src/tests/client/features/multinode_stateless_validators.rs @@ -51,7 +51,7 @@ use near_client::test_utils::test_loop::partial_witness_actor::{ }; use near_client::test_utils::test_loop::sync_actor::{ forward_sync_actor_messages_from_client, forward_sync_actor_messages_from_network, - test_loop_sync_actor_maker, TestSyncActors, + test_loop_sync_actor_maker_old, TestSyncActors, }; use near_client::test_utils::test_loop::sync_jobs_actor::forward_messages_from_client_to_sync_jobs_actor; use near_client::test_utils::test_loop::{ @@ -277,7 +277,7 @@ fn test_stateless_validators_with_multi_test_loop() { let state_sync_adapter = Arc::new(RwLock::new(SyncAdapter::new( builder.sender().for_index(idx).into_sender(), builder.sender().for_index(idx).into_sender(), - test_loop_sync_actor_maker(builder.sender().for_index(idx), sync_actors.clone()), + test_loop_sync_actor_maker_old(builder.sender().for_index(idx), sync_actors.clone()), ))); let contract_cache = FilesystemContractRuntimeCache::new(&homedir, None::<&str>) .expect("filesystem contract cache") diff --git a/integration-tests/src/tests/client/features/multinode_test_loop_example.rs b/integration-tests/src/tests/client/features/multinode_test_loop_example.rs index 10a1402fc62..e70d451953b 100644 --- a/integration-tests/src/tests/client/features/multinode_test_loop_example.rs +++ b/integration-tests/src/tests/client/features/multinode_test_loop_example.rs @@ -51,7 +51,7 @@ use near_client::test_utils::test_loop::partial_witness_actor::{ }; use near_client::test_utils::test_loop::sync_actor::{ forward_sync_actor_messages_from_client, forward_sync_actor_messages_from_network, - test_loop_sync_actor_maker, TestSyncActors, + test_loop_sync_actor_maker_old, TestSyncActors, }; use near_client::test_utils::test_loop::sync_jobs_actor::forward_messages_from_client_to_sync_jobs_actor; use near_client::test_utils::test_loop::{ @@ -256,7 +256,7 @@ fn test_client_with_multi_test_loop() { let state_sync_adapter = Arc::new(RwLock::new(SyncAdapter::new( builder.sender().for_index(idx).into_sender(), builder.sender().for_index(idx).into_sender(), - test_loop_sync_actor_maker(builder.sender().for_index(idx), sync_actors.clone()), + test_loop_sync_actor_maker_old(builder.sender().for_index(idx), sync_actors.clone()), ))); let contract_cache = FilesystemContractRuntimeCache::new(&homedir, None::<&str>) .expect("filesystem contract cache")