use bytes::Bytes;
use monocoque::rt::{LocalRuntime, TcpListener};
use monocoque::zmq::{PullSocket, PushSocket, SocketOptions};
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
const ADDR: &str = "127.0.0.1:0";
const PAYLOAD: &[u8] = b"eager-invariant-exact-bytes";
#[test]
fn eager_send_reaches_peer_with_no_further_sender_action() {
let (port_tx, port_rx) = mpsc::channel::<u16>();
let (sent_tx, sent_rx) = mpsc::channel::<()>();
let (release_tx, release_rx) = mpsc::channel::<()>();
let sender = thread::spawn(move || {
let rt = LocalRuntime::new().unwrap();
rt.block_on(async move {
let port = port_rx.recv().unwrap();
let mut push = PushSocket::connect_with_options(
("127.0.0.1", port),
SocketOptions::default().with_buffer_sizes(16384, 16384),
)
.await
.unwrap();
push.send(vec![Bytes::from_static(PAYLOAD)]).await.unwrap();
sent_tx.send(()).unwrap();
release_rx.recv().unwrap();
drop(push);
});
});
let rt = LocalRuntime::new().unwrap();
rt.block_on(async move {
let listener = TcpListener::bind(ADDR).await.unwrap();
port_tx.send(listener.local_addr().unwrap().port()).unwrap();
let (stream, _) = listener.accept().await.unwrap();
let mut pull = PullSocket::from_tcp_with_options(
stream,
SocketOptions::default().with_buffer_sizes(16384, 16384),
)
.await
.unwrap();
sent_rx.recv().unwrap();
let msg = pull
.recv()
.await
.unwrap()
.expect("eager send must deliver the message with no further sender action");
assert_eq!(msg.len(), 1, "expected a single-frame message");
assert_eq!(
msg[0].as_ref(),
PAYLOAD,
"peer must observe the exact bytes the sender enqueued"
);
release_tx.send(()).unwrap();
});
for _ in 0..50 {
if sender.is_finished() {
break;
}
thread::sleep(Duration::from_millis(20));
}
assert!(
sender.is_finished(),
"sender did not finish; eager delivery may not be self-sufficient"
);
sender.join().unwrap();
}