-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathpoll_api.rs
148 lines (122 loc) · 4.73 KB
/
poll_api.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use futures::future::BoxFuture;
use futures::stream::FuturesUnordered;
use futures::{AsyncRead, AsyncWrite, AsyncWriteExt, FutureExt, StreamExt};
use quickcheck::QuickCheck;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use test_harness::*;
use tokio::net::TcpStream;
use tokio::runtime::Runtime;
use tokio::task;
use tokio_util::compat::TokioAsyncReadCompatExt;
use yamux::{Config, Connection, ConnectionError, Mode};
#[test]
fn prop_config_send_recv_multi() {
let _ = env_logger::try_init();
fn prop(msgs: Vec<Msg>, cfg1: TestConfig, cfg2: TestConfig) {
Runtime::new().unwrap().block_on(async move {
let num_messagses = msgs.len();
let (listener, address) = bind().await.expect("bind");
let server = async {
let socket = listener.accept().await.expect("accept").0.compat();
let connection = Connection::new(socket, cfg1.0, Mode::Server);
EchoServer::new(connection).await
};
let client = async {
let socket = TcpStream::connect(address).await.expect("connect").compat();
let connection = Connection::new(socket, cfg2.0, Mode::Client);
MessageSender::new(connection, msgs).await
};
let (server_processed, client_processed) =
futures::future::try_join(server, client).await.unwrap();
assert_eq!(server_processed, num_messagses);
assert_eq!(client_processed, num_messagses);
})
}
QuickCheck::new().quickcheck(prop as fn(_, _, _) -> _)
}
#[test]
fn prop_max_streams() {
fn prop(n: usize) -> Result<bool, ConnectionError> {
let max_streams = n % 100;
let mut cfg = Config::default();
cfg.set_max_num_streams(max_streams);
Runtime::new().unwrap().block_on(async move {
let (server, client) = connected_peers(cfg.clone(), cfg).await?;
task::spawn(EchoServer::new(server));
let client = OpenStreamsClient::new(client, max_streams);
let (client, streams) = client.await?;
assert_eq!(streams.len(), max_streams);
let open_result = OpenStreamsClient::new(client, 1).await;
Ok(matches!(open_result, Err(ConnectionError::TooManyStreams)))
})
}
QuickCheck::new().tests(7).quickcheck(prop as fn(_) -> _)
}
struct MessageSender<T> {
connection: Connection<T>,
pending_messages: Vec<Msg>,
worker_streams: FuturesUnordered<BoxFuture<'static, ()>>,
streams_processed: usize,
}
impl<T> MessageSender<T> {
fn new(connection: Connection<T>, messages: Vec<Msg>) -> Self {
Self {
connection,
pending_messages: messages,
worker_streams: FuturesUnordered::default(),
streams_processed: 0,
}
}
}
impl<T> Future for MessageSender<T>
where
T: AsyncRead + AsyncWrite + Unpin,
{
type Output = yamux::Result<usize>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
loop {
if this.pending_messages.is_empty() && this.worker_streams.is_empty() {
futures::ready!(this.connection.poll_close(cx)?);
return Poll::Ready(Ok(this.streams_processed));
}
if let Some(message) = this.pending_messages.pop() {
match this.connection.poll_new_outbound(cx)? {
Poll::Ready(mut stream) => {
this.worker_streams.push(
async move {
send_recv_message(&mut stream, message).await.unwrap();
stream.close().await.unwrap();
}
.boxed(),
);
continue;
}
Poll::Pending => {
this.pending_messages.push(message);
}
}
}
match this.worker_streams.poll_next_unpin(cx) {
Poll::Ready(Some(())) => {
this.streams_processed += 1;
continue;
}
Poll::Ready(None) | Poll::Pending => {}
}
match this.connection.poll_next_inbound(cx)? {
Poll::Ready(Some(stream)) => {
drop(stream);
panic!("Did not expect remote to open a stream");
}
Poll::Ready(None) => {
panic!("Did not expect remote to close the connection");
}
Poll::Pending => {}
}
return Poll::Pending;
}
}
}