extern crate amy;
use amy::{Poller, Event};
#[test]
fn send_wakes_poller() {
let mut poller = Poller::new().unwrap();
let registrar = poller.get_registrar();
let (tx, rx) = registrar.channel().unwrap();
let notifications = poller.wait(50).unwrap();
assert_eq!(0, notifications.len());
tx.send("a").unwrap();
let notifications = poller.wait(5000).unwrap();
assert_eq!(1, notifications.len());
assert_eq!(rx.get_id(), notifications[0].id);
assert_eq!(Event::Read, notifications[0].event);
assert_eq!("a", rx.try_recv().unwrap());
assert!(rx.try_recv().is_err());
}
#[test]
fn multiple_sends_wake_poller_once() {
let mut poller = Poller::new().unwrap();
let registrar = poller.get_registrar();
let (tx, rx) = registrar.channel().unwrap();
tx.send("a").unwrap();
tx.send("b").unwrap();
let notifications = poller.wait(5000).unwrap();
assert_eq!(1, notifications.len());
assert_eq!(rx.get_id(), notifications[0].id);
assert_eq!("a", rx.try_recv().unwrap());
assert_eq!("b", rx.try_recv().unwrap());
let notifications = poller.wait(50).unwrap();
assert_eq!(0, notifications.len());
}
#[test]
fn send_before_poll_and_after_poll_but_before_recv_only_wakes_poller_once() {
let mut poller = Poller::new().unwrap();
let registrar = poller.get_registrar();
let (tx, rx) = registrar.channel().unwrap();
tx.send("a").unwrap();
let notifications = poller.wait(5000).unwrap();
assert_eq!(1, notifications.len());
assert_eq!(rx.get_id(), notifications[0].id);
tx.send("b").unwrap();
assert_eq!("a", rx.try_recv().unwrap());
assert_eq!("b", rx.try_recv().unwrap());
let notifications = poller.wait(50).unwrap();
assert_eq!(0, notifications.len());
}
#[test]
fn send_after_receive_after_poll_followed_by_recv_wakes_poller_again() {
let mut poller = Poller::new().unwrap();
let registrar = poller.get_registrar();
let (tx, rx) = registrar.channel().unwrap();
tx.send("a").unwrap();
let notifications = poller.wait(5000).unwrap();
assert_eq!(1, notifications.len());
assert_eq!(rx.get_id(), notifications[0].id);
assert_eq!("a", rx.try_recv().unwrap());
tx.send("b").unwrap();
assert_eq!("b", rx.try_recv().unwrap());
let notifications = poller.wait(1000).unwrap();
assert_eq!(1, notifications.len());
assert_eq!(rx.get_id(), notifications[0].id);
assert!(rx.try_recv().is_err());
}
#[test]
fn send_after_receive_after_poll_followed_by_recv_until_err_doesnt_wake_polller_again() {
let mut poller = Poller::new().unwrap();
let registrar = poller.get_registrar();
let (tx, rx) = registrar.channel().unwrap();
tx.send("a").unwrap();
let notifications = poller.wait(5000).unwrap();
assert_eq!(1, notifications.len());
assert_eq!(rx.get_id(), notifications[0].id);
assert_eq!("a", rx.try_recv().unwrap());
tx.send("b").unwrap();
assert_eq!("b", rx.try_recv().unwrap());
assert!(rx.try_recv().is_err());
let notifications = poller.wait(50).unwrap();
assert_eq!(0, notifications.len());
}
#[test]
fn send_poll_receive_twice_then_send_poll_receive_once() {
let mut poller = Poller::new().unwrap();
let registrar = poller.get_registrar();
let (tx, rx) = registrar.channel().unwrap();
tx.send("a").unwrap();
let notifications = poller.wait(5000).unwrap();
assert_eq!(1, notifications.len());
assert_eq!(rx.get_id(), notifications[0].id);
assert_eq!("a", rx.try_recv().unwrap());
assert!(rx.try_recv().is_err());
tx.send("b").unwrap();
let notifications = poller.wait(5000).unwrap();
assert_eq!(1, notifications.len());
assert_eq!(rx.get_id(), notifications[0].id);
assert_eq!("b", rx.try_recv().unwrap());
}
#[test]
fn simple_sync_channel_test() {
let mut poller = Poller::new().unwrap();
let registrar = poller.get_registrar();
let (tx, rx) = registrar.sync_channel(1).unwrap();
let notifications = poller.wait(50).unwrap();
assert_eq!(0, notifications.len());
tx.send("a").unwrap();
let notifications = poller.wait(5000).unwrap();
assert_eq!(1, notifications.len());
assert_eq!(rx.get_id(), notifications[0].id);
assert_eq!(Event::Read, notifications[0].event);
tx.try_send("b").is_err();
assert_eq!("a", rx.try_recv().unwrap());
assert!(rx.try_recv().is_err());
tx.try_send("b").unwrap();
assert_eq!("b", rx.try_recv().unwrap());
}