use std::{ fmt::Debug, ops::{Deref, DerefMut}, time::Duration, }; use tokio::time::timeout; use crate::{Message, Receiver}; pub struct TestReceiver { recv: Receiver, } impl From> for TestReceiver { fn from(recv: Receiver) -> Self { Self { recv } } } impl Deref for TestReceiver { type Target = Receiver; fn deref(&self) -> &Self::Target { &self.recv } } impl DerefMut for TestReceiver { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.recv } } impl TestReceiver { pub async fn expect_response(&mut self, response: Res) { let request = timeout(Duration::from_secs(3), self.recv()) .await .expect("Timeout to receive request") .expect("Channel closed"); match request { Message::Notification(..) => panic!("Unexpected message type"), Message::Request(_, resp_sender) => { resp_sender.send(response).expect("Channel closed"); } } } pub async fn expect_responses(&mut self, responses: Vec) { for resp in responses { self.expect_response(resp).await; } } }