use core::pin::Pin;
use core::task::{Context, Poll};
use std::sync::Arc;
use std::time::Duration;
use astrs_wire::StopCause;
use tokio::sync::mpsc::Receiver;
use crate::error::{NodeError, Result};
use crate::events::Event;
use crate::events::source::{EventSource, StreamStats};
use crate::runtime::NodeRuntime;
use crate::signal::WaitOutcome;
pub trait Stream {
type Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}
#[derive(Debug)]
pub struct EventStream {
source: Arc<EventSource>,
runtime: NodeRuntime,
fused: bool,
stop_cause: Option<StopCause>,
blocking_refused: bool,
}
impl EventStream {
#[must_use]
pub fn new(source: Arc<EventSource>, runtime: NodeRuntime) -> Self {
Self {
source,
runtime,
fused: false,
stop_cause: None,
blocking_refused: false,
}
}
#[must_use]
pub fn source(&self) -> &Arc<EventSource> {
&self.source
}
#[must_use]
pub const fn is_fused(&self) -> bool {
self.fused
}
#[must_use]
pub fn stop_cause(&self) -> Option<StopCause> {
self.stop_cause.clone()
}
#[must_use]
pub fn stats(&self) -> StreamStats {
self.source.stats()
}
pub fn try_recv(&mut self) -> Option<Event> {
if self.fused {
return None;
}
let event = self.source.try_next()?;
Some(self.observe(event))
}
#[must_use]
pub fn recv(&mut self) -> Option<Event> {
if self.blocking_refused {
return None;
}
match self.recv_checked() {
Ok(event) => event,
Err(error) => {
self.blocking_refused = true;
Some(Event::Error(error.to_string()))
}
}
}
#[must_use]
pub const fn blocking_refused(&self) -> bool {
self.blocking_refused
}
pub fn recv_checked(&mut self) -> Result<Option<Event>> {
self.recv_blocking(None)
}
pub fn recv_timeout(&mut self, timeout: Duration) -> Result<Option<Event>> {
self.recv_blocking(Some(timeout))
}
pub async fn recv_async(&mut self) -> Option<Event> {
loop {
if self.fused {
return None;
}
let notified = self.source.signal().notified();
let ready = self.source.try_next();
if let Some(event) = ready {
drop(notified);
return Some(self.observe(event));
}
if self.source.is_closed() {
return None;
}
notified.await;
}
}
pub async fn recv_async_timeout(&mut self, timeout: Duration) -> Option<Event> {
tokio::time::timeout(timeout, self.recv_async())
.await
.unwrap_or(None)
}
#[must_use]
pub fn session_ended(&self) -> bool {
self.source.is_closed()
}
pub fn merge_external(&mut self, mut receiver: Receiver<Event>) {
let source = Arc::clone(&self.source);
let _task = self.runtime.spawn(async move {
while let Some(event) = receiver.recv().await {
if source.is_closed() {
break;
}
source.push_control(event);
}
});
}
#[must_use]
pub fn merge_external_channel(&mut self, capacity: usize) -> tokio::sync::mpsc::Sender<Event> {
let (sender, receiver) = tokio::sync::mpsc::channel(capacity.max(1));
self.merge_external(receiver);
sender
}
fn recv_blocking(&mut self, timeout: Option<Duration>) -> Result<Option<Event>> {
let deadline = timeout.map(|timeout| std::time::Instant::now() + timeout);
loop {
if self.fused {
return Ok(None);
}
let ticket = self.source.signal().ticket();
if let Some(event) = self.source.try_next() {
return Ok(Some(self.observe(event)));
}
if self.source.is_closed() {
return Ok(None);
}
if !self.runtime.can_block() {
return Err(NodeError::BlockingInAsync {
method: "EventStream::recv",
alternative: "EventStream::recv_async",
});
}
let remaining = match deadline {
None => None,
Some(deadline) => {
match deadline.checked_duration_since(std::time::Instant::now()) {
Some(remaining) => Some(remaining),
None => return Ok(None),
}
}
};
match self.source.signal().wait_blocking(ticket, remaining) {
WaitOutcome::Signalled => {}
WaitOutcome::TimedOut => return Ok(None),
WaitOutcome::Closed => {
return Ok(self.source.try_next().map(|event| self.observe(event)));
}
}
}
}
fn observe(&mut self, event: Event) -> Event {
if let Event::Stop(cause) = &event {
self.fused = true;
self.stop_cause = Some(cause.clone());
}
event
}
}
impl Drop for EventStream {
fn drop(&mut self) {
self.source.abandon();
}
}
impl Iterator for EventStream {
type Item = Event;
fn next(&mut self) -> Option<Self::Item> {
self.recv()
}
}
impl core::iter::FusedIterator for EventStream {}
impl Stream for EventStream {
type Item = Event;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = Pin::into_inner(self);
if this.fused {
return Poll::Ready(None);
}
loop {
let notified = this.source.signal().notified();
let ready = this.source.try_next();
if let Some(event) = ready {
drop(notified);
return Poll::Ready(Some(this.observe(event)));
}
if this.source.is_closed() {
return Poll::Ready(None);
}
let mut notified = core::pin::pin!(notified);
match Future::poll(notified.as_mut(), cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(()) => {}
}
}
}
}
use core::future::Future;
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::events::QueuedEvent;
use crate::payload::Payload;
use astrs_time::HlcTimestamp;
use astrs_wire::{DataId, Metadata, PortRef, PriorityLane, QueuePolicy};
fn stream() -> (Arc<EventSource>, EventStream) {
let source = Arc::new(EventSource::new());
source
.register_raw(
DataId::new("frames").unwrap(),
8,
QueuePolicy::DropOldest,
PriorityLane::Data,
)
.unwrap();
let runtime = NodeRuntime::owned().expect("runtime");
let stream = EventStream::new(Arc::clone(&source), runtime);
(source, stream)
}
fn message() -> QueuedEvent {
QueuedEvent::Input {
source: PortRef::from_parts("camera", "image").unwrap(),
metadata: Metadata::new(HlcTimestamp::new(1, 0)),
payload: Payload::inline(vec![7]),
}
}
#[test]
fn try_recv_never_blocks() {
let (source, mut events) = stream();
assert!(events.try_recv().is_none());
source.push_input(&DataId::new("frames").unwrap(), message());
assert!(events.try_recv().unwrap().is_input());
assert!(events.try_recv().is_none());
}
#[test]
fn recv_blocks_until_an_event_arrives() {
let (source, mut events) = stream();
let producer = Arc::clone(&source);
let handle = std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(10));
producer.push_input(&DataId::new("frames").unwrap(), message());
});
let event = events.recv().unwrap();
assert!(event.is_input());
handle.join().unwrap();
}
#[test]
fn recv_timeout_returns_none_without_ending_the_stream() {
let (_source, mut events) = stream();
let event = events.recv_timeout(Duration::from_millis(10)).unwrap();
assert!(event.is_none());
assert!(!events.is_fused());
assert!(!events.session_ended());
}
#[test]
fn the_stream_fuses_after_stop() {
let (source, mut events) = stream();
source.push_input(&DataId::new("frames").unwrap(), message());
source.push_control(Event::Stop(StopCause::Requested));
source.push_input(&DataId::new("frames").unwrap(), message());
let first = events.recv().unwrap();
assert!(first.is_stop());
assert!(events.is_fused());
assert_eq!(events.stop_cause(), Some(StopCause::Requested));
assert!(events.recv().is_none(), "fused");
assert!(events.try_recv().is_none());
assert!(events.next().is_none());
}
#[test]
fn closing_the_session_ends_the_stream_after_draining() {
let (source, mut events) = stream();
source.push_input(&DataId::new("frames").unwrap(), message());
source.close();
assert!(events.recv().unwrap().is_input(), "the tail is drained");
assert!(events.recv().is_none());
assert!(events.session_ended());
}
#[test]
fn the_iterator_face_is_the_same_queue() {
let (source, events) = stream();
for _ in 0..3 {
source.push_input(&DataId::new("frames").unwrap(), message());
}
source.push_control(Event::Stop(StopCause::Requested));
let kinds: Vec<&str> = events.map(|event| event.kind_name()).collect();
assert_eq!(kinds, vec!["stop"], "the fuse stops the iteration");
}
#[test]
fn dropping_the_stream_marks_the_source_abandoned() {
let (source, events) = stream();
assert!(!source.is_abandoned());
drop(events);
assert!(source.is_abandoned());
}
#[test]
fn stats_track_delivery() {
let (source, mut events) = stream();
source.push_input(&DataId::new("frames").unwrap(), message());
assert_eq!(events.stats().delivered, 0);
let _ = events.try_recv();
assert_eq!(events.stats().delivered, 1);
assert!(std::ptr::eq(
Arc::as_ptr(events.source()),
Arc::as_ptr(&source)
));
}
#[tokio::test(flavor = "current_thread")]
async fn a_blocking_recv_in_a_current_thread_runtime_is_refused() {
let source = Arc::new(EventSource::new());
let runtime = NodeRuntime::acquire().unwrap();
let mut events = EventStream::new(source, runtime);
let error = events.recv_checked().unwrap_err();
assert!(matches!(error, NodeError::BlockingInAsync { .. }));
let event = events.recv().unwrap();
assert!(matches!(event, Event::Error(_)));
assert!(events.blocking_refused());
assert!(events.recv().is_none());
let mut iterations = 0;
while events.next().is_some() {
iterations += 1;
assert!(iterations < 4, "the loop never terminated");
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn recv_async_awaits_an_event() {
let source = Arc::new(EventSource::new());
source
.register_raw(
DataId::new("frames").unwrap(),
4,
QueuePolicy::DropOldest,
PriorityLane::Data,
)
.unwrap();
let runtime = NodeRuntime::acquire().unwrap();
let mut events = EventStream::new(Arc::clone(&source), runtime);
let producer = Arc::clone(&source);
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(5)).await;
producer.push_input(&DataId::new("frames").unwrap(), message());
});
let event = events.recv_async().await.unwrap();
assert!(event.is_input());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn recv_async_timeout_gives_up() {
let source = Arc::new(EventSource::new());
let runtime = NodeRuntime::acquire().unwrap();
let mut events = EventStream::new(source, runtime);
assert!(
events
.recv_async_timeout(Duration::from_millis(10))
.await
.is_none()
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn the_stream_face_polls_the_same_queue() {
let source = Arc::new(EventSource::new());
let runtime = NodeRuntime::acquire().unwrap();
let mut events = EventStream::new(Arc::clone(&source), runtime);
source.push_control(Event::AllInputsClosed);
let polled = core::future::poll_fn(|cx| {
let pinned = core::pin::Pin::new(&mut events);
Stream::poll_next(pinned, cx)
})
.await;
assert!(matches!(polled, Some(Event::AllInputsClosed)));
assert_eq!(Stream::size_hint(&events), (0, None));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn external_streams_merge_onto_the_control_lane() {
let source = Arc::new(EventSource::new());
let runtime = NodeRuntime::acquire().unwrap();
let mut events = EventStream::new(source, runtime);
let sender = events.merge_external_channel(4);
sender
.send(Event::Error("external".to_owned()))
.await
.unwrap();
let event = events.recv_async().await.unwrap();
let Event::Error(message) = event else {
panic!("expected the merged event");
};
assert_eq!(message, "external");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_merged_receiver_can_be_supplied_directly() {
let source = Arc::new(EventSource::new());
let runtime = NodeRuntime::acquire().unwrap();
let mut events = EventStream::new(source, runtime);
let (sender, receiver) = tokio::sync::mpsc::channel(4);
events.merge_external(receiver);
sender.send(Event::AllInputsClosed).await.unwrap();
assert!(matches!(
events.recv_async().await,
Some(Event::AllInputsClosed)
));
drop(sender);
}
}