netspeed_cli/
test_config.rs1#[derive(Debug, Clone)]
21pub struct TestConfig {
22 pub download_rounds: usize,
24
25 pub upload_rounds: usize,
27
28 pub stream_count: usize,
30
31 pub sample_interval_ms: u64,
33
34 pub ping_attempts: usize,
36
37 pub upload_payload_bytes: usize,
39
40 pub estimated_download_bytes: u64,
42
43 pub estimated_upload_bytes: u64,
45
46 pub latency_poll_interval_ms: u64,
48
49 pub http_retry_attempts: usize,
51
52 pub http_retry_base_ms: u64,
54
55 pub http_retry_max_ms: u64,
57}
58
59impl Default for TestConfig {
60 fn default() -> Self {
61 Self {
62 download_rounds: 4,
64 upload_rounds: 4,
65
66 stream_count: 4,
68
69 sample_interval_ms: 50,
71
72 ping_attempts: 8,
74
75 upload_payload_bytes: 200_000,
77
78 estimated_download_bytes: 15_000_000, estimated_upload_bytes: 4_000_000, latency_poll_interval_ms: 100,
84
85 http_retry_attempts: 3,
87 http_retry_base_ms: 100,
88 http_retry_max_ms: 5000,
89 }
90 }
91}
92
93impl TestConfig {
94 #[must_use]
96 pub fn stream_count_for(single: bool) -> usize {
97 if single {
98 1
99 } else {
100 Self::default().stream_count
101 }
102 }
103
104 #[must_use]
107 pub fn retry_delay(attempt: usize) -> (u64, bool) {
108 let config = Self::default();
109 if attempt >= config.http_retry_attempts {
110 return (0, false);
111 }
112 let delay = config.http_retry_base_ms * 2u64.saturating_pow(attempt as u32);
114 let delay = delay.min(config.http_retry_max_ms);
115 (delay, true)
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[test]
124 fn test_default_values() {
125 let config = TestConfig::default();
126 assert_eq!(config.download_rounds, 4);
127 assert_eq!(config.upload_rounds, 4);
128 assert_eq!(config.stream_count, 4);
129 assert_eq!(config.sample_interval_ms, 50);
130 assert_eq!(config.ping_attempts, 8);
131 assert_eq!(config.upload_payload_bytes, 200_000);
132 assert_eq!(config.estimated_download_bytes, 15_000_000);
133 assert_eq!(config.estimated_upload_bytes, 4_000_000);
134 assert_eq!(config.http_retry_attempts, 3);
135 }
136
137 #[test]
138 fn test_stream_count_for_single() {
139 assert_eq!(TestConfig::stream_count_for(true), 1);
140 }
141
142 #[test]
143 fn test_stream_count_for_multi() {
144 assert_eq!(TestConfig::stream_count_for(false), 4);
145 }
146
147 #[test]
148 fn test_retry_delay_first_attempt() {
149 let (delay, should_retry) = TestConfig::retry_delay(0);
150 assert!(should_retry);
151 assert_eq!(delay, 100); }
153
154 #[test]
155 fn test_retry_delay_second_attempt() {
156 let (delay, should_retry) = TestConfig::retry_delay(1);
157 assert!(should_retry);
158 assert_eq!(delay, 200); }
160
161 #[test]
162 fn test_retry_delay_third_attempt() {
163 let (delay, should_retry) = TestConfig::retry_delay(2);
164 assert!(should_retry);
165 assert_eq!(delay, 400); }
167
168 #[test]
169 fn test_retry_delay_exhausted() {
170 let (_, should_retry) = TestConfig::retry_delay(3);
171 assert!(!should_retry);
172 }
173
174 #[test]
175 fn test_retry_delay_beyond_max_attempts() {
176 let (delay, should_retry) = TestConfig::retry_delay(10);
178 assert!(!should_retry, "attempt 10 is beyond max retry attempts");
179 assert_eq!(delay, 0, "delay is 0 when retries exhausted");
180 }
181}