connection_utils/test/
test_options.rs

1use std::ops::RangeInclusive;
2use cs_utils::{random_number, traits::Random};
3
4/// Options for `test_framed_stream`/`test_async_stream` utilities.
5#[derive(Debug, Clone)]
6pub struct TestOptions {
7    /// Number of `messages` or `bytes` to send.
8    data_len: usize,
9    /// Latency value(min..=max) for each stream message transfer in milliseconds.
10    latency_range: RangeInclusive<u64>,
11}
12
13impl TestOptions {
14    /// Sets the `latency_range` value.
15    pub fn with_latency(
16        self,
17        latency_range: RangeInclusive<u64>,
18    ) -> TestOptions {
19        return TestOptions {
20            latency_range,
21            ..self
22        }
23    }
24
25    /// Sets the  `data_len` value.
26    pub fn with_data_len(
27        self,
28        items_count: usize,
29    ) -> TestOptions {
30        return TestOptions {
31            data_len: items_count,
32            ..self
33        }
34    }
35
36    /// Gets the `items_count` value.
37    pub fn data_len(&self) -> usize {
38        return self.data_len;
39    }
40
41    /// Gets the `latency_range` value.
42    pub fn latency_range(&self) -> RangeInclusive<u64> {
43        return self.latency_range.clone();
44    }
45}
46
47impl Default for TestOptions {
48    fn default() -> TestOptions {
49        return TestOptions {
50            data_len: 128,
51            latency_range: (0..=0),
52        };
53    }
54}
55
56impl Random for TestOptions {
57    fn random() -> TestOptions {
58        let min: u64 = random_number(0..5);
59        let max: u64 = random_number(5..50);
60    
61        return TestOptions {
62            data_len: random_number(128..=256),
63            latency_range: (min..=max),
64        };
65    }
66}