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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use std::{convert::TryInto, pin::Pin, time::Duration};
use futures::{
future::{join, ready},
Future, FutureExt,
};
use tokio::{
sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
task::yield_now,
};
pub trait ErrorType: Send + std::fmt::Debug + 'static {}
impl<T: Send + std::fmt::Debug + 'static> ErrorType for T {}
pub type InSender<E> = UnboundedSender<Result<Vec<u8>, E>>;
pub type InReceiver<E> = UnboundedReceiver<Result<Vec<u8>, E>>;
pub type OutReceiver = UnboundedReceiver<Vec<u8>>;
pub type OutSender = UnboundedSender<Vec<u8>>;
pub trait SessionFactory<E: ErrorType>:
FnOnce(InReceiver<E>, OutSender) -> Pin<Box<dyn Future<Output = Result<(), E>> + Send>>
{
}
impl<
E: ErrorType,
F: FnOnce(InReceiver<E>, OutSender) -> Pin<Box<dyn Future<Output = Result<(), E>> + Send>>,
> SessionFactory<E> for F
{
}
pub trait BehaviourFunction<E: ErrorType>:
for<'a> FnOnce(
&'a mut InSender<E>,
&'a mut OutReceiver,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>
+ Send
{
}
impl<E: ErrorType, T> BehaviourFunction<E> for T where
for<'a> T: FnOnce(
&'a mut InSender<E>,
&'a mut OutReceiver,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>
+ Send
{
}
pub trait Chainable<E: ErrorType>: BehaviourFunction<E> + Sized {
fn then<S: BehaviourFunction<E> + 'static>(
self,
followed_by: S,
) -> Box<dyn BehaviourFunction<E>>;
}
impl<E: ErrorType, T: BehaviourFunction<E> + 'static> Chainable<E> for T {
fn then<S: BehaviourFunction<E> + 'static>(
self,
followed_by: S,
) -> Box<dyn BehaviourFunction<E>> {
Box::new(|sender: &mut InSender<E>, receiver: &mut OutReceiver| {
async move {
self(sender, receiver).await;
followed_by(sender, receiver).await
}
.boxed()
})
}
}
fn into_behaviour<E, C>(closure: C) -> impl BehaviourFunction<E>
where
E: ErrorType,
C: for<'a> FnOnce(
&'a mut InSender<E>,
&'a mut OutReceiver,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>
+ Send
+ 'static,
{
closure
}
pub fn send<T: TryInto<Vec<u8>> + Send + 'static, E: ErrorType + From<T::Error>>(
data: T,
) -> impl BehaviourFunction<E> {
into_behaviour(move |in_sender: &mut InSender<E>, _: &mut OutReceiver| {
send_data(in_sender, data);
yield_now().boxed()
})
}
pub fn send_data<T: TryInto<Vec<u8>>, E: ErrorType + From<T::Error>>(
in_sender: &InSender<E>,
data: T,
) {
in_sender
.send(data.try_into().map_err(|e2| e2.into()))
.expect("Connect failed");
}
pub async fn test_expectations<
E: ErrorType,
F: SessionFactory<E>,
T: BehaviourFunction<E> + 'static,
>(
session_factory: F,
client_behaviour: T,
) {
let (in_sender, in_receiver) = unbounded_channel::<Result<Vec<u8>, E>>();
let (out_sender, out_receiver) = unbounded_channel();
let session_future = session_factory(in_receiver, out_sender);
let other_future = tokio::task::spawn(async move {
let mut out_receiver = out_receiver;
let mut in_sender = in_sender;
let result = client_behaviour(&mut in_sender, &mut out_receiver).await;
drop(result);
out_receiver.close();
drop(in_sender);
()
});
let results = join(session_future, other_future).await;
assert!(results.0.is_ok());
assert!(results.1.is_ok());
}
pub fn assert_receive<T: FnOnce(Vec<u8>) -> bool>(
out_receiver: &mut OutReceiver,
message_matcher: T,
) {
let response = out_receiver.recv().now_or_never();
if let Some(Some(bytes)) = response {
assert!(message_matcher(bytes))
} else {
if response.is_none() {
panic!("No server message");
} else {
panic!("Unexpected server message:{:?}", response.unwrap());
}
}
}
pub fn receive<E: ErrorType, T: FnOnce(Vec<u8>) -> bool + Send + 'static>(
message_matcher: T,
) -> impl BehaviourFunction<E> {
into_behaviour(|_: &mut InSender<E>, out_receiver: &mut OutReceiver| {
ready(assert_receive(out_receiver, message_matcher)).boxed()
})
}
pub fn sleep_in_pause(millis: u64) -> impl Future<Output = ()> {
tokio::time::pause();
tokio::time::sleep(Duration::from_millis(millis)).inspect(|_| tokio::time::resume())
}
pub fn wait_for_disconnect<'a, E: ErrorType>(
_: &'a mut InSender<E>,
out_receiver: &'a mut OutReceiver,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
async move {
sleep_in_pause(5050).await;
assert!(matches!(out_receiver.recv().now_or_never(), Some(None)));
()
}
.boxed()
}
#[cfg(test)]
mod test {
use std::convert::Infallible;
use super::*;
#[tokio::test]
async fn chaining_works() {
let (mut tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let (_, mut out_rx) = tokio::sync::mpsc::unbounded_channel();
let behaviour_a = into_behaviour(|sender: &mut InSender<()>, _: &mut OutReceiver| {
async move {
sender.send(Ok(b"foo".to_vec())).expect("Send failed");
()
}
.boxed()
});
let behaviour_b = into_behaviour(|sender: &mut InSender<()>, _: &mut OutReceiver| {
async move {
sender.send(Ok(b"bar".to_vec())).expect("Send failed");
()
}
.boxed()
});
behaviour_a.then(behaviour_b)(&mut tx, &mut out_rx).await;
drop(tx);
assert_eq!(
"foo",
String::from_utf8(
rx.recv()
.now_or_never()
.unwrap()
.unwrap()
.expect("recv failed"),
)
.expect("Parse failed"),
);
assert_eq!(
"bar",
String::from_utf8(
rx.recv()
.now_or_never()
.unwrap()
.unwrap()
.expect("recv failed"),
)
.expect("Parse failed"),
);
assert_eq!(None, rx.recv().now_or_never().unwrap());
}
#[derive(Debug, PartialEq, Eq)]
struct TestError;
impl From<Infallible> for TestError {
fn from(_: Infallible) -> Self {
TestError
}
}
#[tokio::test]
async fn send_works() {
let (mut tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let (_, mut out_rx) = tokio::sync::mpsc::unbounded_channel();
let behaviour = send::<String, TestError>("Hello".to_owned());
behaviour(&mut tx, &mut out_rx);
drop(tx);
assert_eq!(
"Hello",
String::from_utf8(
rx.recv()
.now_or_never()
.unwrap()
.unwrap()
.expect("recv failed"),
)
.expect("Parse failed"),
);
assert_eq!(None, rx.recv().now_or_never().unwrap());
}
struct TestData;
#[derive(Debug, PartialEq, Eq)]
struct TestError2;
impl From<TestError2> for TestError {
fn from(_: TestError2) -> Self {
TestError
}
}
impl TryInto<Vec<u8>> for TestData {
type Error = TestError2;
fn try_into(self) -> Result<Vec<u8>, Self::Error> {
Err(TestError2)
}
}
#[tokio::test]
async fn send_handles_conversion_error() {
let (mut tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let (_, mut out_rx) = tokio::sync::mpsc::unbounded_channel();
let behaviour = send::<TestData, TestError>(TestData);
behaviour(&mut tx, &mut out_rx);
drop(tx);
assert_eq!(Err(TestError), rx.recv().now_or_never().unwrap().unwrap());
assert_eq!(None, rx.recv().now_or_never().unwrap());
}
}