externally-driven-async-rt 0.5.1

Async runtime that is meant to be embedded
Documentation
use core::{
    cell::Cell,
    future::Future,
    pin::pin,
    task::{Context, Poll, Waker},
};

use crate::{
    notify::Notification,
    testing::{counting_waker, flag_waker},
};

#[test]
fn fire_before_poll_resolves_immediately() {
    let notification = Notification::new();
    let mut cx = Context::from_waker(Waker::noop());
    notification.fire();
    assert_eq!(pin!(notification.notified()).poll(&mut cx), Poll::Ready(()));
    assert_eq!(pin!(notification.notified()).poll(&mut cx), Poll::Pending);
}

#[test]
fn dropping_a_pending_notified_forgets_its_waker() {
    let drops = Cell::new(0);
    let notification = Notification::new();
    {
        let waker = counting_waker(&drops);
        let mut cx = Context::from_waker(&waker);
        let mut notified = pin!(notification.notified());
        assert_eq!(notified.as_mut().poll(&mut cx), Poll::Pending);
    }
    assert_eq!(drops.get(), 2);
}

#[test]
fn clear_drops_a_pending_notification() {
    let notification = Notification::new();
    notification.fire();
    notification.clear();
    let mut cx = Context::from_waker(Waker::noop());
    assert_eq!(pin!(notification.notified()).poll(&mut cx), Poll::Pending);
}

#[test]
fn fire_wakes_a_pending_waiter() {
    let woken = Cell::new(false);
    let notification = Notification::new();
    let waker = flag_waker(&woken);
    let mut cx = Context::from_waker(&waker);
    let mut notified = pin!(notification.notified());
    assert_eq!(notified.as_mut().poll(&mut cx), Poll::Pending);
    assert!(!woken.get());
    notification.fire();
    assert!(woken.get());
    assert_eq!(notified.as_mut().poll(&mut cx), Poll::Ready(()));
}

#[test]
fn fires_coalesce() {
    let notification = Notification::new();
    let mut cx = Context::from_waker(Waker::noop());
    notification.fire();
    notification.fire();
    notification.fire();
    assert_eq!(pin!(notification.notified()).poll(&mut cx), Poll::Ready(()));
    assert_eq!(pin!(notification.notified()).poll(&mut cx), Poll::Pending);
}