2021-09-15 19:54:59 +04:00
|
|
|
use std::{sync::atomic::AtomicU32, time::Duration};
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
2021-09-20 16:48:20 +04:00
|
|
|
use messagebus::{
|
|
|
|
derive::{Error as MbError, Message},
|
|
|
|
error,
|
|
|
|
receivers::BufferUnorderedConfig,
|
|
|
|
AsyncHandler, Bus, Message, Module,
|
|
|
|
};
|
|
|
|
use thiserror::Error;
|
2021-09-15 19:54:59 +04:00
|
|
|
|
|
|
|
#[derive(Debug, Error, MbError)]
|
|
|
|
enum Error {
|
|
|
|
#[error("Error({0})")]
|
|
|
|
Error(anyhow::Error),
|
|
|
|
|
|
|
|
#[error("MyError")]
|
|
|
|
MyError,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<M: Message> From<error::Error<M>> for Error {
|
|
|
|
fn from(err: error::Error<M>) -> Self {
|
|
|
|
Self::Error(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Message)]
|
|
|
|
struct Req(pub u32);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Message)]
|
|
|
|
struct Resp(pub u32);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Message)]
|
|
|
|
struct GetCount;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Message)]
|
|
|
|
struct CountResult(pub u32);
|
|
|
|
|
|
|
|
struct TmpReceiver {
|
2021-09-20 16:48:20 +04:00
|
|
|
counter: AtomicU32,
|
2021-09-15 19:54:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl AsyncHandler<Req> for TmpReceiver {
|
|
|
|
type Error = Error;
|
|
|
|
type Response = ();
|
|
|
|
|
|
|
|
async fn handle(&self, msg: Req, bus: &Bus) -> Result<Self::Response, Self::Error> {
|
2021-09-20 16:48:20 +04:00
|
|
|
tokio::time::sleep(Duration::from_millis((msg.0 % 20) as _)).await;
|
2021-09-15 19:54:59 +04:00
|
|
|
|
2021-09-20 16:48:20 +04:00
|
|
|
if msg.0 % 128 == 0 {
|
2021-09-15 19:54:59 +04:00
|
|
|
return Err(Error::MyError);
|
|
|
|
} else {
|
|
|
|
bus.send(Resp(msg.0)).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl AsyncHandler<Resp> for TmpReceiver {
|
|
|
|
type Error = Error;
|
|
|
|
type Response = ();
|
|
|
|
|
|
|
|
async fn handle(&self, _msg: Resp, _bus: &Bus) -> Result<Self::Response, Self::Error> {
|
2021-09-20 16:48:20 +04:00
|
|
|
self.counter
|
|
|
|
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
2021-09-15 19:54:59 +04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl AsyncHandler<GetCount> for TmpReceiver {
|
|
|
|
type Error = Error;
|
|
|
|
type Response = CountResult;
|
|
|
|
|
|
|
|
async fn handle(&self, _: GetCount, _bus: &Bus) -> Result<Self::Response, Self::Error> {
|
2021-09-20 16:48:20 +04:00
|
|
|
Ok(CountResult(
|
|
|
|
self.counter.load(std::sync::atomic::Ordering::SeqCst),
|
|
|
|
))
|
2021-09-15 19:54:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn module() -> Module {
|
|
|
|
Module::new()
|
2021-09-20 16:48:20 +04:00
|
|
|
.register(TmpReceiver {
|
|
|
|
counter: AtomicU32::new(0),
|
2021-09-15 19:54:59 +04:00
|
|
|
})
|
2021-09-20 16:48:20 +04:00
|
|
|
.subscribe_async::<Req>(
|
|
|
|
1024,
|
|
|
|
BufferUnorderedConfig {
|
|
|
|
buffer_size: 1024,
|
|
|
|
max_parallel: 1024,
|
|
|
|
},
|
|
|
|
)
|
2021-09-15 19:54:59 +04:00
|
|
|
.subscribe_async::<Resp>(1024, Default::default())
|
|
|
|
.subscribe_async::<GetCount>(8, Default::default())
|
|
|
|
.done()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_sync() {
|
|
|
|
let (b, poller) = Bus::build().add_module(module()).build();
|
|
|
|
let cnt = 4u32;
|
2021-09-20 16:48:20 +04:00
|
|
|
for i in 0..cnt {
|
2021-09-15 19:54:59 +04:00
|
|
|
for j in 0..32768 {
|
|
|
|
b.send(Req(i * 128 + j)).await.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("{} sent", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("sent");
|
|
|
|
|
2021-11-15 20:01:28 +04:00
|
|
|
b.flush_all().await;
|
2021-09-15 19:54:59 +04:00
|
|
|
|
|
|
|
println!("flushed");
|
2021-09-20 16:48:20 +04:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
b.request_we::<_, CountResult, Error>(GetCount, Default::default())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.0,
|
|
|
|
cnt * 32768 - cnt * 256
|
|
|
|
);
|
2021-09-15 19:54:59 +04:00
|
|
|
|
|
|
|
b.close().await;
|
|
|
|
|
|
|
|
println!("closed");
|
|
|
|
|
|
|
|
poller.await;
|
|
|
|
}
|