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

adding more tests #37

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rocksdb = "0.22.0"
byte-unit = "5.1.6"
getset = "0.1.3"
chrono = "0.4"
tempfile = "3.15.0"

[dev-dependencies]
rstest = "0.23.0"
Expand Down
39 changes: 22 additions & 17 deletions src/primary/header_elector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ mod test {
identity::ed25519::{self, Keypair},
PeerId,
};
use rstest::rstest;
use tempfile::tempdir;
use tokio::{
sync::{broadcast, mpsc, watch},
time::timeout,
Expand All @@ -152,9 +152,8 @@ mod test {

use crate::{
db::{Column, Db},
primary::test_utils::fixtures::{
load_committee, random_digests, CHANNEL_CAPACITY, COMMITTEE_PATH, GENESIS_SEED,
},
primary::test_utils::fixtures::{random_digests, CHANNEL_CAPACITY},
settings::parser::Committee,
types::{
block_header::BlockHeader,
certificate::{Certificate, CertificateId},
Expand All @@ -178,18 +177,28 @@ mod test {
mpsc::Receiver<NetworkRequest>,
Arc<Db>,
CancellationToken,
tempfile::TempDir,
);

fn launch_header_elector(committee_path: String, db_path: &str) -> HeaderElectorFixutre {
async fn launch_header_elector() -> HeaderElectorFixutre {
let (headers_tx, headers_rx) = broadcast::channel(CHANNEL_CAPACITY);
let (round_tx, round_rx) = watch::channel((0, HashSet::new()));
let (incomplete_headers_tx, incomplete_headers_rx) = mpsc::channel(CHANNEL_CAPACITY);
let (network_tx, network_rx) = mpsc::channel(CHANNEL_CAPACITY);
let db = Arc::new(Db::new(db_path.into()).unwrap());

// Create a temporary directory for the test database
let temp_dir = tempdir().unwrap();
let db_path = temp_dir.path().join("test.db");

let db = Arc::new(Db::new(db_path).unwrap());

let validator_keypair = ed25519::Keypair::generate();
let token = CancellationToken::new();
let db_clone = db.clone();
let token_clone = token.clone();

let committee = Committee::new_test();

tokio::spawn(async move {
HeaderElector::spawn(
token_clone,
Expand All @@ -198,19 +207,21 @@ mod test {
round_rx,
validator_keypair,
db_clone,
load_committee(&committee_path),
committee,
incomplete_headers_tx,
)
.await
.unwrap()
});

(
headers_tx,
round_tx,
incomplete_headers_rx,
network_rx,
db,
token,
temp_dir,
)
}

Expand Down Expand Up @@ -259,21 +270,15 @@ mod test {
let vote_status = vote.verify(&header_hash);
assert!(vote_status.is_ok());
}
_ => {
assert!(false);
}
_ => assert!(false),
}
}

#[tokio::test]
#[rstest]
async fn test_first_round_valid_header_digests_stored() {
let (headers_tx, round_state_tx, _incomplete_headers_rx, network_rx, db, _) =
launch_header_elector(
COMMITTEE_PATH.into(),
"/tmp/test_first_round_valid_header_digests_stored_db",
);
let genesis = Certificate::genesis(GENESIS_SEED);
let (headers_tx, round_state_tx, _incomplete_headers_rx, network_rx, db, _, _temp_dir) =
launch_header_elector().await;
let genesis = Certificate::genesis([0; 32]);
let header = random_header(&[genesis.id()], 1);
set_header_storage_in_db(&header, &db);
set_certificates_in_db(&[genesis.clone()], &db);
Expand Down
5 changes: 5 additions & 0 deletions src/primary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,8 @@ impl BaseAgent for Primary {
}
}
}

#[cfg(test)]
mod tests {
mod header_tests;
}
Loading