use tet_libp2p_core::{muxing, upgrade, Transport};
use tet_libp2p_tcp::TcpConfig;
use futures::{channel::oneshot, prelude::*};
use std::sync::Arc;
#[test]
fn client_to_server_outbound() {
let (tx, rx) = oneshot::channel();
let bg_thread = async_std::task::spawn(async move {
let mplex = tet_libp2p_mplex::MplexConfig::new();
let transport = TcpConfig::new().and_then(move |c, e|
upgrade::apply(c, mplex, e, upgrade::Version::V1));
let mut listener = transport
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
.unwrap();
let addr = listener.next().await
.expect("some event")
.expect("no error")
.into_new_address()
.expect("listen address");
tx.send(addr).unwrap();
let client = listener
.next().await
.unwrap()
.unwrap()
.into_upgrade().unwrap().0.await.unwrap();
let mut outbound = muxing::outbound_from_ref_and_wrap(Arc::new(client)).await.unwrap();
let mut buf = Vec::new();
outbound.read_to_end(&mut buf).await.unwrap();
assert_eq!(buf, b"hello world");
});
async_std::task::block_on(async {
let mplex = tet_libp2p_mplex::MplexConfig::new();
let transport = TcpConfig::new().and_then(move |c, e|
upgrade::apply(c, mplex, e, upgrade::Version::V1));
let client = Arc::new(transport.dial(rx.await.unwrap()).unwrap().await.unwrap());
let mut inbound = loop {
if let Some(s) = muxing::event_from_ref_and_wrap(client.clone()).await.unwrap()
.into_inbound_substream() {
break s;
}
};
inbound.write_all(b"hello world").await.unwrap();
inbound.close().await.unwrap();
bg_thread.await;
});
}
#[test]
fn client_to_server_inbound() {
let (tx, rx) = oneshot::channel();
let bg_thread = async_std::task::spawn(async move {
let mplex = tet_libp2p_mplex::MplexConfig::new();
let transport = TcpConfig::new().and_then(move |c, e|
upgrade::apply(c, mplex, e, upgrade::Version::V1));
let mut listener = transport
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
.unwrap();
let addr = listener.next().await
.expect("some event")
.expect("no error")
.into_new_address()
.expect("listen address");
tx.send(addr).unwrap();
let client = Arc::new(listener
.next().await
.unwrap()
.unwrap()
.into_upgrade().unwrap().0.await.unwrap());
let mut inbound = loop {
if let Some(s) = muxing::event_from_ref_and_wrap(client.clone()).await.unwrap()
.into_inbound_substream() {
break s;
}
};
let mut buf = Vec::new();
inbound.read_to_end(&mut buf).await.unwrap();
assert_eq!(buf, b"hello world");
});
async_std::task::block_on(async {
let mplex = tet_libp2p_mplex::MplexConfig::new();
let transport = TcpConfig::new().and_then(move |c, e|
upgrade::apply(c, mplex, e, upgrade::Version::V1));
let client = transport.dial(rx.await.unwrap()).unwrap().await.unwrap();
let mut outbound = muxing::outbound_from_ref_and_wrap(Arc::new(client)).await.unwrap();
outbound.write_all(b"hello world").await.unwrap();
outbound.close().await.unwrap();
bg_thread.await;
});
}