use flo_stream::*;
use futures::prelude::*;
use futures::stream;
use futures::executor;
use ::desync::*;
use std::mem;
use std::thread;
use std::time::{Duration};
#[test]
fn switch_stream() {
let stream_1 = stream::iter(vec![1, 2, 3, 4]);
let stream_2 = stream::iter(vec![10, 11, 12, 13]);
let (stream, switch) = switchable_stream(stream_1);
let mut stream = stream;
let a = executor::block_on(async { stream.next().await.unwrap() });
let b = executor::block_on(async { stream.next().await.unwrap() });
switch.switch_to_stream(stream_2);
let c = executor::block_on(async { stream.next().await.unwrap() });
let d = executor::block_on(async { stream.next().await.unwrap() });
let e = executor::block_on(async { stream.next().await.unwrap() });
assert!(a == 1);
assert!(b == 2);
assert!(c == 10);
assert!(d == 11);
assert!(e == 12);
}
#[test]
fn close_when_switch_is_dropped() {
let stream_1 = stream::iter(vec![1, 2, 3, 4]);
let (stream, switch) = switchable_stream(stream_1);
let mut stream = stream;
mem::drop(switch);
executor::block_on(async { stream.next().await.unwrap() });
executor::block_on(async { stream.next().await.unwrap() });
executor::block_on(async { stream.next().await.unwrap() });
executor::block_on(async { stream.next().await.unwrap() });
let closed = executor::block_on(async { stream.next().await });
assert!(closed.is_none());
}
#[test]
fn switch_after_first_stream_is_closed() {
let background = Desync::new(());
let stream_1 = stream::iter(vec![1, 2, 3, 4]);
let stream_2 = stream::iter(vec![10, 11, 12, 13]);
let (stream, switch) = switchable_stream(stream_1);
let mut stream = stream;
let a = executor::block_on(async { stream.next().await.unwrap() });
let b = executor::block_on(async { stream.next().await.unwrap() });
let c = executor::block_on(async { stream.next().await.unwrap() });
let d = executor::block_on(async { stream.next().await.unwrap() });
let next_value = background.future_desync(move |_| async move { stream.next().await }.boxed());
thread::sleep(Duration::from_millis(100));
switch.switch_to_stream(stream_2);
assert!(a == 1);
assert!(b == 2);
assert!(c == 3);
assert!(d == 4);
let e = executor::block_on(async { next_value.await.unwrap() });
assert!(e == Some(10));
}