Skip to content

Commit a26561c

Browse files
sirhceleldruin
authored andcommitted
Add first integration tests for read timeouts
1 parent fb9cc1e commit a26561c

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

tests/test_timeout.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
mod config;
2+
3+
use config::{hw_config, HardwareConfig};
4+
use rstest::rstest;
5+
use serialport::*;
6+
use std::io::Read;
7+
use std::time::{Duration, Instant};
8+
9+
#[rstest]
10+
#[case(b"a")]
11+
#[case(b"0123456789")]
12+
#[case(b"0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")]
13+
#[cfg_attr(feature = "ignore-hardware-tests", ignore)]
14+
fn test_timeout_zero(hw_config: HardwareConfig, #[case] message: &[u8]) {
15+
let timeout = Duration::ZERO;
16+
let margin = Duration::from_millis(100);
17+
18+
let mut sender = serialport::new(hw_config.port_1, 115200).open().unwrap();
19+
let mut receiver = serialport::new(hw_config.port_2, 115200)
20+
.timeout(timeout)
21+
.open()
22+
.unwrap();
23+
let mut buffer: [u8; 1024] = [0xff; 1024];
24+
25+
sender.clear(ClearBuffer::All).unwrap();
26+
receiver.clear(ClearBuffer::All).unwrap();
27+
28+
sender.write_all(message).unwrap();
29+
sender.flush().unwrap();
30+
let flushed_at = Instant::now();
31+
32+
let expected_until = flushed_at + timeout + margin;
33+
let mut timeouts = 0usize;
34+
35+
loop {
36+
match receiver.read(&mut buffer) {
37+
Ok(read) => {
38+
assert!(read > 0);
39+
println!(
40+
"read: {} bytes of {} after {} timeouts/{} ms",
41+
read,
42+
message.len(),
43+
timeouts,
44+
(Instant::now() - flushed_at).as_millis()
45+
);
46+
assert_eq!(message[..read], buffer[..read]);
47+
break;
48+
}
49+
Err(e) => {
50+
assert_eq!(e.kind(), std::io::ErrorKind::TimedOut);
51+
timeouts += 1;
52+
}
53+
}
54+
55+
assert!(expected_until > Instant::now());
56+
}
57+
}
58+
59+
#[rstest]
60+
#[case(Duration::from_millis(10))]
61+
#[case(Duration::from_millis(100))]
62+
#[case(Duration::from_millis(1000))]
63+
#[cfg_attr(feature = "ignore-hardware-tests", ignore)]
64+
fn test_timeout_greater_zero(hw_config: HardwareConfig, #[case] timeout: Duration) {
65+
let margin = Duration::from_millis(100);
66+
67+
let mut sender = serialport::new(hw_config.port_1, 115200).open().unwrap();
68+
let mut receiver = serialport::new(hw_config.port_2, 115200)
69+
.timeout(timeout)
70+
.open()
71+
.unwrap();
72+
73+
let message =
74+
b"0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
75+
let mut buffer: [u8; 1024] = [0xff; 1024];
76+
77+
sender.clear(ClearBuffer::All).unwrap();
78+
receiver.clear(ClearBuffer::All).unwrap();
79+
80+
sender.write_all(message).unwrap();
81+
sender.flush().unwrap();
82+
83+
let flushed_at = Instant::now();
84+
85+
let read = receiver.read(&mut buffer).unwrap();
86+
let read_at = Instant::now();
87+
88+
println!(
89+
"read: {} bytes of {} after {} ms",
90+
read,
91+
message.len(),
92+
(Instant::now() - flushed_at).as_millis()
93+
);
94+
95+
assert!(read > 0);
96+
assert!(flushed_at + timeout + margin > read_at);
97+
assert_eq!(buffer[..read], message[..read]);
98+
}

0 commit comments

Comments
 (0)