Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
test: adding tests to task
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Paitrault <[email protected]>
  • Loading branch information
Freyskeyd committed Mar 18, 2024
1 parent cf1430b commit 3a33465
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
8 changes: 6 additions & 2 deletions crates/topos-tce-api/tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,9 @@ async fn get_pending_pool(
create_validator_store(&[], futures::future::ready(fullnode_store.clone())).await;

for certificate in &certificates {
_ = store.insert_pending_certificate(&certificate.certificate);
_ = store
.insert_pending_certificate(&certificate.certificate)
.await;
}

let storage_client = StorageClient::new(store.clone());
Expand Down Expand Up @@ -800,7 +802,9 @@ async fn check_precedence(
create_validator_store(&[], futures::future::ready(fullnode_store.clone())).await;

for certificate in &certificates {
_ = store.insert_pending_certificate(&certificate.certificate);
_ = store
.insert_pending_certificate(&certificate.certificate)
.await;
}

let storage_client = StorageClient::new(store.clone());
Expand Down
1 change: 1 addition & 0 deletions crates/topos-tce-broadcast/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use topos_tce_storage::validator::ValidatorStore;
use topos_test_sdk::constants::*;
use topos_test_sdk::storage::create_validator_store;

mod task;
mod task_manager;

const CHANNEL_SIZE: usize = 10;
Expand Down
86 changes: 86 additions & 0 deletions crates/topos-tce-broadcast/src/tests/task.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::{future::IntoFuture, str::FromStr, sync::Arc, time::Duration};

use rstest::rstest;
use tokio::{
spawn,
sync::{broadcast, mpsc},
};
use topos_core::uci::Certificate;
use topos_crypto::{messages::MessageSigner, validator_id::ValidatorId};
use topos_tce_storage::validator::ValidatorStore;
use topos_test_sdk::{
certificates::create_certificate_chain,
constants::{SOURCE_SUBNET_ID_1, TARGET_SUBNET_ID_1},
storage::create_validator_store,
};

use crate::{
double_echo::broadcast_state::BroadcastState, event::ProtocolEvents,
sampler::SubscriptionsView, task_manager::task::Task,
};

#[rstest]
#[test_log::test(tokio::test)]
#[timeout(Duration::from_secs(1))]
async fn start_with_ungossiped_cert(
#[future(awt)]
#[from(create_validator_store)]
validatore_store: Arc<ValidatorStore>,
) {
let certificate = create_certificate_chain(SOURCE_SUBNET_ID_1, &[TARGET_SUBNET_ID_1], 1)
.pop()
.unwrap()
.certificate;
let certificate_id = certificate.id;
let validator_id = ValidatorId::default();
let thresholds = topos_config::tce::broadcast::ReliableBroadcastParams {
echo_threshold: 1,
ready_threshold: 1,
delivery_threshold: 1,
};
let (event_sender, mut event_receiver) = mpsc::channel(2);
let (broadcast_sender, _) = broadcast::channel(1);
let message_signer = Arc::new(
MessageSigner::from_str("122f3ae6ade1fd136b292cea4f6243c7811160352c8821528547a1fe7c459daf")
.unwrap(),
);
let need_gossip = true;
let subscriptions = SubscriptionsView::default();

let broadcast_state = BroadcastState::new(
certificate,
validator_id,
thresholds.echo_threshold,
thresholds.ready_threshold,
thresholds.delivery_threshold,
event_sender,
subscriptions,
need_gossip,
message_signer,
);

let (task, _ctx) = Task::new(
certificate_id,
broadcast_state,
validatore_store,
broadcast_sender,
);

let _handle = spawn(task.into_future());

let event = event_receiver.recv().await;
assert!(matches!(
event,
Some(ProtocolEvents::Broadcast {
certificate_id: id
}) if id == certificate_id
));

let event = event_receiver.recv().await;
assert!(matches!(
event,
Some(ProtocolEvents::Gossip {
cert: Certificate { id, .. }
}) if id == certificate_id
));
}
7 changes: 5 additions & 2 deletions crates/topos-tce-broadcast/src/tests/task_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ use crate::{sampler::SubscriptionsView, task_manager::TaskManager};

#[rstest]
#[tokio::test]
async fn can_start(#[future] create_validator_store: Arc<ValidatorStore>) {
let validator_store = create_validator_store.await;
async fn can_start(
#[future(awt)]
#[from(create_validator_store)]
validator_store: Arc<ValidatorStore>,
) {
let (message_sender, message_receiver) = mpsc::channel(1);
let (event_sender, _) = mpsc::channel(1);
let (broadcast_sender, _) = broadcast::channel(1);
Expand Down

0 comments on commit 3a33465

Please sign in to comment.