Skip to content

Commit

Permalink
Improve error handling in non-wasm integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ndebuhr committed Mar 10, 2022
1 parent c1f77c1 commit 8aec089
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 116 deletions.
4 changes: 4 additions & 0 deletions sim/src/utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ pub enum SimulationError {
#[error("Failed to convert to a Float value")]
FloatConvError,

/// Represents a message unexpectedly lost/dropped/stuck during simulation execution
#[error("A message was unexpectedly lost, dropped, or stuck during simulation execution")]
DroppedMessageError,

/// Transparent serde_json errors
#[error(transparent)]
JSONError(#[from] serde_json::error::Error),
Expand Down
110 changes: 56 additions & 54 deletions sim/tests/coupled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use sim::models::{
};
use sim::output_analysis::{ConfidenceInterval, SteadyStateOutput};
use sim::simulator::{Connector, Message, Simulation};
use sim::utils::errors::SimulationError;

fn get_message_number(message: &str) -> &str {
message.split_whitespace().last().unwrap()
fn get_message_number(message: &str) -> Option<&str> {
message.split_whitespace().last()
}

#[test]
fn closure_under_coupling() {
fn closure_under_coupling() -> Result<(), SimulationError> {
let atomic_models = vec![
Model::new(
String::from("generator-01"),
Expand Down Expand Up @@ -137,58 +138,58 @@ fn closure_under_coupling() {
]
.iter()
.enumerate()
.map(|(index, (models, connectors))| {
let mut simulation = Simulation::post(models.to_vec(), connectors.to_vec());
let message_records: Vec<Message> = simulation.step_n(1000).unwrap();
let arrivals: Vec<(&f64, &str)>;
let departures: Vec<(&f64, &str)>;
match index {
0 => {
arrivals = message_records
.iter()
.filter(|message_record| message_record.target_id() == "processor-01")
.map(|message_record| (message_record.time(), message_record.content()))
.collect();
departures = message_records
.iter()
.filter(|message_record| message_record.target_id() == "storage-01")
.map(|message_record| (message_record.time(), message_record.content()))
.collect();
}
_ => {
arrivals = message_records
.iter()
.filter(|message_record| message_record.target_id() == "storage-02")
.filter(|message_record| message_record.source_port() == "start")
.map(|message_record| (message_record.time(), message_record.content()))
.collect();
departures = message_records
.iter()
.filter(|message_record| message_record.target_id() == "storage-02")
.filter(|message_record| message_record.source_port() == "stop")
.map(|message_record| (message_record.time(), message_record.content()))
.collect();
}
}
let response_times: Vec<f64> = departures
.iter()
.map(|departure| {
departure.0
- arrivals
.map(
|(index, (models, connectors))| -> Result<ConfidenceInterval<f64>, SimulationError> {
let mut simulation = Simulation::post(models.to_vec(), connectors.to_vec());
let message_records: Vec<Message> = simulation.step_n(1000)?;
let arrivals: Vec<(&f64, &str)>;
let departures: Vec<(&f64, &str)>;
match index {
0 => {
arrivals = message_records
.iter()
.filter(|message_record| message_record.target_id() == "processor-01")
.map(|message_record| (message_record.time(), message_record.content()))
.collect();
departures = message_records
.iter()
.find(|arrival| {
get_message_number(&arrival.1) == get_message_number(&departure.1)
})
.unwrap()
.0
})
.collect();
let mut response_times_sample = SteadyStateOutput::post(response_times);
response_times_sample
.confidence_interval_mean(0.001)
.unwrap()
})
.collect();
.filter(|message_record| message_record.target_id() == "storage-01")
.map(|message_record| (message_record.time(), message_record.content()))
.collect();
}
_ => {
arrivals = message_records
.iter()
.filter(|message_record| message_record.target_id() == "storage-02")
.filter(|message_record| message_record.source_port() == "start")
.map(|message_record| (message_record.time(), message_record.content()))
.collect();
departures = message_records
.iter()
.filter(|message_record| message_record.target_id() == "storage-02")
.filter(|message_record| message_record.source_port() == "stop")
.map(|message_record| (message_record.time(), message_record.content()))
.collect();
}
}
let response_times: Vec<f64> = departures
.iter()
.map(|departure| -> Result<f64, SimulationError> {
Ok(departure.0
- arrivals
.iter()
.find(|arrival| {
get_message_number(&arrival.1) == get_message_number(&departure.1)
})
.ok_or(SimulationError::DroppedMessageError)?
.0)
})
.collect::<Result<Vec<f64>, SimulationError>>()?;
let mut response_times_sample = SteadyStateOutput::post(response_times);
response_times_sample.confidence_interval_mean(0.001)
},
)
.collect::<Result<Vec<ConfidenceInterval<f64>>, SimulationError>>()?;
// Ensure confidence intervals overlap
assert![
response_times_confidence_intervals[0].lower()
Expand All @@ -198,4 +199,5 @@ fn closure_under_coupling() {
response_times_confidence_intervals[1].lower()
< response_times_confidence_intervals[0].upper()
];
Ok(())
}
5 changes: 3 additions & 2 deletions sim/tests/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Reportable for Passive {
impl ReportableModel for Passive {}

#[test]
fn step_n_with_custom_passive_model() {
fn step_n_with_custom_passive_model() -> Result<(), SimulationError> {
let models = [
Model::new(
String::from("generator-01"),
Expand All @@ -111,10 +111,11 @@ fn step_n_with_custom_passive_model() {
)];
let mut simulation = Simulation::post(models.to_vec(), connectors.to_vec());
// 1 initialization event, and 2 events per generation
let messages = simulation.step_n(9).unwrap();
let messages = simulation.step_n(9)?;
let generations_count = messages.len();
let expected = 4; // 4 interarrivals from 9 steps
assert_eq!(generations_count, expected);
Ok(())
}

#[test]
Expand Down
Loading

0 comments on commit 8aec089

Please sign in to comment.