Skip to content

Commit

Permalink
Add PingResult DTO for data consuming and unit tests for file loggers. (
Browse files Browse the repository at this point in the history
  • Loading branch information
r12f authored Jul 21, 2021
1 parent 9096fe5 commit 2295930
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
log.*
target
Cargo.lock
Cargo.lock
tests_data
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ chrono = "0.4"
rand = "0.8"
thiserror = "1.0.26"
async-trait = "0.1.50"
serde = { version = "1.0", features = ["derive"] }

[target.'cfg(any(not(target_os = "windows"), not(target_arch = "aarch64")))'.dependencies]
quinn = "0.7.2"
Expand All @@ -33,6 +34,8 @@ webpki = "0.21"
tide = "0.16.0"
async-std = "1.9.0"
pretty_assertions = "0.7.2"
serde_json = "1.0"
csv = "1.1"

[profile.release]
opt-level = 'z' # Optimize for size.
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod ping_worker;
mod rnp_core;
mod rnp_core_config;
mod rnp_utils;
mod rnp_dto;

#[cfg(test)]
mod rnp_test_utils;
mod rnp_test_common;
20 changes: 10 additions & 10 deletions src/ping_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl PingResult {
});

let json = format!(
"{{\"utcTime\":\"{:?}\",\"protocol\":\"{}\",\"workerId\":{},\"targetIP\":\"{}\",\"targetPort\":\"{}\",\"sourceIP\":\"{}\",\"sourcePort\":\"{}\",\"isWarmup\":\"{}\",\"isSucceeded\":\"{}\",\"roundTripTimeInMs\":{:.2},\"isTimedOut\":\"{}\",\"preparationError\":\"{}\",\"pingError\":\"{}\",\"handshakeError\":\"{}\"}}",
"{{\"utcTime\":\"{:?}\",\"protocol\":\"{}\",\"workerId\":{},\"targetIp\":\"{}\",\"targetPort\":{},\"sourceIp\":\"{}\",\"sourcePort\":{},\"isWarmup\":{},\"isSucceeded\":{},\"rttInMs\":{:.2},\"isTimedOut\":{},\"preparationError\":\"{}\",\"pingError\":\"{}\",\"handshakeError\":\"{}\"}}",
self.ping_time(),
self.protocol(),
self.worker_id(),
Expand Down Expand Up @@ -228,7 +228,7 @@ mod tests {
use pretty_assertions::assert_eq;
use std::net::SocketAddr;
use std::time::Duration;
use crate::rnp_test_utils;
use crate::rnp_test_common;

#[test]
fn new_ping_result_should_work() {
Expand Down Expand Up @@ -259,7 +259,7 @@ mod tests {

#[test]
fn format_ping_result_as_log_should_work() {
let results = rnp_test_utils::generate_ping_result_test_samples();
let results = rnp_test_common::generate_ping_result_test_samples();
assert_eq!(
vec![
"Reaching TCP 1.2.3.4:443 from 5.6.7.8:8080 (warmup) succeeded: RTT=10.00ms",
Expand All @@ -277,22 +277,22 @@ mod tests {

#[test]
fn format_ping_result_as_json_should_work() {
let results = rnp_test_utils::generate_ping_result_test_samples();
let results = rnp_test_common::generate_ping_result_test_samples();
assert_eq!(
vec![
"{\"utcTime\":\"2021-07-06T09:10:11.012Z\",\"protocol\":\"TCP\",\"workerId\":1,\"targetIP\":\"1.2.3.4\",\"targetPort\":\"443\",\"sourceIP\":\"5.6.7.8\",\"sourcePort\":\"8080\",\"isWarmup\":\"true\",\"isSucceeded\":\"true\",\"roundTripTimeInMs\":10.00,\"isTimedOut\":\"false\",\"preparationError\":\"\",\"pingError\":\"\",\"handshakeError\":\"\"}",
"{\"utcTime\":\"2021-07-06T09:10:11.012Z\",\"protocol\":\"TCP\",\"workerId\":1,\"targetIP\":\"1.2.3.4\",\"targetPort\":\"443\",\"sourceIP\":\"5.6.7.8\",\"sourcePort\":\"8080\",\"isWarmup\":\"false\",\"isSucceeded\":\"false\",\"roundTripTimeInMs\":1000.00,\"isTimedOut\":\"true\",\"preparationError\":\"\",\"pingError\":\"\",\"handshakeError\":\"\"}",
"{\"utcTime\":\"2021-07-06T09:10:11.012Z\",\"protocol\":\"TCP\",\"workerId\":1,\"targetIP\":\"1.2.3.4\",\"targetPort\":\"443\",\"sourceIP\":\"5.6.7.8\",\"sourcePort\":\"8080\",\"isWarmup\":\"false\",\"isSucceeded\":\"true\",\"roundTripTimeInMs\":20.00,\"isTimedOut\":\"false\",\"preparationError\":\"\",\"pingError\":\"\",\"handshakeError\":\"connect aborted\"}",
"{\"utcTime\":\"2021-07-06T09:10:11.012Z\",\"protocol\":\"TCP\",\"workerId\":1,\"targetIP\":\"1.2.3.4\",\"targetPort\":\"443\",\"sourceIP\":\"5.6.7.8\",\"sourcePort\":\"8080\",\"isWarmup\":\"false\",\"isSucceeded\":\"false\",\"roundTripTimeInMs\":0.00,\"isTimedOut\":\"false\",\"preparationError\":\"\",\"pingError\":\"connect failed\",\"handshakeError\":\"\"}",
"{\"utcTime\":\"2021-07-06T09:10:11.012Z\",\"protocol\":\"TCP\",\"workerId\":1,\"targetIP\":\"1.2.3.4\",\"targetPort\":\"443\",\"sourceIP\":\"5.6.7.8\",\"sourcePort\":\"8080\",\"isWarmup\":\"false\",\"isSucceeded\":\"false\",\"roundTripTimeInMs\":0.00,\"isTimedOut\":\"false\",\"preparationError\":\"address in use\",\"pingError\":\"\",\"handshakeError\":\"\"}",
"{\"utcTime\":\"2021-07-06T09:10:11.012Z\",\"protocol\":\"TCP\",\"workerId\":1,\"targetIp\":\"1.2.3.4\",\"targetPort\":443,\"sourceIp\":\"5.6.7.8\",\"sourcePort\":8080,\"isWarmup\":true,\"isSucceeded\":true,\"rttInMs\":10.00,\"isTimedOut\":false,\"preparationError\":\"\",\"pingError\":\"\",\"handshakeError\":\"\"}",
"{\"utcTime\":\"2021-07-06T09:10:11.012Z\",\"protocol\":\"TCP\",\"workerId\":1,\"targetIp\":\"1.2.3.4\",\"targetPort\":443,\"sourceIp\":\"5.6.7.8\",\"sourcePort\":8080,\"isWarmup\":false,\"isSucceeded\":false,\"rttInMs\":1000.00,\"isTimedOut\":true,\"preparationError\":\"\",\"pingError\":\"\",\"handshakeError\":\"\"}",
"{\"utcTime\":\"2021-07-06T09:10:11.012Z\",\"protocol\":\"TCP\",\"workerId\":1,\"targetIp\":\"1.2.3.4\",\"targetPort\":443,\"sourceIp\":\"5.6.7.8\",\"sourcePort\":8080,\"isWarmup\":false,\"isSucceeded\":true,\"rttInMs\":20.00,\"isTimedOut\":false,\"preparationError\":\"\",\"pingError\":\"\",\"handshakeError\":\"connect aborted\"}",
"{\"utcTime\":\"2021-07-06T09:10:11.012Z\",\"protocol\":\"TCP\",\"workerId\":1,\"targetIp\":\"1.2.3.4\",\"targetPort\":443,\"sourceIp\":\"5.6.7.8\",\"sourcePort\":8080,\"isWarmup\":false,\"isSucceeded\":false,\"rttInMs\":0.00,\"isTimedOut\":false,\"preparationError\":\"\",\"pingError\":\"connect failed\",\"handshakeError\":\"\"}",
"{\"utcTime\":\"2021-07-06T09:10:11.012Z\",\"protocol\":\"TCP\",\"workerId\":1,\"targetIp\":\"1.2.3.4\",\"targetPort\":443,\"sourceIp\":\"5.6.7.8\",\"sourcePort\":8080,\"isWarmup\":false,\"isSucceeded\":false,\"rttInMs\":0.00,\"isTimedOut\":false,\"preparationError\":\"address in use\",\"pingError\":\"\",\"handshakeError\":\"\"}",
],
results.into_iter().map(|x| x.format_as_json_string()).collect::<Vec<String>>()
);
}

#[test]
fn format_ping_result_as_csv_should_work() {
let results = rnp_test_utils::generate_ping_result_test_samples();
let results = rnp_test_common::generate_ping_result_test_samples();
assert_eq!(
vec![
"2021-07-06T09:10:11.012Z,1,TCP,1.2.3.4,443,5.6.7.8,8080,true,true,10.00,false,\"\",\"\",\"\"",
Expand Down
3 changes: 3 additions & 0 deletions src/ping_result_processors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ mod ping_result_processor_text_logger;
mod ping_result_processor_result_scatter_logger;
mod ping_result_processor_latency_scatter_logger;
mod ping_result_processor_latency_bucket_logger;

#[cfg(test)]
mod ping_result_processor_test_common;
117 changes: 115 additions & 2 deletions src/ping_result_processors/ping_result_processor_csv_logger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ping_result_processors::ping_result_processor::PingResultProcessor;
use crate::{rnp_utils, PingResult};
use std::{fs::File, io::prelude::*, path::PathBuf, io};
use std::{fs::File, io, io::prelude::*, path::PathBuf};
use tracing;

pub struct PingResultProcessorCsvLogger {
Expand Down Expand Up @@ -29,7 +29,7 @@ impl PingResultProcessor for PingResultProcessorCsvLogger {
fn initialize(&mut self) {
// Writer CSV header
self.log_file
.write("UTCTime,WorkerId,Protocol,TargetIP,TargetPort,SourceIP,SourcePort,IsWarmup,IsSucceeded,RTTInMs,IsTimedOut,PreparationError,PingError,HandshakeError\n".as_bytes())
.write("UtcTime,WorkerId,Protocol,TargetIp,TargetPort,SourceIp,SourcePort,IsWarmup,IsSucceeded,RttInMs,IsTimedOut,PreparationError,PingError,HandshakeError\n".as_bytes())
.expect(&format!(
"Failed to write logs to csv file! Path = {}",
self.log_path.display()
Expand All @@ -43,3 +43,116 @@ impl PingResultProcessor for PingResultProcessorCsvLogger {
));
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::ping_result_processors::ping_result_processor_test_common;
use crate::rnp_dto::PingResultCsvDto;
use pretty_assertions::assert_eq;
use chrono::{Utc, TimeZone};

#[test]
fn ping_result_process_csv_logger_should_work() {
let test_log_file_path = "tests_data\\test_log.csv";
let mut processor: Box<dyn PingResultProcessor + Send + Sync> = Box::new(PingResultProcessorCsvLogger::new(&PathBuf::from(test_log_file_path)));
ping_result_processor_test_common::run_ping_result_processor_with_test_samples(
&mut processor,
);

let mut actual_logged_records = Vec::new();
{
let mut csv_reader = csv::Reader::from_path(test_log_file_path).unwrap();
for result in csv_reader.deserialize() {
let actual_record: PingResultCsvDto = result.unwrap();
actual_logged_records.push(actual_record);
}
}

assert_eq!(
vec![
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: true,
is_succeeded: true,
is_timed_out: false,
rtt_in_ms: 10f64,
preparation_error: "".to_string(),
ping_error: "".to_string(),
handshake_error: "".to_string()
},
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: false,
is_timed_out: true,
rtt_in_ms: 1000f64,
preparation_error: "".to_string(),
ping_error: "".to_string(),
handshake_error: "".to_string()
},
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: true,
is_timed_out: false,
rtt_in_ms: 20f64,
preparation_error: "".to_string(),
ping_error: "".to_string(),
handshake_error: "connect aborted".to_string()
},
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: false,
is_timed_out: false,
rtt_in_ms: 0f64,
preparation_error: "".to_string(),
ping_error: "connect failed".to_string(),
handshake_error: "".to_string()
},
PingResultCsvDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: false,
is_timed_out: false,
rtt_in_ms: 0f64,
preparation_error: "address in use".to_string(),
ping_error: "".to_string(),
handshake_error: "".to_string()
},
],
actual_logged_records,
);
}
}
112 changes: 112 additions & 0 deletions src/ping_result_processors/ping_result_processor_json_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,115 @@ impl PingResultProcessor for PingResultProcessorJsonLogger {
));
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::ping_result_processors::ping_result_processor_test_common;
use crate::rnp_dto::PingResultJsonDto;
use pretty_assertions::assert_eq;
use chrono::{Utc, TimeZone};
use std::io::BufReader;

#[test]
fn ping_result_process_json_logger_should_work() {
let test_log_file_path = "tests_data\\test_log.json";
let mut processor: Box<dyn PingResultProcessor + Send + Sync> = Box::new(PingResultProcessorJsonLogger::new(&PathBuf::from(test_log_file_path)));
ping_result_processor_test_common::run_ping_result_processor_with_test_samples(
&mut processor,
);

let actual_logged_records : Vec<PingResultJsonDto>;
{
let test_log_file = File::open(test_log_file_path).unwrap();
let test_log_reader = BufReader::new(test_log_file);
actual_logged_records = serde_json::from_reader(test_log_reader).unwrap();
}

assert_eq!(
vec![
PingResultJsonDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: true,
is_succeeded: true,
is_timed_out: false,
rtt_in_ms: 10f64,
preparation_error: "".to_string(),
ping_error: "".to_string(),
handshake_error: "".to_string()
},
PingResultJsonDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: false,
is_timed_out: true,
rtt_in_ms: 1000f64,
preparation_error: "".to_string(),
ping_error: "".to_string(),
handshake_error: "".to_string()
},
PingResultJsonDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: true,
is_timed_out: false,
rtt_in_ms: 20f64,
preparation_error: "".to_string(),
ping_error: "".to_string(),
handshake_error: "connect aborted".to_string()
},
PingResultJsonDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: false,
is_timed_out: false,
rtt_in_ms: 0f64,
preparation_error: "".to_string(),
ping_error: "connect failed".to_string(),
handshake_error: "".to_string()
},
PingResultJsonDto {
utc_time: Utc.ymd(2021, 7, 6).and_hms_milli(9, 10, 11, 12),
worker_id: 1,
protocol: "TCP".to_string(),
target_ip: "1.2.3.4".parse().unwrap(),
target_port: 443,
source_ip: "5.6.7.8".parse().unwrap(),
source_port: 8080,
is_warmup: false,
is_succeeded: false,
is_timed_out: false,
rtt_in_ms: 0f64,
preparation_error: "address in use".to_string(),
ping_error: "".to_string(),
handshake_error: "".to_string()
},
],
actual_logged_records,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ impl PingResultProcessor for PingResultProcessorLatencyBucketLogger {
#[cfg(test)]
mod tests {
use super::*;
use crate::rnp_test_utils;
use crate::rnp_test_common;

#[test]
fn latency_bucket_logger_should_work() {
let ping_results = rnp_test_utils::generate_ping_result_test_samples();
let ping_results = rnp_test_common::generate_ping_result_test_samples();

let mut logger =
PingResultProcessorLatencyBucketLogger::new(&vec![0.1, 0.5, 1.0, 10.0, 50.0, 100.0]);
Expand Down
15 changes: 15 additions & 0 deletions src/ping_result_processors/ping_result_processor_test_common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::rnp_test_common;
use crate::ping_result_processors::ping_result_processor::PingResultProcessor;

pub fn run_ping_result_processor_with_test_samples(
processor: &mut Box<dyn PingResultProcessor + Send + Sync>,
)
{
let ping_results = rnp_test_common::generate_ping_result_test_samples();

processor.initialize();
for ping_result in &ping_results {
processor.process_ping_result(&ping_result);
}
processor.rundown();
}
Loading

0 comments on commit 2295930

Please sign in to comment.