comparison tests/comm.rs @ 15:08b37039504b

Improved tests * Use `localhost` for connecting which makes sure to resolve to the same address as `bind` does. * Check for slave crashing before attempting to connect. * Improve error message.
author Martin Habovstiak <martin.habovstiak@gmail.com>
date Tue, 22 Dec 2020 13:56:56 +0100
parents 372afb9a700f
children 1941e9d9819c
comparison
equal deleted inserted replaced
14:2039b5d39d72 15:08b37039504b
10 10
11 const REQUEST: &[u8] = b"orange coin"; 11 const REQUEST: &[u8] = b"orange coin";
12 const RESPONSE: &[u8] = b"good"; 12 const RESPONSE: &[u8] = b"good";
13 13
14 fn main_master(slave: io::Result<Child>) { 14 fn main_master(slave: io::Result<Child>) {
15 let mut slave = slave.expect("failed to run systemd-socket-activate"); 15 let mut slave = slave.expect("failed to run a child");
16 16
17 // give slave some time to bind the socket just to be sure 17 // give slave some time to bind the socket just to be sure
18 std::thread::sleep(std::time::Duration::from_secs(5)); 18 std::thread::sleep(std::time::Duration::from_secs(5));
19 19
20 let mut client_socket = std::net::TcpStream::connect("127.0.0.1:4242").expect("Failed to connect to 127.0.0.1:4242"); 20 if let Some(exited) = slave.try_wait().expect("failed to check if the child exited") {
21 panic!("child exited unexpectedly: {}", exited);
22 }
23
24 let mut client_socket = std::net::TcpStream::connect("localhost:4242").expect("Failed to connect to 127.0.0.1:4242");
21 client_socket.write_all(REQUEST).expect("failed to send data"); 25 client_socket.write_all(REQUEST).expect("failed to send data");
22 let mut buf = [0u8; RESPONSE.len()]; 26 let mut buf = [0u8; RESPONSE.len()];
23 client_socket.read_exact(&mut buf).expect("failed to read response"); 27 client_socket.read_exact(&mut buf).expect("failed to read response");
24 assert_eq!(buf, RESPONSE); 28 assert_eq!(buf, RESPONSE);
25 29