use dambi::Bytes;
use dope_runtime::{DefaultBackend, block_on};
use dope_udp::udp_socket::UdpSocket;

#[test]
fn udp_roundtrip() {
    block_on(async {
        let a = UdpSocket::<DefaultBackend>::bind("127.0.0.1:0").expect("bind a");
        let b = UdpSocket::<DefaultBackend>::bind("127.0.0.1:0").expect("bind b");

        let a_addr = a.local_addr().expect("a local_addr");
        let b_addr = b.local_addr().expect("b local_addr");

        a.connect(b_addr).expect("a connect");
        b.connect(a_addr).expect("b connect");

        let sent = a
            .send_bytes(Bytes::from_static(b"ping"))
            .await
            .expect("send");
        assert_eq!(sent, 4);

        let got = b.recv(64).await.expect("recv");
        assert_eq!(got.as_slice(), b"ping");

        let sent = b
            .send_bytes(Bytes::from_static(b"pong"))
            .await
            .expect("send2");
        assert_eq!(sent, 4);

        let got = a.recv(64).await.expect("recv2");
        assert_eq!(got.as_slice(), b"pong");
    });
}
