tor_proto/congestion/
test_utils.rs

1//! Test helpers.
2
3#[cfg(test)]
4use super::{rtt::RoundtripTimeEstimator, CongestionWindow};
5
6// Make a new RTT estimator.
7#[cfg(test)]
8pub(crate) fn new_rtt_estimator() -> RoundtripTimeEstimator {
9    RoundtripTimeEstimator::new(&params::build_rtt_params())
10}
11
12// Make a new congestion window.
13#[cfg(test)]
14pub(crate) fn new_cwnd() -> CongestionWindow {
15    CongestionWindow::new(&params::build_cwnd_params())
16}
17
18#[cfg(test)]
19pub(crate) mod params {
20    use tor_units::Percentage;
21
22    use crate::ccparams::{
23        Algorithm, CongestionControlParams, CongestionControlParamsBuilder, CongestionWindowParams,
24        CongestionWindowParamsBuilder, FixedWindowParams, FixedWindowParamsBuilder,
25        RoundTripEstimatorParams, RoundTripEstimatorParamsBuilder, VegasParamsBuilder,
26    };
27
28    fn build_fixed_params() -> FixedWindowParams {
29        FixedWindowParamsBuilder::default()
30            .circ_window_start(1000)
31            .circ_window_min(100)
32            .circ_window_max(1000)
33            .build()
34            .expect("Unable to build fixed window params")
35    }
36
37    pub(crate) fn build_cc_vegas_params() -> CongestionControlParams {
38        // Following values are the default from the proposal. They likely differ from what the
39        // consensus uses today.
40        let params = VegasParamsBuilder::default()
41            .cell_in_queue_params((186, 248, 310, 186, 600).into())
42            .ss_cwnd_max(5000)
43            .cwnd_full_gap(4)
44            .cwnd_full_min_pct(Percentage::new(25))
45            .cwnd_full_per_cwnd(1)
46            .build()
47            .expect("Unable to build Vegas params");
48        CongestionControlParamsBuilder::default()
49            .rtt_params(build_rtt_params())
50            .cwnd_params(build_cwnd_params())
51            .alg(Algorithm::Vegas(params.clone()))
52            .fallback_alg(Algorithm::FixedWindow(build_fixed_params()))
53            .build()
54            .expect("Unable to build CC params")
55    }
56
57    pub(crate) fn build_cc_fixed_params() -> CongestionControlParams {
58        let params = build_fixed_params();
59        CongestionControlParamsBuilder::default()
60            .rtt_params(build_rtt_params())
61            .cwnd_params(build_cwnd_params())
62            .alg(Algorithm::FixedWindow(params.clone()))
63            .fallback_alg(Algorithm::FixedWindow(params))
64            .build()
65            .expect("Unable to build CC params")
66    }
67
68    // Build the round trip estimator parameters. with good enough values for tests.
69    pub(crate) fn build_rtt_params() -> RoundTripEstimatorParams {
70        RoundTripEstimatorParamsBuilder::default()
71            .ewma_cwnd_pct(Percentage::new(50))
72            .ewma_max(10)
73            .ewma_ss_max(2)
74            .rtt_reset_pct(Percentage::new(100))
75            .build()
76            .expect("Unable to build RTT parameters")
77    }
78
79    // Build the congestion window parameters. with good enough values for tests.
80    pub(crate) fn build_cwnd_params() -> CongestionWindowParams {
81        // Values taken from the prop324.
82        CongestionWindowParamsBuilder::default()
83            .cwnd_init(124)
84            .cwnd_inc_pct_ss(Percentage::new(100))
85            .cwnd_inc(1)
86            .cwnd_inc_rate(31)
87            .cwnd_min(124)
88            .cwnd_max(u32::MAX)
89            .sendme_inc(31)
90            .build()
91            .expect("Unable to build congestion window parameters")
92    }
93}