|
| 1 | +#![cfg(all(target_family = "wasm", feature = "futures"))] |
| 2 | + |
| 3 | +use futures_channel::{mpsc, oneshot}; |
| 4 | +use futures_util::{ |
| 5 | + future::{select, Either, FutureExt}, |
| 6 | + stream::StreamExt, |
| 7 | +}; |
| 8 | +use gloo_timers::{ |
| 9 | + callback::{Interval, Timeout}, |
| 10 | + future::{sleep, IntervalStream, TimeoutFuture}, |
| 11 | +}; |
| 12 | +use std::cell::Cell; |
| 13 | +use std::rc::Rc; |
| 14 | +use std::time::Duration; |
| 15 | +use wasm_bindgen_test::*; |
| 16 | + |
| 17 | +#[wasm_bindgen_test] |
| 18 | +async fn timeout() { |
| 19 | + let (sender, receiver) = oneshot::channel(); |
| 20 | + Timeout::new(1, || sender.send(()).unwrap()).forget(); |
| 21 | + receiver.await.unwrap(); |
| 22 | +} |
| 23 | + |
| 24 | +#[wasm_bindgen_test] |
| 25 | +async fn timeout_cancel() { |
| 26 | + let cell = Rc::new(Cell::new(false)); |
| 27 | + |
| 28 | + let t = Timeout::new(1, { |
| 29 | + let cell = cell.clone(); |
| 30 | + move || { |
| 31 | + cell.set(true); |
| 32 | + panic!("should have been cancelled"); |
| 33 | + } |
| 34 | + }); |
| 35 | + t.cancel(); |
| 36 | + |
| 37 | + let (sender, receiver) = oneshot::channel(); |
| 38 | + |
| 39 | + Timeout::new(2, move || { |
| 40 | + sender.send(()).unwrap(); |
| 41 | + assert_eq!(cell.get(), false); |
| 42 | + }) |
| 43 | + .forget(); |
| 44 | + |
| 45 | + receiver.await.unwrap(); |
| 46 | +} |
| 47 | + |
| 48 | +#[wasm_bindgen_test] |
| 49 | +async fn timeout_future() { |
| 50 | + TimeoutFuture::new(1).await; |
| 51 | +} |
| 52 | + |
| 53 | +#[wasm_bindgen_test] |
| 54 | +async fn timeout_future_cancel() { |
| 55 | + let cell = Rc::new(Cell::new(false)); |
| 56 | + |
| 57 | + let a = TimeoutFuture::new(1).map({ |
| 58 | + let cell = cell.clone(); |
| 59 | + move |_| { |
| 60 | + assert_eq!(cell.get(), false); |
| 61 | + 1 |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + let b = TimeoutFuture::new(2).map({ |
| 66 | + let cell = cell.clone(); |
| 67 | + move |_| { |
| 68 | + cell.set(true); |
| 69 | + 2u32 |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + let (who, other) = match select(a, b).await { |
| 74 | + Either::Left(x) => x, |
| 75 | + Either::Right(_) => panic!("Timer for 2 ms finished before timer for 1 ms"), |
| 76 | + }; |
| 77 | + assert_eq!(who, 1); |
| 78 | + // Drop `b` so that its timer is canceled. |
| 79 | + drop(other); |
| 80 | + TimeoutFuture::new(3).await; |
| 81 | + // We should never have fired `b`'s timer. |
| 82 | + assert_eq!(cell.get(), false); |
| 83 | +} |
| 84 | + |
| 85 | +#[wasm_bindgen_test] |
| 86 | +async fn interval() { |
| 87 | + let (mut sender, receiver) = mpsc::channel(1); |
| 88 | + let i = Interval::new(1, move || { |
| 89 | + if !sender.is_closed() { |
| 90 | + sender.try_send(()).unwrap() |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + let results: Vec<_> = receiver.take(5).collect().await; |
| 95 | + drop(i); |
| 96 | + assert_eq!(results.len(), 5); |
| 97 | +} |
| 98 | + |
| 99 | +#[wasm_bindgen_test] |
| 100 | +async fn interval_cancel() { |
| 101 | + let i = Interval::new(10, move || { |
| 102 | + panic!("This should never be called"); |
| 103 | + }); |
| 104 | + i.cancel(); |
| 105 | + |
| 106 | + // This keeps us live for long enough that if any erroneous Interval callbacks fired, we'll have seen them. |
| 107 | + sleep(Duration::from_millis(100)).await; |
| 108 | +} |
| 109 | + |
| 110 | +#[wasm_bindgen_test] |
| 111 | +async fn interval_stream() { |
| 112 | + let results: Vec<_> = IntervalStream::new(1).take(5).collect().await; |
| 113 | + assert_eq!(results.len(), 5); |
| 114 | +} |
0 commit comments