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
|
#[cfg(feature = "async")]
use {
flume::*,
futures::{stream::FuturesUnordered, StreamExt, TryFutureExt, Future},
futures::task::{Context, Waker, Poll},
async_std::prelude::FutureExt,
std::{time::Duration, sync::{atomic::{AtomicUsize, Ordering}, Arc}},
};
#[cfg(feature = "async")]
#[test]
fn r#async_recv() {
let (tx, rx) = unbounded();
let t = std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(250));
tx.send(42u32).unwrap();
});
async_std::task::block_on(async {
assert_eq!(rx.recv_async().await.unwrap(), 42);
});
t.join().unwrap();
}
#[cfg(feature = "async")]
#[test]
fn r#async_send() {
let (tx, rx) = bounded(1);
let t = std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(250));
assert_eq!(rx.recv(), Ok(42));
});
async_std::task::block_on(async {
tx.send_async(42u32).await.unwrap();
});
t.join().unwrap();
}
#[cfg(feature = "async")]
#[test]
fn r#async_recv_disconnect() {
let (tx, rx) = bounded::<i32>(0);
let t = std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(250));
drop(tx)
});
async_std::task::block_on(async {
assert_eq!(rx.recv_async().await, Err(RecvError::Disconnected));
});
t.join().unwrap();
}
#[cfg(feature = "async")]
#[test]
fn r#async_send_disconnect() {
let (tx, rx) = bounded(0);
let t = std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(250));
drop(rx)
});
async_std::task::block_on(async {
assert_eq!(tx.send_async(42u32).await, Err(SendError(42)));
});
t.join().unwrap();
}
#[cfg(feature = "async")]
#[test]
fn r#async_recv_drop_recv() {
let (tx, rx) = bounded::<i32>(10);
let recv_fut = rx.recv_async();
async_std::task::block_on(async {
let res = async_std::future::timeout(std::time::Duration::from_millis(500), rx.recv_async()).await;
assert!(res.is_err());
});
let rx2 = rx.clone();
let t = std::thread::spawn(move || {
async_std::task::block_on(async {
rx2.recv_async().await
})
});
std::thread::sleep(std::time::Duration::from_millis(500));
tx.send(42).unwrap();
drop(recv_fut);
assert_eq!(t.join().unwrap(), Ok(42))
}
#[cfg(feature = "async")]
#[async_std::test]
async fn r#async_send_1_million_no_drop_or_reorder() {
#[derive(Debug)]
enum Message {
Increment {
old: u64,
},
ReturnCount,
}
let (tx, rx) = unbounded();
let t = async_std::task::spawn(async move {
let mut count = 0u64;
while let Ok(Message::Increment { old }) = rx.recv_async().await {
assert_eq!(old, count);
count += 1;
}
count
});
for next in 0..1_000_000 {
tx.send(Message::Increment { old: next }).unwrap();
}
tx.send(Message::ReturnCount).unwrap();
let count = t.await;
assert_eq!(count, 1_000_000)
}
#[cfg(feature = "async")]
#[async_std::test]
async fn parallel_async_receivers() {
let (tx, rx) = flume::unbounded();
let send_fut = async move {
let n_sends: usize = 100000;
for _ in 0..n_sends {
tx.send_async(()).await.unwrap();
}
};
async_std::task::spawn(
send_fut
.timeout(Duration::from_secs(5))
.map_err(|_| panic!("Send timed out!"))
);
let mut futures_unordered = (0..250)
.map(|_| async {
while let Ok(()) = rx.recv_async().await
/* rx.recv() is OK */
{}
})
.collect::<FuturesUnordered<_>>();
let recv_fut = async {
while futures_unordered.next().await.is_some() {}
};
recv_fut
.timeout(Duration::from_secs(5))
.map_err(|_| panic!("Receive timed out!"))
.await
.unwrap();
println!("recv end");
}
#[cfg(feature = "async")]
#[test]
fn change_waker() {
let (tx, rx) = flume::bounded(1);
tx.send(()).unwrap();
struct DebugWaker(Arc<AtomicUsize>, Waker);
impl DebugWaker {
fn new() -> Self {
let woken = Arc::new(AtomicUsize::new(0));
let woken_cloned = woken.clone();
let waker = waker_fn::waker_fn(move || {
woken.fetch_add(1, Ordering::SeqCst);
});
DebugWaker(woken_cloned, waker)
}
fn woken(&self) -> usize {
self.0.load(Ordering::SeqCst)
}
fn ctx(&self) -> Context {
Context::from_waker(&self.1)
}
}
// Check that the waker is correctly updated when sending tasks change their wakers
{
let send_fut = tx.send_async(());
futures::pin_mut!(send_fut);
let (waker1, waker2) = (DebugWaker::new(), DebugWaker::new());
// Set the waker to waker1
assert_eq!(send_fut.as_mut().poll(&mut waker1.ctx()), Poll::Pending);
// Change the waker to waker2
assert_eq!(send_fut.poll(&mut waker2.ctx()), Poll::Pending);
// Wake the future
rx.recv().unwrap();
// Check that waker2 was woken and waker1 was not
assert_eq!(waker1.woken(), 0);
assert_eq!(waker2.woken(), 1);
}
// Check that the waker is correctly updated when receiving tasks change their wakers
{
rx.recv().unwrap();
let recv_fut = rx.recv_async();
futures::pin_mut!(recv_fut);
let (waker1, waker2) = (DebugWaker::new(), DebugWaker::new());
// Set the waker to waker1
assert_eq!(recv_fut.as_mut().poll(&mut waker1.ctx()), Poll::Pending);
// Change the waker to waker2
assert_eq!(recv_fut.poll(&mut waker2.ctx()), Poll::Pending);
// Wake the future
tx.send(()).unwrap();
// Check that waker2 was woken and waker1 was not
assert_eq!(waker1.woken(), 0);
assert_eq!(waker2.woken(), 1);
}
}
#[cfg(feature = "async")]
#[test]
fn spsc_single_threaded_value_ordering() {
async fn test() {
let (tx, rx) = flume::bounded(4);
tokio::select! {
_ = producer(tx) => {},
_ = consumer(rx) => {},
}
}
async fn producer(tx: flume::Sender<usize>) {
for i in 0..100 {
tx.send_async(i).await.unwrap();
}
}
async fn consumer(rx: flume::Receiver<usize>) {
let mut expected = 0;
while let Ok(value) = rx.recv_async().await {
assert_eq!(value, expected);
expected += 1;
}
}
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
rt.block_on(test());
}
|