2021-06-18 21:01:13 +04:00
|
|
|
use async_trait::async_trait;
|
2021-07-16 20:21:27 +04:00
|
|
|
use messagebus::{
|
|
|
|
derive::{Error as MbError, Message},
|
|
|
|
error, receivers, AsyncHandler, Bus, Message,
|
|
|
|
};
|
2021-06-28 17:41:00 +04:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2021-07-16 20:21:27 +04:00
|
|
|
#[derive(Debug, Error, MbError)]
|
2021-06-28 17:41:00 +04:00
|
|
|
enum Error {
|
|
|
|
#[error("Error({0})")]
|
2021-07-02 20:21:53 +04:00
|
|
|
Error(anyhow::Error),
|
2021-06-28 17:41:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-07-16 20:21:27 +04:00
|
|
|
#[derive(Debug, Clone, Message)]
|
|
|
|
struct MsgF32(pub f32);
|
|
|
|
|
2020-12-17 17:35:11 +04:00
|
|
|
struct TmpReceiver;
|
|
|
|
|
2021-06-18 21:01:13 +04:00
|
|
|
#[async_trait]
|
2021-07-16 20:21:27 +04:00
|
|
|
impl AsyncHandler<MsgF32> for TmpReceiver {
|
2021-06-24 13:32:19 +04:00
|
|
|
type Error = Error;
|
2021-06-18 21:01:13 +04:00
|
|
|
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)
|
2021-07-16 20:21:27 +04:00
|
|
|
.subscribe_async::<MsgF32>(
|
|
|
|
1,
|
|
|
|
receivers::BufferUnorderedConfig {
|
|
|
|
buffer_size: 1,
|
|
|
|
max_parallel: 1,
|
|
|
|
},
|
|
|
|
)
|
2020-12-17 17:35:11 +04:00
|
|
|
.done()
|
|
|
|
.build();
|
|
|
|
|
2021-07-16 20:21:27 +04:00
|
|
|
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
|
|
|
|
2021-11-15 20:01:28 +04:00
|
|
|
b.flush_all().await;
|
2021-06-18 21:01:13 +04:00
|
|
|
b.close().await;
|
2020-12-17 17:35:11 +04:00
|
|
|
poller.await;
|
|
|
|
}
|