Skip to content

Commit 6713c6f

Browse files
authored
Merge pull request #143 from sr-gi/20231017-adds-tests
sim-all: Adds tests coverage to `random_activity`
2 parents 1270a16 + 4ad2105 commit 6713c6f

File tree

6 files changed

+347
-25
lines changed

6 files changed

+347
-25
lines changed

Cargo.lock

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sim-cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ struct Cli {
3939
#[clap(long, short, verbatim_doc_comment, default_value = "info")]
4040
log_level: LevelFilter,
4141
/// Expected payment amount for the random activity generator
42-
#[clap(long, short, default_value_t = EXPECTED_PAYMENT_AMOUNT)]
42+
#[clap(long, short, default_value_t = EXPECTED_PAYMENT_AMOUNT, value_parser = clap::builder::RangedU64ValueParser::<u64>::new().range(1..u64::MAX))]
4343
expected_pmt_amt: u64,
4444
/// Multiplier of the overall network capacity used by the random activity generator
45-
#[clap(long, short, default_value_t = ACTIVITY_MULTIPLIER)]
45+
#[clap(long, short, default_value_t = ACTIVITY_MULTIPLIER, value_parser = clap::builder::RangedU64ValueParser::<u32>::new().range(1..u64::MAX))]
4646
capacity_multiplier: f64,
4747
/// Do not create an output file containing the simulations results
4848
#[clap(long, default_value_t = false)]

sim-lib/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ hex = "0.4.3"
2929
csv = "1.2.2"
3030
serde_millis = "0.1.1"
3131
rand_distr = "0.4.3"
32+
33+
[dev-dependencies]
34+
ntest = "0.9.0"

sim-lib/src/lib.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use bitcoin::Network;
44
use csv::WriterBuilder;
55
use lightning::ln::features::NodeFeatures;
66
use lightning::ln::PaymentHash;
7+
use random_activity::RandomActivityError;
78
use serde::{Deserialize, Serialize};
89
use std::collections::HashSet;
910
use std::fmt::{Display, Formatter};
@@ -23,6 +24,8 @@ pub mod cln;
2324
pub mod lnd;
2425
mod random_activity;
2526
mod serializers;
27+
#[cfg(test)]
28+
mod test_utils;
2629

2730
#[derive(Serialize, Deserialize, Debug, Clone)]
2831
#[serde(untagged)]
@@ -125,8 +128,8 @@ pub enum SimulationError {
125128
CsvError(#[from] csv::Error),
126129
#[error("File Error")]
127130
FileError,
128-
#[error("Random activity Error: {0}")]
129-
RandomActivityError(String),
131+
#[error("{0}")]
132+
RandomActivityError(RandomActivityError),
130133
}
131134

132135
// Phase 2: Event Queue
@@ -203,12 +206,15 @@ pub trait NetworkGenerator {
203206
fn sample_node_by_capacity(&self, source: PublicKey) -> (NodeInfo, u64);
204207
}
205208

209+
#[derive(Debug, Error)]
210+
#[error("Payment generation error: {0}")]
211+
pub struct PaymentGenerationError(String);
206212
pub trait PaymentGenerator {
207213
// Returns the number of seconds that a node should wait until firing its next payment.
208214
fn next_payment_wait(&self) -> time::Duration;
209215

210216
// Returns a payment amount based on the capacity of the sending and receiving node.
211-
fn payment_amount(&self, destination_capacity: u64) -> Result<u64, SimulationError>;
217+
fn payment_amount(&self, destination_capacity: u64) -> Result<u64, PaymentGenerationError>;
212218
}
213219

214220
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -642,9 +648,10 @@ impl Simulation {
642648
producer_channels: HashMap<PublicKey, Sender<SimulationEvent>>,
643649
tasks: &mut JoinSet<()>,
644650
) -> Result<(), SimulationError> {
645-
let network_generator = Arc::new(Mutex::new(NetworkGraphView::new(
646-
node_capacities.values().cloned().collect(),
647-
)?));
651+
let network_generator = Arc::new(Mutex::new(
652+
NetworkGraphView::new(node_capacities.values().cloned().collect())
653+
.map_err(SimulationError::RandomActivityError)?,
654+
));
648655

649656
log::info!(
650657
"Created network generator: {}.",
@@ -655,18 +662,21 @@ impl Simulation {
655662
let (info, source_capacity) = match node_capacities.get(&pk) {
656663
Some((info, capacity)) => (info.clone(), *capacity),
657664
None => {
658-
return Err(SimulationError::RandomActivityError(format!(
659-
"Random activity generator run for: {} with unknown capacity.",
660-
pk
661-
)));
665+
return Err(SimulationError::RandomActivityError(
666+
RandomActivityError::ValueError(format!(
667+
"Random activity generator run for: {} with unknown capacity.",
668+
pk
669+
)),
670+
));
662671
}
663672
};
664673

665674
let node_generator = PaymentActivityGenerator::new(
666675
source_capacity,
667676
self.expected_payment_msat,
668677
self.activity_multiplier,
669-
)?;
678+
)
679+
.map_err(SimulationError::RandomActivityError)?;
670680

671681
tasks.spawn(produce_random_events(
672682
info,

0 commit comments

Comments
 (0)