Skip to content

Commit

Permalink
test(starknet_mempool_p2p): test MempoolP2pRunner terminates if netwo…
Browse files Browse the repository at this point in the history
…rk future terminates / fails
  • Loading branch information
AlonLStarkWare committed Dec 16, 2024
1 parent b8c21bf commit 21535ee
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions crates/starknet_mempool_p2p/src/runner/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use std::time::Duration;

use async_trait::async_trait;
use futures::channel::mpsc::Sender;
use futures::future::ready;
use futures::stream::StreamExt;
use futures::SinkExt;
use futures::{FutureExt, SinkExt};
use papyrus_network::network_manager::test_utils::{
mock_register_broadcast_topic,
BroadcastNetworkMock,
TestSubscriberChannels,
};
use papyrus_network::network_manager::{BroadcastTopicChannels, NetworkManager};
use papyrus_network::network_manager::{BroadcastTopicChannels, NetworkError, NetworkManager};
use papyrus_network::NetworkConfig;
use papyrus_network_types::network_types::BroadcastedMessageMetadata;
use papyrus_protobuf::mempool::RpcTransactionWrapper;
Expand All @@ -24,6 +25,42 @@ use tokio::time::sleep;

use super::MempoolP2pRunner;

#[test]
fn run_returns_when_network_future_returns() {
let network_future = ready(Ok(()));
let TestSubscriberChannels { mock_network: _, subscriber_channels } =
mock_register_broadcast_topic().expect("Failed to create mock network");
let BroadcastTopicChannels { broadcasted_messages_receiver, broadcast_topic_client } =
subscriber_channels;
let gateway_client =
Arc::new(MockGatewayClient { add_tx_sender: futures::channel::mpsc::channel(1).0 });
let mut mempool_p2p_runner = MempoolP2pRunner::new(
Box::pin(network_future),
broadcasted_messages_receiver,
broadcast_topic_client,
gateway_client,
);
mempool_p2p_runner.start().now_or_never().unwrap().unwrap();
}

#[test]
fn run_returns_error_when_network_future_returns_error() {
let network_future = ready(Err(NetworkError::DialError(libp2p::swarm::DialError::Aborted)));
let TestSubscriberChannels { mock_network: _, subscriber_channels } =
mock_register_broadcast_topic().expect("Failed to create mock network");
let BroadcastTopicChannels { broadcasted_messages_receiver, broadcast_topic_client } =
subscriber_channels;
let gateway_client =
Arc::new(MockGatewayClient { add_tx_sender: futures::channel::mpsc::channel(1).0 });
let mut mempool_p2p_runner = MempoolP2pRunner::new(
Box::pin(network_future),
broadcasted_messages_receiver,
broadcast_topic_client,
gateway_client,
);
mempool_p2p_runner.start().now_or_never().unwrap().unwrap_err();
}

// TODO(eitan): Make it an automock
#[derive(Clone)]
struct MockGatewayClient {
Expand All @@ -50,10 +87,11 @@ async fn start_component_receive_tx_happy_flow() {
} = mock_network;
// Creating a placeholder network manager with default config for init of a mempool receiver
let placeholder_network_manager = NetworkManager::new(NetworkConfig::default(), None);
let placeholder_network_future = placeholder_network_manager.run();
let (add_tx_sender, mut add_tx_receiver) = futures::channel::mpsc::channel(1);
let mock_gateway_client = Arc::new(MockGatewayClient { add_tx_sender });
let mut mempool_p2p_runner = MempoolP2pRunner::new(
Some(placeholder_network_manager),
Box::pin(placeholder_network_future),
broadcasted_messages_receiver,
broadcast_topic_client,
mock_gateway_client,
Expand Down

0 comments on commit 21535ee

Please sign in to comment.