|
| 1 | +mod test_utils; |
| 2 | +use async_std::prelude::*; |
| 3 | +use async_std::task; |
| 4 | +use std::time::Duration; |
| 5 | + |
| 6 | +use tide::stopper::Stopper; |
| 7 | +use tide::Response; |
| 8 | + |
| 9 | +#[async_std::test] |
| 10 | +async fn cancellation() -> Result<(), http_types::Error> { |
| 11 | + let port = test_utils::find_port().await; |
| 12 | + let stopper = Stopper::new(); |
| 13 | + let stopper_ = stopper.clone(); |
| 14 | + |
| 15 | + let server = task::spawn(async move { |
| 16 | + let mut app = tide::new(); |
| 17 | + app.with_stopper(stopper_); |
| 18 | + app.at("/").get(|_| async { |
| 19 | + task::sleep(Duration::from_secs(1)).await; |
| 20 | + Ok(Response::new(200)) |
| 21 | + }); |
| 22 | + app.listen(("localhost", port)).await?; |
| 23 | + tide::Result::Ok(()) |
| 24 | + }); |
| 25 | + |
| 26 | + let client1 = task::spawn(async move { |
| 27 | + task::sleep(Duration::from_millis(100)).await; |
| 28 | + let res = surf::get(format!("http://localhost:{}", port)) |
| 29 | + .await |
| 30 | + .unwrap(); |
| 31 | + assert_eq!(res.status(), 200); |
| 32 | + async_std::future::pending().await |
| 33 | + }); |
| 34 | + |
| 35 | + let client2 = task::spawn(async move { |
| 36 | + task::sleep(Duration::from_millis(200)).await; |
| 37 | + let res = surf::get(format!("http://localhost:{}", port)) |
| 38 | + .await |
| 39 | + .unwrap(); |
| 40 | + assert_eq!(res.status(), 200); |
| 41 | + async_std::future::pending().await |
| 42 | + }); |
| 43 | + |
| 44 | + let stop = task::spawn(async move { |
| 45 | + task::sleep(Duration::from_millis(300)).await; |
| 46 | + stopper.stop(); |
| 47 | + Ok(()) |
| 48 | + }); |
| 49 | + |
| 50 | + server |
| 51 | + .try_join(stop) |
| 52 | + .race(client1.try_join(client2)) |
| 53 | + .timeout(Duration::from_secs(2)) |
| 54 | + .await??; |
| 55 | + |
| 56 | + Ok(()) |
| 57 | +} |
0 commit comments