1
//! Test helpers.
2

            
3
#[cfg(test)]
4
use super::{CongestionWindow, rtt::RoundtripTimeEstimator};
5

            
6
// Make a new RTT estimator.
7
#[cfg(test)]
8
10
pub(crate) fn new_rtt_estimator() -> RoundtripTimeEstimator {
9
10
    RoundtripTimeEstimator::new(&params::build_rtt_params())
10
10
}
11

            
12
// Make a new congestion window.
13
#[cfg(test)]
14
28
pub(crate) fn new_cwnd() -> CongestionWindow {
15
28
    CongestionWindow::new(&params::build_cwnd_params())
16
28
}
17

            
18
#[cfg(test)]
19
pub(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
380
    fn build_fixed_params() -> FixedWindowParams {
29
380
        FixedWindowParamsBuilder::default()
30
380
            .circ_window_start(1000)
31
380
            .circ_window_min(100)
32
380
            .circ_window_max(1000)
33
380
            .build()
34
380
            .expect("Unable to build fixed window params")
35
380
    }
36

            
37
62
    pub(crate) fn build_cc_vegas_params() -> CongestionControlParams {
38
62
        // Following values are the default from the proposal. They likely differ from what the
39
62
        // consensus uses today.
40
62
        let params = VegasParamsBuilder::default()
41
62
            .cell_in_queue_params((186, 248, 310, 186, 600).into())
42
62
            .ss_cwnd_max(5000)
43
62
            .cwnd_full_gap(4)
44
62
            .cwnd_full_min_pct(Percentage::new(25))
45
62
            .cwnd_full_per_cwnd(1)
46
62
            .build()
47
62
            .expect("Unable to build Vegas params");
48
62
        CongestionControlParamsBuilder::default()
49
62
            .rtt_params(build_rtt_params())
50
62
            .cwnd_params(build_cwnd_params())
51
62
            .alg(Algorithm::Vegas(params))
52
62
            .fixed_window_params(build_fixed_params())
53
62
            .build()
54
62
            .expect("Unable to build CC params")
55
62
    }
56

            
57
318
    pub(crate) fn build_cc_fixed_params() -> CongestionControlParams {
58
318
        let params = build_fixed_params();
59
318
        CongestionControlParamsBuilder::default()
60
318
            .rtt_params(build_rtt_params())
61
318
            .cwnd_params(build_cwnd_params())
62
318
            .alg(Algorithm::FixedWindow(params))
63
318
            .fixed_window_params(params)
64
318
            .build()
65
318
            .expect("Unable to build CC params")
66
318
    }
67

            
68
    // Build the round trip estimator parameters. with good enough values for tests.
69
390
    pub(crate) fn build_rtt_params() -> RoundTripEstimatorParams {
70
390
        RoundTripEstimatorParamsBuilder::default()
71
390
            .ewma_cwnd_pct(Percentage::new(50))
72
390
            .ewma_max(10)
73
390
            .ewma_ss_max(2)
74
390
            .rtt_reset_pct(Percentage::new(100))
75
390
            .build()
76
390
            .expect("Unable to build RTT parameters")
77
390
    }
78

            
79
    // Build the congestion window parameters. with good enough values for tests.
80
408
    pub(crate) fn build_cwnd_params() -> CongestionWindowParams {
81
408
        // Values taken from the prop324.
82
408
        CongestionWindowParamsBuilder::default()
83
408
            .cwnd_init(124)
84
408
            .cwnd_inc_pct_ss(Percentage::new(100))
85
408
            .cwnd_inc(1)
86
408
            .cwnd_inc_rate(31)
87
408
            .cwnd_min(124)
88
408
            .cwnd_max(u32::MAX)
89
408
            .sendme_inc(31)
90
408
            .build()
91
408
            .expect("Unable to build congestion window parameters")
92
408
    }
93
}