use std::thread;
use std::thread::sleep;
use std::time::Duration;
use crate::sync::prelude::*;
struct HelloEvent {
message: String,
}
struct UpdateEvent {
message: String,
}
impl ApplicationEvent for HelloEvent {
fn topic() -> String {
String::from("io.github.eventbuzz.global.hello.topic")
}
}
impl ApplicationEvent for UpdateEvent {
fn topic() -> String {
String::from("io.github.eventbuzz.global.hello.topic")
}
}
struct HelloEventListener;
struct GreetingEventListener;
struct UpdateEventListener;
impl ApplicationEventListener<HelloEvent> for HelloEventListener {
fn on_application_event(&self, event: &HelloEvent) {
sleep(Duration::from_secs(1));
println!(
"sync: thread.current.id: {:?}, HelloEventListener: Received event with message: {}",
thread::current().id(),
event.message
);
}
}
impl ApplicationEventListener<HelloEvent> for GreetingEventListener {
fn on_application_event(&self, event: &HelloEvent) {
sleep(Duration::from_secs(3));
println!(
"sync: thread.current.id: {:?}, GreetingEventListener: Received event with message: {}",
thread::current().id(),
event.message
);
}
}
impl ApplicationEventListener<UpdateEvent> for UpdateEventListener {
fn on_application_event(&self, event: &UpdateEvent) {
sleep(Duration::from_secs(1));
println!(
"sync: thread.current.id: {:?}, UpdateEventListener: Received event with message: {}",
thread::current().id(),
event.message
);
}
}
#[test]
#[cfg(feature = "synchronous")]
fn test_eventbus_pub_sub() {
let mut eventbus: Eventbus = Eventbus::builder()
.build();
eventbus.register_listener(HelloEventListener);
eventbus.register_listener(GreetingEventListener);
eventbus.register_listener(UpdateEventListener);
println!(
"sync: prepare.sync.publish.event, thread.current.id: {:?}",
thread::current().id()
);
println!("sync: --- prepare.sync.publish.HelloEvent ---");
eventbus.publish_event(HelloEvent {
message: String::from("Hello, Rust!"),
});
println!("sync: --- prepare.sync.publish.UpdateEvent ---");
eventbus.publish_event(UpdateEvent {
message: String::from("Hello, Rust!"),
});
println!("sync: --- post.sync.publish.UpdateEvent ---");
}