Closed ustulation closed 5 years ago
As expected the 2nd test case fails on Windows and Mac OS: https://travis-ci.org/maidsafe/socket-collection/builds/461290797?utm_source=github_status&utm_medium=notification I'm removing this test, but it was:
#[test]
fn connected_socket_can_use_write_and_write_to_simultaneously() {
const SOCKET1_TOKEN: Token = Token(0);
const SOCKET2_TOKEN: Token = Token(1);
const SOCKET3_TOKEN: Token = Token(2);
const TIMEOUT_TOKEN: Token = Token(3);
let mut sock2 = unwrap!(UdpSock::bind(&unwrap!("127.0.0.1:0".parse())));
let sock2_addr = unwrap!(sock2.local_addr());
let mut sock1 = unwrap!(UdpSock::bind(&unwrap!("127.0.0.1:0".parse())));
unwrap!(sock1.connect(&sock2_addr));
let sock1_addr = unwrap!(sock1.local_addr());
let mut sock3 = unwrap!(UdpSock::bind(&unwrap!("127.0.0.1:0".parse())));
let sock3_addr = unwrap!(sock3.local_addr());
let poll = unwrap!(Poll::new());
unwrap!(poll.register(&sock1, SOCKET1_TOKEN, Ready::writable(), PollOpt::edge()));
unwrap!(poll.register(&sock2, SOCKET2_TOKEN, Ready::readable(), PollOpt::edge()));
unwrap!(poll.register(&sock3, SOCKET3_TOKEN, Ready::readable(), PollOpt::edge()));
let mut timer = Timer::default();
timer.set_timeout(Duration::from_secs(1), ()); // let's terminate the test after 1 second
unwrap!(poll.register(&timer, TIMEOUT_TOKEN, Ready::readable(), PollOpt::edge(),));
let mut sock2_received_from = None;
let mut sock3_received_from = None;
let mut retry_send = false;
let mut events = Events::with_capacity(1024);
'event_loop: loop {
let _ = unwrap!(poll.poll(&mut events, None));
for event in events.iter() {
match event.token() {
SOCKET1_TOKEN => {
if retry_send {
let _ = unwrap!(sock1.write::<Vec<u8>>(None));
} else {
let sent1 = unwrap!(sock1.write(Some((vec![1u8, 2, 3], 1))));
let sent2 = unwrap!(sock1.write_to(Some((vec![1u8, 2, 3], sock3_addr, 1))));
if !sent1 || !sent2 {
retry_send = true;
} else {
unwrap!(poll.deregister(&sock1));
}
}
}
SOCKET2_TOKEN => {
let res: Option<(Vec<u8>, _)> = unwrap!(sock2.read_frm());
sock2_received_from = res.map(|(_data, peer_addr)| peer_addr);
}
SOCKET3_TOKEN => {
let res: Option<(Vec<u8>, _)> = unwrap!(sock3.read_frm());
sock3_received_from = res.map(|(_data, peer_addr)| peer_addr);
}
TIMEOUT_TOKEN => break 'event_loop,
_ => panic!("Unexpected event"),
}
}
}
assert_that!(sock2_received_from, eq(Some(sock1_addr)));
assert_that!(sock3_received_from, eq(Some(sock1_addr)));
}
Done.
send()
and Sock-2 should dosend_to()
ofc.send()
and Sock-1 rx it. Simultaneously Sock-0 should be able tosend_to()
to Sock-2 and Sock-2 should rx it too. So while sending a connected socket (Sock-0) should be able to send to anyone but while rx only receive it from Sock-1. This behaviour might fail as I think connected Sock-0 will be disallowed to send to Sock-2 (because it cannot rx from it so OS stack might prevent sending to someone you can't rx from).