use limnus_macros::Message;
use limnus_message::{Message, MessageStorage};
use std::fmt::Debug;
#[derive(Copy, Clone, Message, Debug)]
pub struct MyMessage {
pub secret: u8,
}
#[derive(Copy, Clone, Message, Debug, PartialEq, Eq)]
pub struct AnotherMessage {
pub data: u32,
}
#[derive(Copy, Clone, Message, Debug, Eq, PartialEq)]
pub struct GenericMessage<T: 'static + Debug + Send + Sync + Copy + Clone> {
pub value: T,
}
#[test_log::test]
fn swapping() {
let mut storage = MessageStorage::new();
storage.register_message_type::<MyMessage>();
{
let msg_queue = storage
.get_mut::<MyMessage>()
.expect("we just registered MyMessage");
msg_queue.send(MyMessage { secret: 42 });
}
{
let msg_queue_to_read = storage
.get::<MyMessage>()
.expect("we just registered MyMessage");
assert_eq!(msg_queue_to_read.len_current(), 1);
let mut count = 0;
for my_message in msg_queue_to_read.iter_current() {
assert_eq!(my_message.secret, 42);
count += 1;
}
assert_eq!(count, 1);
}
storage.swap_all();
{
let queue_left = storage
.get::<MyMessage>()
.expect("we just registered MyMessage");
assert_eq!(queue_left.len_previous(), 1);
assert_eq!(queue_left.len_current(), 0);
}
storage.swap_all();
{
let empty_queue = storage
.get::<MyMessage>()
.expect("we just registered MyMessage");
assert_eq!(empty_queue.len_previous(), 0);
assert_eq!(empty_queue.len_current(), 0);
}
}
#[test_log::test]
fn send_message_with_generic_type() {
let mut storage = MessageStorage::new();
let _ = storage.send(GenericMessage { value: 42.42 });
{
let msg_queue = storage
.get::<GenericMessage<f64>>()
.expect("GenericMessage<f64> should be automatically registered");
assert_eq!(msg_queue.len_current(), 1);
let msg = msg_queue.iter_current().next().unwrap();
assert_eq!(msg.value, 42.42);
}
storage.swap_all();
{
let msg_queue = storage
.get::<GenericMessage<f64>>()
.expect("GenericMessage<f64> should be automatically registered");
assert_eq!(msg_queue.len_previous(), 1);
assert_eq!(msg_queue.len_current(), 0);
let msg = msg_queue.iter_previous().next().unwrap();
assert_eq!(msg.value, 42.42);
}
}
#[test_log::test]
fn multiple_message_types() {
let mut storage = MessageStorage::new();
storage.register_message_type::<MyMessage>();
storage.register_message_type::<AnotherMessage>();
{
let my_msg_queue = storage
.get_mut::<MyMessage>()
.expect("MyMessage should be registered");
my_msg_queue.send(MyMessage { secret: 100 });
let another_msg_queue = storage
.get_mut::<AnotherMessage>()
.expect("AnotherMessage should be registered");
another_msg_queue.send(AnotherMessage { data: 999 });
}
{
let my_msg_queue = storage
.get::<MyMessage>()
.expect("MyMessage should be registered");
assert_eq!(my_msg_queue.len_current(), 1);
let my_msg = my_msg_queue.iter_current().next().unwrap();
assert_eq!(my_msg.secret, 100);
let another_msg_queue = storage
.get::<AnotherMessage>()
.expect("AnotherMessage should be registered");
assert_eq!(another_msg_queue.len_current(), 1);
let another_msg = another_msg_queue.iter_current().next().unwrap();
assert_eq!(another_msg.data, 999);
}
storage.swap_all();
{
let my_msg_queue = storage
.get::<MyMessage>()
.expect("MyMessage should be registered");
assert_eq!(my_msg_queue.len_previous(), 1);
let my_msg = my_msg_queue.iter_previous().next().unwrap();
assert_eq!(my_msg.secret, 100);
let another_msg_queue = storage
.get::<AnotherMessage>()
.expect("AnotherMessage should be registered");
assert_eq!(another_msg_queue.len_previous(), 1);
let another_msg = another_msg_queue.iter_previous().next().unwrap();
assert_eq!(another_msg.data, 999);
}
storage.swap_all();
{
let my_msg_queue = storage
.get::<MyMessage>()
.expect("MyMessage should be registered");
assert_eq!(my_msg_queue.len_previous(), 0);
assert_eq!(my_msg_queue.len_current(), 0);
let another_msg_queue = storage
.get::<AnotherMessage>()
.expect("AnotherMessage should be registered");
assert_eq!(another_msg_queue.len_previous(), 0);
assert_eq!(another_msg_queue.len_current(), 0);
}
}