diff --git a/src/modules/message_handler.rs b/src/modules/message_handler.rs index e69de29..9b28cd1 100644 --- a/src/modules/message_handler.rs +++ b/src/modules/message_handler.rs @@ -0,0 +1,31 @@ +use std::sync::mpsc::{Receiver}; + +use super::stream_states::{stream_states_class::StreamStates, enums::StateUpdate}; + +pub enum StateMessage { + StateUpdateContainer(StateUpdate), + CloseListener, +} + +pub trait MessageListnerThread { + fn listen(self, rx: Receiver) -> std::thread::JoinHandle<()>; +} + +#[derive(Debug)] +pub struct MessageHandler { + pub state: StreamStates, +} + +impl MessageHandler { + pub fn new() -> Self { + return MessageHandler { + state: StreamStates::new(), + } + } + + pub fn handle_update(&mut self, update: StateUpdate) { + self.state.update(update.clone()); + } + + +} \ No newline at end of file diff --git a/src/tests/message_handler_tests.rs b/src/tests/message_handler_tests.rs index e69de29..90ee00c 100644 --- a/src/tests/message_handler_tests.rs +++ b/src/tests/message_handler_tests.rs @@ -0,0 +1,69 @@ +use std::{sync::mpsc::{self, Receiver}, thread::{self, JoinHandle}}; + +use crate::modules::{message_handler::{MessageHandler, StateMessage}, stream_states::{enums::{StateUpdate, Scenes}}}; + + + + +#[test] +fn create_message_handler() { + MessageHandler::new(); +} + +#[test] +fn message_hander_can_handle_basic_message() { + let (tx, rx) = mpsc::channel(); + + fn listen(mut handler: MessageHandler, rx: Receiver) -> JoinHandle<()>{ + thread::spawn(move || { + for message in rx { + match message { + StateMessage::StateUpdateContainer(update) => {handler.handle_update(update)}, + StateMessage::CloseListener => {break;}, + }; + } + assert_eq!(handler.state.computer_sound_is_on, false); + }) + } + + let handler = MessageHandler::new(); + + let join_handle = listen(handler, rx); + tx.send(StateMessage::StateUpdateContainer(StateUpdate::ComputerSoundIsOn(false))).unwrap(); + thread::sleep(std::time::Duration::from_millis(1000)); + tx.send(StateMessage::CloseListener).unwrap(); + join_handle.join().unwrap(); +} + +#[test] +fn message_handler_can_handle_multiple_messages() { + let (tx, rx) = mpsc::channel(); + + + fn listen(mut handler: MessageHandler, rx: Receiver) -> JoinHandle<()>{ + thread::spawn(move || { + for message in rx { + match message { + StateMessage::StateUpdateContainer(update) => {handler.handle_update(update)}, + StateMessage::CloseListener => {break;}, + }; + } + assert_eq!(handler.state.computer_sound_is_on, false); + assert_eq!(handler.state.scene_is_augmented, true); + assert_eq!(handler.state.scene_is_augmented, true); + assert_eq!(handler.state.get_current_scene(), Scenes::Augmented); + assert_eq!(handler.state.timer_can_run, false); + }) + } + + + let handler = MessageHandler::new(); + + let join_handle = listen(handler, rx); + tx.send(StateMessage::StateUpdateContainer(StateUpdate::ComputerSoundIsOn(false))).unwrap(); + tx.send(StateMessage::StateUpdateContainer(StateUpdate::SceneIsAugmented(true))).unwrap(); + tx.send(StateMessage::StateUpdateContainer(StateUpdate::TimerCanRun(false))).unwrap(); + thread::sleep(std::time::Duration::from_millis(1000)); + tx.send(StateMessage::CloseListener).unwrap(); + join_handle.join().unwrap(); +}