From 4c478b1f77a22451ff9a4bc8fc376e1ddec26f01 Mon Sep 17 00:00:00 2001 From: Waffle Date: Sun, 6 Oct 2019 11:19:48 +0300 Subject: [PATCH] Rewrite simple dispatcher's test for a bit --- src/dispatcher/simple/mod.rs | 59 ++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/dispatcher/simple/mod.rs b/src/dispatcher/simple/mod.rs index 79ec7bb9..87d8575f 100644 --- a/src/dispatcher/simple/mod.rs +++ b/src/dispatcher/simple/mod.rs @@ -223,17 +223,38 @@ where #[cfg(test)] mod tests { use std::convert::Infallible; + use std::sync::atomic::{AtomicI32, Ordering}; + + use crate::{ + types::{ + Message, ChatKind, MessageKind, Sender, ForwardKind, MediaKind, Chat, User, Update, UpdateKind + }, + dispatcher::{simple::{Dispatcher, error_policy::ErrorPolicy}, updater::StreamUpdater}, + }; + use futures::Stream; #[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}, - }; + async fn first_handler_executes_1_time() { + let counter = &AtomicI32::new(0); + let counter2 = &AtomicI32::new(0); - let mes = Message { + let mut dp = Dispatcher::::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(()) - } - - let mut dp = Dispatcher::::new(ErrorPolicy::Ignore) - .message_handler(true, handler) - .message_handler(true, handler2); + fn message_update() -> Update { + Update { id: 0, kind: UpdateKind::Message(message()) } + } + fn one_message_updater() -> StreamUpdater>> { 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()))) + ) } }