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

Commit

Permalink
chore: adding test for the coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Paitrault <[email protected]>
  • Loading branch information
Freyskeyd committed Dec 15, 2023
1 parent 153f2f7 commit 08aca99
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/topos-tce/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
tracing.workspace = true
test-log.workspace = true
cucumber = "0.13.0"
env_logger.workspace = true

[features]
default = []
Expand Down
15 changes: 9 additions & 6 deletions crates/topos-tce/src/app_context/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ impl AppContext {
{
entry.insert(CERTIFICATE_DELIVERY_LATENCY.start_timer());
}

debug!(
"Received certificate {} from Gossip message from {}",
cert.id, from
);
spawn(async move {
debug!(
"Received certificate {} from Gossip message from {}",
cert.id, from
);
info!("Send certificate {} to be broadcast", cert.id);
if channel
.send(DoubleEchoCommand::Broadcast {
Expand Down Expand Up @@ -140,8 +139,12 @@ impl AppContext {
}
});
}
_ => {}
e => {
error!("{:?}", e);
}
}
} else {
error!("Unable to decode received message");
}
}
}
107 changes: 107 additions & 0 deletions crates/topos-tce/src/tests/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use std::sync::Arc;

use libp2p::PeerId;
use prost::Message;
use rstest::rstest;
use test_log::test;
use tokio::sync::{mpsc, oneshot};
use topos_core::api::grpc::tce::v1::{double_echo_request, DoubleEchoRequest, Echo, Gossip, Ready};
use topos_crypto::{messages::MessageSigner, validator_id::ValidatorId};
use topos_tce_storage::{store::WriteStore, types::PendingResult};
use topos_test_sdk::{
certificates::create_certificate_chain,
constants::{SOURCE_SUBNET_ID_1, TARGET_SUBNET_ID_1},
};

use crate::AppContext;

use super::setup_test;

#[rstest]
#[test(tokio::test)]
async fn handle_new_certificate(
#[future] setup_test: (
AppContext,
mpsc::Receiver<topos_p2p::Command>,
Arc<MessageSigner>,
),
) {
let (mut context, mut p2p_receiver, message_signer) = setup_test.await;
let mut certificates = create_certificate_chain(SOURCE_SUBNET_ID_1, &[TARGET_SUBNET_ID_1], 1);
let certificate = certificates.pop().unwrap().certificate;

let (sender, receiver) = oneshot::channel();

context
.on_api_event(topos_tce_api::RuntimeEvent::CertificateSubmitted {
certificate: Box::new(certificate),
sender,
})
.await;

let response = receiver.await;

assert!(matches!(response, Ok(Ok(PendingResult::InPending(_)))));
}

#[rstest]
#[test(tokio::test)]
async fn handle_certificate_in_precedence_pool(
#[future] setup_test: (
AppContext,
mpsc::Receiver<topos_p2p::Command>,
Arc<MessageSigner>,
),
) {
let (mut context, mut p2p_receiver, message_signer) = setup_test.await;
let mut certificates = create_certificate_chain(SOURCE_SUBNET_ID_1, &[TARGET_SUBNET_ID_1], 2);
let certificate = certificates.pop().unwrap().certificate;

let (sender, receiver) = oneshot::channel();

context
.on_api_event(topos_tce_api::RuntimeEvent::CertificateSubmitted {
certificate: Box::new(certificate),
sender,
})
.await;

let response = receiver.await;

assert!(matches!(response, Ok(Ok(PendingResult::AwaitPrecedence))));
}

#[rstest]
#[test(tokio::test)]
async fn handle_certificate_already_delivered(
#[future] setup_test: (
AppContext,
mpsc::Receiver<topos_p2p::Command>,
Arc<MessageSigner>,
),
) {
let (mut context, mut p2p_receiver, message_signer) = setup_test.await;
let mut certificates = create_certificate_chain(SOURCE_SUBNET_ID_1, &[TARGET_SUBNET_ID_1], 1);
let certificate_delivered = certificates.pop().unwrap();

_ = context
.validator_store
.insert_certificate_delivered(&certificate_delivered)
.await
.unwrap();

let certificate = certificate_delivered.certificate;

let (sender, receiver) = oneshot::channel();

context
.on_api_event(topos_tce_api::RuntimeEvent::CertificateSubmitted {
certificate: Box::new(certificate),
sender,
})
.await;

let response = receiver.await;

assert!(matches!(response, Ok(Ok(PendingResult::AlreadyDelivered))));
}
5 changes: 4 additions & 1 deletion crates/topos-tce/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use topos_test_sdk::{

use crate::AppContext;

mod api;
mod network;

#[rstest]
#[tokio::test]
async fn non_validator_publish_gossip(
Expand Down Expand Up @@ -91,7 +94,7 @@ async fn non_validator_do_not_publish_ready(
}

#[fixture]
async fn setup_test(
pub async fn setup_test(
#[future] create_validator_store: Arc<ValidatorStore>,
#[future] create_public_api: (PublicApiContext, impl Stream<Item = RuntimeEvent>),
) -> (
Expand Down
101 changes: 101 additions & 0 deletions crates/topos-tce/src/tests/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::sync::Arc;

use libp2p::PeerId;
use prost::Message;
use rstest::rstest;
use test_log::test;
use tokio::sync::mpsc;
use topos_core::api::grpc::tce::v1::{double_echo_request, DoubleEchoRequest, Echo, Gossip, Ready};
use topos_crypto::{messages::MessageSigner, validator_id::ValidatorId};
use topos_test_sdk::{
certificates::create_certificate_chain,
constants::{SOURCE_SUBNET_ID_1, TARGET_SUBNET_ID_1},
};

use crate::AppContext;

use super::setup_test;

#[rstest]
#[test(tokio::test)]
async fn handle_gossip(
#[future] setup_test: (
AppContext,
mpsc::Receiver<topos_p2p::Command>,
Arc<MessageSigner>,
),
) {
let (mut context, mut p2p_receiver, message_signer) = setup_test.await;
let mut certificates = create_certificate_chain(SOURCE_SUBNET_ID_1, &[TARGET_SUBNET_ID_1], 1);
let certificate = certificates.pop().unwrap().certificate;

let msg = DoubleEchoRequest {
request: Some(double_echo_request::Request::Gossip(Gossip {
certificate: Some(certificate.into()),
})),
};
context
.on_net_event(topos_p2p::Event::Gossip {
from: PeerId::random(),
data: msg.encode_to_vec(),
})
.await;
}

#[rstest]
#[test(tokio::test)]
async fn handle_echo(
#[future] setup_test: (
AppContext,
mpsc::Receiver<topos_p2p::Command>,
Arc<MessageSigner>,
),
) {
let (mut context, mut p2p_receiver, message_signer) = setup_test.await;
let mut certificates = create_certificate_chain(SOURCE_SUBNET_ID_1, &[TARGET_SUBNET_ID_1], 1);
let certificate = certificates.pop().unwrap().certificate;
let validator_id: ValidatorId = message_signer.public_address.into();

let msg = DoubleEchoRequest {
request: Some(double_echo_request::Request::Echo(Echo {
certificate_id: Some(certificate.id.into()),
signature: Some(message_signer.sign_message(&[]).ok().unwrap().into()),
validator_id: Some(validator_id.into()),
})),
};
context
.on_net_event(topos_p2p::Event::Gossip {
from: PeerId::random(),
data: msg.encode_to_vec(),
})
.await;
}

#[rstest]
#[test(tokio::test)]
async fn handle_ready(
#[future] setup_test: (
AppContext,
mpsc::Receiver<topos_p2p::Command>,
Arc<MessageSigner>,
),
) {
let (mut context, mut p2p_receiver, message_signer) = setup_test.await;
let mut certificates = create_certificate_chain(SOURCE_SUBNET_ID_1, &[TARGET_SUBNET_ID_1], 1);
let certificate = certificates.pop().unwrap().certificate;
let validator_id: ValidatorId = message_signer.public_address.into();

let msg = DoubleEchoRequest {
request: Some(double_echo_request::Request::Ready(Ready {
certificate_id: Some(certificate.id.into()),
signature: Some(message_signer.sign_message(&[]).ok().unwrap().into()),
validator_id: Some(validator_id.into()),
})),
};
context
.on_net_event(topos_p2p::Event::Gossip {
from: PeerId::random(),
data: msg.encode_to_vec(),
})
.await;
}

0 comments on commit 08aca99

Please sign in to comment.