use crate::errors::{BusRequestError, BusRequestErrorCode, SemiontError};
use crate::transport::BoxFuture;
use futures_core::Stream;
use std::future::{Future, IntoFuture};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::sync::mpsc;
pub struct Reporter<T> {
reports: mpsc::UnboundedSender<T>,
}
impl<T> Reporter<T> {
pub fn report(&self, value: T) {
let _ = self.reports.send(value);
}
}
pub struct Running<T, F = T> {
reports: mpsc::UnboundedReceiver<T>,
work: Option<BoxFuture<'static, Result<F, SemiontError>>>,
outcome: Option<Result<F, SemiontError>>,
}
impl<T, F> Unpin for Running<T, F> {}
impl<T: Send + 'static, F: Send + 'static> Running<T, F> {
pub fn new<W, Fut>(work: W) -> Running<T, F>
where
W: FnOnce(Reporter<T>) -> Fut,
Fut: Future<Output = Result<F, SemiontError>> + Send + 'static,
{
let (reports, receiver) = mpsc::unbounded_channel();
Running {
reports: receiver,
work: Some(Box::pin(work(Reporter { reports }))),
outcome: None,
}
}
fn drive(&mut self, cx: &mut Context<'_>) {
if let Some(work) = self.work.as_mut()
&& let Poll::Ready(outcome) = work.as_mut().poll(cx)
{
self.outcome = Some(outcome);
self.work = None;
}
}
}
impl<T: Send + 'static, F: Into<T> + Send + 'static> Running<T, F> {
pub async fn next(&mut self) -> Option<Result<T, SemiontError>> {
std::future::poll_fn(|cx| Pin::new(&mut *self).poll_next(cx)).await
}
}
impl<T: Send + 'static> Running<T> {
pub async fn run(mut self, mut on_each: impl FnMut(&T)) -> Result<T, SemiontError> {
let mut last = None;
while let Some(item) = self.next().await {
let value = item?;
on_each(&value);
last = Some(value);
}
last.ok_or_else(ended_without_a_value)
}
}
fn ended_without_a_value() -> SemiontError {
BusRequestError::new(
BusRequestErrorCode::Closed,
"The operation ended before it gave a value",
)
.into()
}
impl<T: Send + 'static, F: Into<T> + Send + 'static> Stream for Running<T, F> {
type Item = Result<T, SemiontError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.drive(cx);
match self.reports.poll_recv(cx) {
Poll::Ready(Some(report)) => Poll::Ready(Some(Ok(report))),
Poll::Ready(None) | Poll::Pending if self.work.is_none() => {
Poll::Ready(self.outcome.take().map(|outcome| outcome.map(Into::into)))
}
Poll::Ready(None) | Poll::Pending => Poll::Pending,
}
}
}
impl<T: Send + 'static, F: Send + 'static> IntoFuture for Running<T, F> {
type Output = Result<F, SemiontError>;
type IntoFuture = BoxFuture<'static, Self::Output>;
fn into_future(mut self) -> Self::IntoFuture {
Box::pin(async move {
std::future::poll_fn(|cx| {
self.drive(cx);
match self.outcome.take() {
Some(outcome) => Poll::Ready(outcome),
None => Poll::Pending,
}
})
.await
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn refused() -> SemiontError {
BusRequestError::new(BusRequestErrorCode::Rejected, "refused").into()
}
fn counting_to(last: u32) -> Running<u32> {
Running::new(move |reporter| async move {
for n in 1..last {
reporter.report(n);
tokio::task::yield_now().await;
}
Ok(last)
})
}
#[tokio::test]
async fn awaited_it_gives_the_final_value() {
assert_eq!(counting_to(3).await, Ok(3));
}
#[tokio::test]
async fn as_a_stream_it_gives_every_report_then_the_final_value_then_ends() {
let mut running = counting_to(3);
let mut seen = Vec::new();
while let Some(item) = running.next().await {
seen.push(item);
}
assert_eq!(seen, [Ok(1), Ok(2), Ok(3)]);
assert_eq!(running.next().await, None);
}
#[tokio::test]
async fn run_gives_each_value_in_order_and_returns_the_last() {
let mut seen = Vec::new();
let last = counting_to(3).run(|n| seen.push(*n)).await;
assert_eq!(last, Ok(3));
assert_eq!(seen, [1, 2, 3]);
}
#[tokio::test]
async fn one_that_ends_in_another_type_gives_it_awaited_and_as_its_last_report() {
let ending = || -> Running<u64, u32> {
Running::new(|reporter| async move {
reporter.report(1_u64);
Ok(2_u32)
})
};
assert_eq!(ending().await, Ok(2_u32));
let mut running = ending();
assert_eq!(running.next().await, Some(Ok(1_u64)));
assert_eq!(running.next().await, Some(Ok(2_u64)));
assert_eq!(running.next().await, None);
}
#[tokio::test]
async fn a_failure_is_the_streams_last_item_after_what_was_reported() {
let mut running: Running<u32> = Running::new(|reporter| async move {
reporter.report(1);
Err(refused())
});
assert_eq!(running.next().await, Some(Ok(1)));
assert_eq!(running.next().await, Some(Err(refused())));
assert_eq!(running.next().await, None);
}
#[tokio::test]
async fn a_failure_is_what_awaiting_and_running_return() {
let failing = || -> Running<u32> { Running::new(|_| async { Err(refused()) }) };
assert_eq!(failing().await, Err(refused()));
assert_eq!(failing().run(|_| {}).await, Err(refused()));
}
#[tokio::test]
async fn nothing_runs_until_it_is_polled_and_dropping_it_stops_the_work() {
let (started, mut was_started) = mpsc::unbounded_channel();
let (finished, mut was_finished) = mpsc::unbounded_channel::<()>();
let mut running: Running<u32> = Running::new(move |reporter| async move {
let _ = started.send(());
reporter.report(1);
std::future::pending::<()>().await;
let _ = finished.send(());
Ok(2)
});
tokio::task::yield_now().await;
assert!(was_started.try_recv().is_err());
assert_eq!(running.next().await, Some(Ok(1)));
assert!(was_started.try_recv().is_ok());
drop(running);
assert!(was_finished.recv().await.is_none());
}
}