You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.4 KiB
41 lines
1.4 KiB
use futures::StreamExt;
|
|
use std::net::ToSocketAddrs;
|
|
use tokio::net::tcp::{TcpListener, TcpStream};
|
|
use tokio_tungstenite::{accept_async, client_async};
|
|
|
|
#[tokio::test]
|
|
async fn handshakes() {
|
|
let (tx, rx) = futures::channel::oneshot::channel();
|
|
|
|
let f = async move {
|
|
let address = "0.0.0.0:12345"
|
|
.to_socket_addrs()
|
|
.expect("Not a valid address")
|
|
.next()
|
|
.expect("No address resolved");
|
|
let listener = TcpListener::bind(&address).await.unwrap();
|
|
let mut connections = listener.incoming();
|
|
tx.send(()).unwrap();
|
|
while let Some(connection) = connections.next().await {
|
|
let connection = connection.expect("Failed to accept connection");
|
|
let stream = accept_async(connection).await;
|
|
stream.expect("Failed to handshake with connection");
|
|
}
|
|
};
|
|
|
|
tokio::spawn(f);
|
|
|
|
rx.await.expect("Failed to wait for server to be ready");
|
|
let address = "0.0.0.0:12345"
|
|
.to_socket_addrs()
|
|
.expect("Not a valid address")
|
|
.next()
|
|
.expect("No address resolved");
|
|
let tcp = TcpStream::connect(&address)
|
|
.await
|
|
.expect("Failed to connect");
|
|
let url = url::Url::parse("ws://localhost:12345/").unwrap();
|
|
let _stream = client_async(url, tcp)
|
|
.await
|
|
.expect("Client failed to connect");
|
|
}
|
|
|