Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test_loop] test_loop_sync_actor_maker implementation in TestLoopV2 #11522

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
};
Comment on lines +30 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add comment why we can't register actor right now?
I guess this is because we have some dependency tree of actors. In such case, drawing it somewhere could be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment

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
Loading