messagebus/tests/test_backpressure.rs

59 lines
1.3 KiB
Rust
Raw Permalink Normal View History

use async_trait::async_trait;
use messagebus::{
derive::{Error as MbError, Message},
error, receivers, AsyncHandler, Bus, Message,
};
use thiserror::Error;
#[derive(Debug, Error, MbError)]
enum Error {
#[error("Error({0})")]
Error(anyhow::Error),
}
impl<M: Message> From<error::Error<M>> for Error {
fn from(err: error::Error<M>) -> Self {
Self::Error(err.into())
}
}
2020-12-17 17:35:11 +04:00
#[derive(Debug, Clone, Message)]
struct MsgF32(pub f32);
2020-12-17 17:35:11 +04:00
struct TmpReceiver;
#[async_trait]
impl AsyncHandler<MsgF32> for TmpReceiver {
2021-06-24 13:32:19 +04:00
type Error = Error;
type Response = ();
2021-07-23 17:29:47 +04:00
async fn handle(&self, _msg: MsgF32, _bus: &Bus) -> Result<Self::Response, Self::Error> {
std::thread::sleep(std::time::Duration::from_millis(100));
2020-12-17 17:35:11 +04:00
Ok(())
}
}
2021-07-23 17:29:47 +04:00
#[tokio::test]
async fn test_backpressure() {
2020-12-17 17:35:11 +04:00
let (b, poller) = Bus::build()
.register(TmpReceiver)
.subscribe_async::<MsgF32>(
1,
receivers::BufferUnorderedConfig {
buffer_size: 1,
max_parallel: 1,
},
)
2020-12-17 17:35:11 +04:00
.done()
.build();
b.send(MsgF32(32f32)).await.unwrap();
b.send(MsgF32(32f32)).await.unwrap();
2020-12-17 17:35:11 +04:00
2021-07-23 17:29:47 +04:00
assert!(b.try_send(MsgF32(32f32)).is_err());
2020-12-17 17:35:11 +04:00
b.flush_all().await;
b.close().await;
2020-12-17 17:35:11 +04:00
poller.await;
}