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
|
use async_stream::stream;
use futures_core::stream::{FusedStream, Stream};
use futures_util::pin_mut;
use futures_util::stream::StreamExt;
use tokio::sync::mpsc;
use tokio_test::assert_ok;
#[tokio::test]
async fn noop_stream() {
let s = stream! {};
pin_mut!(s);
while s.next().await.is_some() {
unreachable!();
}
}
#[tokio::test]
async fn empty_stream() {
let mut ran = false;
{
let r = &mut ran;
let s = stream! {
*r = true;
println!("hello world!");
};
pin_mut!(s);
while s.next().await.is_some() {
unreachable!();
}
}
assert!(ran);
}
#[tokio::test]
async fn yield_single_value() {
let s = stream! {
yield "hello";
};
let values: Vec<_> = s.collect().await;
assert_eq!(1, values.len());
assert_eq!("hello", values[0]);
}
#[tokio::test]
async fn fused() {
let s = stream! {
yield "hello";
};
pin_mut!(s);
assert!(!s.is_terminated());
assert_eq!(s.next().await, Some("hello"));
assert_eq!(s.next().await, None);
assert!(s.is_terminated());
// This should return None from now on
assert_eq!(s.next().await, None);
}
#[tokio::test]
async fn yield_multi_value() {
let s = stream! {
yield "hello";
yield "world";
yield "dizzy";
};
let values: Vec<_> = s.collect().await;
assert_eq!(3, values.len());
assert_eq!("hello", values[0]);
assert_eq!("world", values[1]);
assert_eq!("dizzy", values[2]);
}
#[tokio::test]
async fn unit_yield_in_select() {
use tokio::select;
async fn do_stuff_async() {}
let s = stream! {
select! {
_ = do_stuff_async() => yield,
else => yield,
}
};
let values: Vec<_> = s.collect().await;
assert_eq!(values.len(), 1);
}
#[tokio::test]
async fn yield_with_select() {
use tokio::select;
async fn do_stuff_async() {}
async fn more_async_work() {}
let s = stream! {
select! {
_ = do_stuff_async() => yield "hey",
_ = more_async_work() => yield "hey",
else => yield "hey",
}
};
let values: Vec<_> = s.collect().await;
assert_eq!(values, vec!["hey"]);
}
#[tokio::test]
async fn return_stream() {
fn build_stream() -> impl Stream<Item = u32> {
stream! {
yield 1;
yield 2;
yield 3;
}
}
let s = build_stream();
let values: Vec<_> = s.collect().await;
assert_eq!(3, values.len());
assert_eq!(1, values[0]);
assert_eq!(2, values[1]);
assert_eq!(3, values[2]);
}
#[tokio::test]
async fn consume_channel() {
let (tx, mut rx) = mpsc::channel(10);
let s = stream! {
while let Some(v) = rx.recv().await {
yield v;
}
};
pin_mut!(s);
for i in 0..3 {
assert_ok!(tx.send(i).await);
assert_eq!(Some(i), s.next().await);
}
drop(tx);
assert_eq!(None, s.next().await);
}
#[tokio::test]
async fn borrow_self() {
struct Data(String);
impl Data {
fn stream<'a>(&'a self) -> impl Stream<Item = &str> + 'a {
stream! {
yield &self.0[..];
}
}
}
let data = Data("hello".to_string());
let s = data.stream();
pin_mut!(s);
assert_eq!(Some("hello"), s.next().await);
}
#[tokio::test]
async fn stream_in_stream() {
let s = stream! {
let s = stream! {
for i in 0..3 {
yield i;
}
};
pin_mut!(s);
while let Some(v) = s.next().await {
yield v;
}
};
let values: Vec<_> = s.collect().await;
assert_eq!(3, values.len());
}
#[tokio::test]
async fn yield_non_unpin_value() {
let s: Vec<_> = stream! {
for i in 0..3 {
yield async move { i };
}
}
.buffered(1)
.collect()
.await;
assert_eq!(s, vec![0, 1, 2]);
}
#[test]
fn inner_try_stream() {
use async_stream::try_stream;
use tokio::select;
async fn do_stuff_async() {}
let _ = stream! {
select! {
_ = do_stuff_async() => {
let another_s = try_stream! {
yield;
};
let _: Result<(), ()> = Box::pin(another_s).next().await.unwrap();
},
else => {},
}
yield
};
}
#[rustversion::attr(not(stable), ignore)]
#[test]
fn test() {
let t = trybuild::TestCases::new();
use std::fs::{self, DirEntry};
for entry in fs::read_dir("tests/ui").unwrap() {
let entry = entry.unwrap();
let path = entry.path();
let path = path.to_str().unwrap();
println!("{}",path);
if !path.ends_with(".rs") { continue }; //igore files that don't end in .rs
if path.ends_with("/yield_in_closure.rs") { continue }; // test seems sensitive to compiler version
if path.ends_with("/yield_in_async.rs") { continue }; // test seems sensitive to compiler version
if path.ends_with("/yield_in_nested_fn.rs") { continue }; // test seems sensitive to compiler version
t.compile_fail(path);
}
}
|