Function channel_on

Source
pub fn channel_on(address: impl ToSocketAddrs) -> (TcpStream, TcpStream)
Expand description

Returns two TCP streams pointing at each other.

The internal TCP listener is bound to address. Only one listener is used throughout the entire program, so the address should match in all calls to this function, otherwise it is not specified which address is finally used.

ยงExample

use tcp_test::channel_on;
use std::io::{Read, Write};

#[test]
fn test() {
    let data = b"Hello world!";
    let (mut local, mut remote) = channel_on("127.0.0.1:31399");

    assert_eq!(local.peer_addr().unwrap(), "127.0.0.1:31399".parse().unwrap());
    assert_eq!(remote.local_addr().unwrap(), "127.0.0.1:31399".parse().unwrap());

    local.write_all(data).unwrap();

    let mut buf = [0; 12];
    remote.read_exact(&mut buf).unwrap();

    assert_eq!(&buf, data);
}