From bd63732e258a1ddef228bb61821b6ddf13b5576c Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Sun, 12 Feb 2023 13:15:31 +0100 Subject: [PATCH] fix server message serialization tests --- src/protos/irc/server.rs | 9 +++++++-- src/util/mod.rs | 2 ++ src/util/testkit.rs | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/util/testkit.rs diff --git a/src/protos/irc/server.rs b/src/protos/irc/server.rs index b99df48..dd53a50 100644 --- a/src/protos/irc/server.rs +++ b/src/protos/irc/server.rs @@ -206,6 +206,7 @@ mod test { use assert_matches::*; use super::*; + use crate::util::testkit::*; #[test] fn test_server_message_notice() { @@ -224,7 +225,9 @@ mod test { assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result)); let mut bytes = vec![]; - expected.write(&mut bytes).unwrap(); + sync_future(expected.write_async(&mut bytes)) + .unwrap() + .unwrap(); assert_eq!(bytes, input); } @@ -244,7 +247,9 @@ mod test { assert_matches!(result, Ok((_, result)) => assert_eq!(expected, result)); let mut bytes = vec![]; - expected.write(&mut bytes).unwrap(); + sync_future(expected.write_async(&mut bytes)) + .unwrap() + .unwrap(); assert_eq!(bytes, input); } } diff --git a/src/util/mod.rs b/src/util/mod.rs index e9d3e02..8b9f86e 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -6,6 +6,8 @@ use crate::prelude::*; pub mod http; pub mod table; pub mod telemetry; +#[cfg(test)] +pub mod testkit; pub struct Terminator { signal: Sender<()>, diff --git a/src/util/testkit.rs b/src/util/testkit.rs new file mode 100644 index 0000000..d568bea --- /dev/null +++ b/src/util/testkit.rs @@ -0,0 +1,16 @@ +use std::task::{Context, Poll}; + +use futures_util::task::noop_waker_ref; + +use crate::prelude::*; + +pub fn sync_future(future: impl Future) -> Result { + let waker = noop_waker_ref(); + let mut context = Context::from_waker(waker); + pin!(future); + if let Poll::Ready(a) = future.poll(&mut context) { + Ok(a) + } else { + Err(anyhow::Error::msg("Future has suspended")) + } +}