Skip to content

Commit

Permalink
[test_loop] test_loop_sync_actor_maker implementation in TestLoopV2 (#…
Browse files Browse the repository at this point in the history
…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](#11521): [test_loop]
Introduce TestLoopV2
[Part 2](#11520): [test_loop]
Change ClientQueries trait requirements in TestLoop util
[Part 3](#11522): [test_loop]
test_loop_sync_actor_maker implementation in TestLoopV2
[Part 4](#11523): [test_loop]
Introduce TestLoopPeerManagerActor for handling network messages across
clients
[Part 5](#11524): [test_loop]
Convert current tests to use TestLoopV2
[Part 6](#11525): [test_loop]
Cleanup old test loop code
[Part 7](#11528): [test_loop]
Better visualizer support for TestLoopV2
[Part 8](#11539): [test_loop]
Simple TestLoopEnvBuilder
  • Loading branch information
Shreyan Gupta authored Jun 12, 2024
1 parent 164b7a3 commit c81ec74
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
37 changes: 33 additions & 4 deletions chain/client/src/test_utils/test_loop/sync_actor.rs
Original file line number Diff line number Diff line change
@@ -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<SyncMessage>, Sender<PeerManagerMessageRequest>) -> 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<Mutex<HashMap<ShardUId, SyncActor>>>;

pub fn test_loop_sync_actor_maker<E>(
sender: DelaySender<E>,
pub fn test_loop_sync_actor_maker_old<E>(
sender: DelaySenderOld<E>,
sync_actors: TestSyncActors,
) -> Arc<
dyn Fn(ShardUId, Sender<SyncMessage>, Sender<PeerManagerMessageRequest>) -> SyncActorHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit c81ec74

Please sign in to comment.