mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-23 06:51:01 +01:00
Rewrite simple dispatcher's test for a bit
This commit is contained in:
parent
4c00f48339
commit
4c478b1f77
1 changed files with 37 additions and 22 deletions
|
@ -223,17 +223,38 @@ where
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::convert::Infallible;
|
||||
use std::sync::atomic::{AtomicI32, Ordering};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test() {
|
||||
use crate::{
|
||||
types::{
|
||||
Message, ChatKind, MessageKind, Sender, ForwardKind, MediaKind, Chat, User, Update, UpdateKind
|
||||
},
|
||||
dispatcher::{simple::{Dispatcher, error_policy::ErrorPolicy}, updater::StreamUpdater},
|
||||
};
|
||||
use futures::Stream;
|
||||
|
||||
let mes = Message {
|
||||
#[tokio::test]
|
||||
async fn first_handler_executes_1_time() {
|
||||
let counter = &AtomicI32::new(0);
|
||||
let counter2 = &AtomicI32::new(0);
|
||||
|
||||
let mut dp = Dispatcher::<Infallible>::new(ErrorPolicy::Ignore)
|
||||
.message_handler(true, |_mes: Message| async move {
|
||||
counter.fetch_add(1, Ordering::SeqCst);
|
||||
})
|
||||
.message_handler(true, |_mes: Message| async move {
|
||||
counter2.fetch_add(1, Ordering::SeqCst);
|
||||
Ok::<_, Infallible>(())
|
||||
});
|
||||
|
||||
dp.dispatch(one_message_updater()).await;
|
||||
|
||||
assert_eq!(counter.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(counter2.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
||||
fn message() -> Message {
|
||||
Message {
|
||||
id: 6534,
|
||||
date: 1567898953,
|
||||
chat: Chat {
|
||||
|
@ -265,25 +286,19 @@ mod tests {
|
|||
},
|
||||
reply_markup: None,
|
||||
},
|
||||
};
|
||||
|
||||
async fn handler(mes: Message) {
|
||||
println!("{:#?}", mes)
|
||||
}
|
||||
}
|
||||
|
||||
async fn handler2(mes: Message) -> Result<(), Infallible>{
|
||||
println!("{:#?}", mes);
|
||||
|
||||
Ok(())
|
||||
fn message_update() -> Update {
|
||||
Update { id: 0, kind: UpdateKind::Message(message()) }
|
||||
}
|
||||
|
||||
let mut dp = Dispatcher::<Infallible>::new(ErrorPolicy::Ignore)
|
||||
.message_handler(true, handler)
|
||||
.message_handler(true, handler2);
|
||||
|
||||
fn one_message_updater() -> StreamUpdater<impl Stream<Item=Result<Update, Infallible>>> {
|
||||
use futures::future::ready;
|
||||
use futures::stream;
|
||||
|
||||
dp.dispatch(StreamUpdater::new(stream::once(ready(Result::<_, ()>::Ok(Update { id: 0, kind: UpdateKind::Message(mes) }))))).await;
|
||||
StreamUpdater::new(
|
||||
stream::once(ready(Ok(message_update())))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue