Update multistream-select to stable futures (#1484)

* Update multistream-select to stable futures

* Fix intradoc links
This commit is contained in:
Pierre Krieger
2020-03-11 14:49:41 +01:00
committed by GitHub
parent 2084fadd86
commit 31271fc824
11 changed files with 851 additions and 652 deletions

View File

@ -25,164 +25,156 @@
use crate::{Version, NegotiationError};
use crate::dialer_select::{dialer_select_proto_parallel, dialer_select_proto_serial};
use crate::{dialer_select_proto, listener_select_proto};
use async_std::net::{TcpListener, TcpStream};
use futures::prelude::*;
use tokio::runtime::current_thread::Runtime;
use tokio_tcp::{TcpListener, TcpStream};
use tokio_io::io as nio;
#[test]
fn select_proto_basic() {
fn run(version: Version) {
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
async fn run(version: Version) {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let listener_addr = listener.local_addr().unwrap();
let server = listener
.incoming()
.into_future()
.map(|s| s.0.unwrap())
.map_err(|(e, _)| e.into())
.and_then(move |connec| {
let protos = vec![b"/proto1", b"/proto2"];
listener_select_proto(connec, protos)
})
.and_then(|(proto, io)| {
nio::write_all(io, b"pong").from_err().map(move |_| proto)
});
let server = async_std::task::spawn(async move {
let connec = listener.accept().await.unwrap().0;
let protos = vec![b"/proto1", b"/proto2"];
let (proto, mut io) = listener_select_proto(connec, protos).await.unwrap();
assert_eq!(proto, b"/proto2");
let client = TcpStream::connect(&listener_addr)
.from_err()
.and_then(move |connec| {
let protos = vec![b"/proto3", b"/proto2"];
dialer_select_proto(connec, protos, version)
})
.and_then(|(proto, io)| {
nio::write_all(io, b"ping").from_err().map(move |(io, _)| (proto, io))
})
.and_then(|(proto, io)| {
nio::read_exact(io, [0; 4]).from_err().map(move |(_, msg)| {
assert_eq!(&msg, b"pong");
proto
})
});
let mut out = vec![0; 32];
let n = io.read(&mut out).await.unwrap();
out.truncate(n);
assert_eq!(out, b"ping");
let mut rt = Runtime::new().unwrap();
let (dialer_chosen, listener_chosen) =
rt.block_on(client.join(server)).unwrap();
io.write_all(b"pong").await.unwrap();
io.flush().await.unwrap();
});
assert_eq!(dialer_chosen, b"/proto2");
assert_eq!(listener_chosen, b"/proto2");
let client = async_std::task::spawn(async move {
let connec = TcpStream::connect(&listener_addr).await.unwrap();
let protos = vec![b"/proto3", b"/proto2"];
let (proto, mut io) = dialer_select_proto(connec, protos.into_iter(), version)
.await.unwrap();
assert_eq!(proto, b"/proto2");
io.write_all(b"ping").await.unwrap();
io.flush().await.unwrap();
let mut out = vec![0; 32];
let n = io.read(&mut out).await.unwrap();
out.truncate(n);
assert_eq!(out, b"pong");
});
server.await;
client.await;
}
run(Version::V1);
run(Version::V1Lazy);
async_std::task::block_on(run(Version::V1));
async_std::task::block_on(run(Version::V1Lazy));
}
#[test]
fn no_protocol_found() {
fn run(version: Version) {
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
async fn run(version: Version) {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let listener_addr = listener.local_addr().unwrap();
let server = listener
.incoming()
.into_future()
.map(|s| s.0.unwrap())
.map_err(|(e, _)| e.into())
.and_then(move |connec| {
let protos = vec![b"/proto1", b"/proto2"];
listener_select_proto(connec, protos)
})
.and_then(|(proto, io)| io.complete().map(move |_| proto));
let server = async_std::task::spawn(async move {
let connec = listener.accept().await.unwrap().0;
let protos = vec![b"/proto1", b"/proto2"];
let io = match listener_select_proto(connec, protos).await {
Ok((_, io)) => io,
// We don't explicitly check for `Failed` because the client might close the connection when it
// realizes that we have no protocol in common.
Err(_) => return,
};
match io.complete().await {
Err(NegotiationError::Failed) => {},
_ => panic!(),
}
});
let client = TcpStream::connect(&listener_addr)
.from_err()
.and_then(move |connec| {
let protos = vec![b"/proto3", b"/proto4"];
dialer_select_proto(connec, protos, version)
})
.and_then(|(proto, io)| io.complete().map(move |_| proto));
let client = async_std::task::spawn(async move {
let connec = TcpStream::connect(&listener_addr).await.unwrap();
let protos = vec![b"/proto3", b"/proto4"];
let io = match dialer_select_proto(connec, protos.into_iter(), version).await {
Err(NegotiationError::Failed) => return,
Ok((_, io)) => io,
Err(_) => panic!()
};
match io.complete().await {
Err(NegotiationError::Failed) => {},
_ => panic!(),
}
});
let mut rt = Runtime::new().unwrap();
match rt.block_on(client.join(server)) {
Err(NegotiationError::Failed) => (),
e => panic!("{:?}", e),
}
server.await;
client.await;
}
run(Version::V1);
run(Version::V1Lazy);
async_std::task::block_on(run(Version::V1));
async_std::task::block_on(run(Version::V1Lazy));
}
#[test]
fn select_proto_parallel() {
fn run(version: Version) {
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
async fn run(version: Version) {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let listener_addr = listener.local_addr().unwrap();
let server = listener
.incoming()
.into_future()
.map(|s| s.0.unwrap())
.map_err(|(e, _)| e.into())
.and_then(move |connec| {
let protos = vec![b"/proto1", b"/proto2"];
listener_select_proto(connec, protos)
})
.and_then(|(proto, io)| io.complete().map(move |_| proto));
let server = async_std::task::spawn(async move {
let connec = listener.accept().await.unwrap().0;
let protos = vec![b"/proto1", b"/proto2"];
let (proto, io) = listener_select_proto(connec, protos).await.unwrap();
assert_eq!(proto, b"/proto2");
io.complete().await.unwrap();
});
let client = TcpStream::connect(&listener_addr)
.from_err()
.and_then(move |connec| {
let protos = vec![b"/proto3", b"/proto2"];
dialer_select_proto_parallel(connec, protos.into_iter(), version)
})
.and_then(|(proto, io)| io.complete().map(move |_| proto));
let client = async_std::task::spawn(async move {
let connec = TcpStream::connect(&listener_addr).await.unwrap();
let protos = vec![b"/proto3", b"/proto2"];
let (proto, io) = dialer_select_proto_parallel(connec, protos.into_iter(), version)
.await.unwrap();
assert_eq!(proto, b"/proto2");
io.complete().await.unwrap();
});
let mut rt = Runtime::new().unwrap();
let (dialer_chosen, listener_chosen) =
rt.block_on(client.join(server)).unwrap();
assert_eq!(dialer_chosen, b"/proto2");
assert_eq!(listener_chosen, b"/proto2");
server.await;
client.await;
}
run(Version::V1);
run(Version::V1Lazy);
async_std::task::block_on(run(Version::V1));
async_std::task::block_on(run(Version::V1Lazy));
}
#[test]
fn select_proto_serial() {
fn run(version: Version) {
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
async fn run(version: Version) {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let listener_addr = listener.local_addr().unwrap();
let server = listener
.incoming()
.into_future()
.map(|s| s.0.unwrap())
.map_err(|(e, _)| e.into())
.and_then(move |connec| {
let protos = vec![b"/proto1", b"/proto2"];
listener_select_proto(connec, protos)
})
.and_then(|(proto, io)| io.complete().map(move |_| proto));
let server = async_std::task::spawn(async move {
let connec = listener.accept().await.unwrap().0;
let protos = vec![b"/proto1", b"/proto2"];
let (proto, io) = listener_select_proto(connec, protos).await.unwrap();
assert_eq!(proto, b"/proto2");
io.complete().await.unwrap();
});
let client = TcpStream::connect(&listener_addr)
.from_err()
.and_then(move |connec| {
let protos = vec![b"/proto3", b"/proto2"];
dialer_select_proto_serial(connec, protos.into_iter(), version)
})
.and_then(|(proto, io)| io.complete().map(move |_| proto));
let client = async_std::task::spawn(async move {
let connec = TcpStream::connect(&listener_addr).await.unwrap();
let protos = vec![b"/proto3", b"/proto2"];
let (proto, io) = dialer_select_proto_serial(connec, protos.into_iter(), version)
.await.unwrap();
assert_eq!(proto, b"/proto2");
io.complete().await.unwrap();
});
let mut rt = Runtime::new().unwrap();
let (dialer_chosen, listener_chosen) =
rt.block_on(client.join(server)).unwrap();
assert_eq!(dialer_chosen, b"/proto2");
assert_eq!(listener_chosen, b"/proto2");
server.await;
client.await;
}
run(Version::V1);
run(Version::V1Lazy);
async_std::task::block_on(run(Version::V1));
async_std::task::block_on(run(Version::V1Lazy));
}