|
| 1 | +include!(env!("BINDINGS")); |
| 2 | + |
| 3 | +use crate::my::test::i::{read_and_drop, take_then_close}; |
| 4 | +use futures::task::noop_waker_ref; |
| 5 | +use std::future::Future; |
| 6 | +use std::task::Context; |
| 7 | +use wit_bindgen::FutureWriteCancel; |
| 8 | + |
| 9 | +fn main() { |
| 10 | + wit_bindgen::block_on(async { |
| 11 | + // cancel from the other end |
| 12 | + let (tx, rx) = wit_future::new(); |
| 13 | + let f1 = async { tx.write("hello".into()).await }; |
| 14 | + let f2 = async { take_then_close(rx) }; |
| 15 | + let (result, ()) = futures::join!(f1, f2); |
| 16 | + assert_eq!(result.unwrap_err().value, "hello"); |
| 17 | + |
| 18 | + // cancel before we actually hit the intrinsic |
| 19 | + let (tx, _rx) = wit_future::new::<String>(); |
| 20 | + let mut future = Box::pin(tx.write("hello2".into())); |
| 21 | + let tx = match future.as_mut().cancel() { |
| 22 | + FutureWriteCancel::Cancelled(val, tx) => { |
| 23 | + assert_eq!(val, "hello2"); |
| 24 | + tx |
| 25 | + } |
| 26 | + _ => unreachable!(), |
| 27 | + }; |
| 28 | + |
| 29 | + // cancel after we hit the intrinsic |
| 30 | + let mut future = Box::pin(tx.write("hello3".into())); |
| 31 | + assert!(future |
| 32 | + .as_mut() |
| 33 | + .poll(&mut Context::from_waker(noop_waker_ref())) |
| 34 | + .is_pending()); |
| 35 | + match future.as_mut().cancel() { |
| 36 | + FutureWriteCancel::Cancelled(val, _) => { |
| 37 | + assert_eq!(val, "hello3"); |
| 38 | + } |
| 39 | + _ => unreachable!(), |
| 40 | + }; |
| 41 | + |
| 42 | + // cancel after we hit the intrinsic and then close the other end |
| 43 | + // |
| 44 | + // FIXME(wasip3-prototyping#137) |
| 45 | + if false { |
| 46 | + let (tx, rx) = wit_future::new::<String>(); |
| 47 | + let mut future = Box::pin(tx.write("hello3".into())); |
| 48 | + assert!(future |
| 49 | + .as_mut() |
| 50 | + .poll(&mut Context::from_waker(noop_waker_ref())) |
| 51 | + .is_pending()); |
| 52 | + drop(rx); |
| 53 | + match future.as_mut().cancel() { |
| 54 | + FutureWriteCancel::Closed(val) => assert_eq!(val, "hello3"), |
| 55 | + other => panic!("expected closed, got: {other:?}"), |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + // Start a write, wait for it to be pending, then go complete the write |
| 60 | + // in some async work, then cancel it and witness that it was written, |
| 61 | + // not cancelled. |
| 62 | + // |
| 63 | + // FIXME(wasip3-prototyping#138) |
| 64 | + if false { |
| 65 | + let (tx, rx) = wit_future::new::<String>(); |
| 66 | + let mut future = Box::pin(tx.write("hello3".into())); |
| 67 | + assert!(future |
| 68 | + .as_mut() |
| 69 | + .poll(&mut Context::from_waker(noop_waker_ref())) |
| 70 | + .is_pending()); |
| 71 | + read_and_drop(rx).await; |
| 72 | + match future.as_mut().cancel() { |
| 73 | + FutureWriteCancel::AlreadySent => {} |
| 74 | + other => panic!("expected sent, got: {other:?}"), |
| 75 | + }; |
| 76 | + } |
| 77 | + }); |
| 78 | +} |
0 commit comments