Skip to content

Commit 0aa5a68

Browse files
committed
feat(model): support transition to endless model
- add `Forever` trait to do the transition work Resolves: #9
1 parent ea57c95 commit 0aa5a68

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

.github/workflows/cargo-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
uses: BobAnkh/clippy-check@master
8383
with:
8484
token: ${{ secrets.GITHUB_TOKEN }}
85-
args: --all-features --workspace
85+
args: --all-targets --all-features --workspace --no-deps
8686

8787
cargo-fmt:
8888
runs-on: ubuntu-latest

src/lib.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
//! }
8080
//! ```
8181
//!
82-
//! This is almost the same as how this library implements the [`StaticBw`] model.
82+
//! This is almost the same as how this library implements the [`model::StaticBw`] model.
8383
//!
8484
//! ## Features
8585
//!
@@ -179,6 +179,8 @@ pub trait LossTrace: Send {
179179

180180
#[cfg(test)]
181181
mod test {
182+
use self::model::bw::Forever;
183+
182184
use super::*;
183185
use crate::model::{
184186
BwTraceConfig, NormalizedBwConfig, RepeatedBwPatternConfig, SawtoothBwConfig,
@@ -337,4 +339,48 @@ mod test {
337339
Some((Bandwidth::from_mbps(12), Duration::from_secs(1)))
338340
);
339341
}
342+
343+
#[test]
344+
fn test_forever() {
345+
let mut normal_bw = NormalizedBwConfig::new()
346+
.mean(Bandwidth::from_mbps(12))
347+
.std_dev(Bandwidth::from_mbps(1))
348+
.duration(Duration::from_millis(200))
349+
.step(Duration::from_millis(100))
350+
.seed(42)
351+
.build();
352+
assert_eq!(
353+
normal_bw.next_bw(),
354+
Some((Bandwidth::from_bps(12069427), Duration::from_millis(100)))
355+
);
356+
assert_eq!(
357+
normal_bw.next_bw(),
358+
Some((Bandwidth::from_bps(12132938), Duration::from_millis(100)))
359+
);
360+
assert_eq!(normal_bw.next_bw(), None);
361+
let normal_bw_config = NormalizedBwConfig::new()
362+
.mean(Bandwidth::from_mbps(12))
363+
.std_dev(Bandwidth::from_mbps(1))
364+
.duration(Duration::from_millis(200))
365+
.step(Duration::from_millis(100))
366+
.seed(42);
367+
let normal_bw_repeated = normal_bw_config.forever();
368+
let mut model = Box::new(normal_bw_repeated).into_model();
369+
assert_eq!(
370+
model.next_bw(),
371+
Some((Bandwidth::from_bps(12069427), Duration::from_millis(100)))
372+
);
373+
assert_eq!(
374+
model.next_bw(),
375+
Some((Bandwidth::from_bps(12132938), Duration::from_millis(100)))
376+
);
377+
assert_eq!(
378+
model.next_bw(),
379+
Some((Bandwidth::from_bps(12069427), Duration::from_millis(100)))
380+
);
381+
assert_eq!(
382+
model.next_bw(),
383+
Some((Bandwidth::from_bps(12132938), Duration::from_millis(100)))
384+
);
385+
}
340386
}

src/model/bw.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,3 +791,32 @@ impl_bw_trace_config!(StaticBwConfig);
791791
impl_bw_trace_config!(NormalizedBwConfig);
792792
impl_bw_trace_config!(SawtoothBwConfig);
793793
impl_bw_trace_config!(RepeatedBwPatternConfig);
794+
795+
/// Turn a [`BwTraceConfig`] into a forever repeated [`RepeatedBwPatternConfig`].
796+
pub trait Forever: BwTraceConfig {
797+
fn forever(self) -> RepeatedBwPatternConfig;
798+
}
799+
800+
/// Implement the [`Forever`] trait for the bandwidth trace model config (any struct implements [`BwTraceConfig`]).
801+
#[macro_export]
802+
macro_rules! impl_forever {
803+
($name:ident) => {
804+
impl Forever for $name {
805+
fn forever(self) -> RepeatedBwPatternConfig {
806+
RepeatedBwPatternConfig::new()
807+
.pattern(vec![Box::new(self)])
808+
.count(0)
809+
}
810+
}
811+
};
812+
}
813+
814+
impl_forever!(StaticBwConfig);
815+
impl_forever!(NormalizedBwConfig);
816+
impl_forever!(SawtoothBwConfig);
817+
818+
impl Forever for RepeatedBwPatternConfig {
819+
fn forever(self) -> RepeatedBwPatternConfig {
820+
self.count(0)
821+
}
822+
}

src/model/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ pub mod bw;
1515

1616
#[cfg(feature = "bw-model")]
1717
pub use bw::{
18-
BwTraceConfig, NormalizedBwConfig, RepeatedBwPatternConfig, SawtoothBwConfig, StaticBwConfig,
18+
BwTraceConfig, Forever, NormalizedBwConfig, RepeatedBwPatternConfig, SawtoothBwConfig,
19+
StaticBwConfig,
1920
};
2021
#[cfg(feature = "bw-model")]
2122
pub use bw::{NormalizedBw, RepeatedBwPattern, SawtoothBw, StaticBw};

0 commit comments

Comments
 (0)