use tokio::sync::broadcast;
#[derive(Debug)]
pub struct Session {
#[allow(dead_code)]
id: String,
notification_tx: broadcast::Sender<String>,
}
impl Session {
pub fn new(id: String) -> Self {
let (notification_tx, _) = broadcast::channel(64);
Self {
id,
notification_tx,
}
}
#[allow(dead_code)]
pub fn id(&self) -> &str {
&self.id
}
#[allow(dead_code)]
pub fn notification_tx(&self) -> broadcast::Sender<String> {
self.notification_tx.clone()
}
pub fn subscribe_notifications(&self) -> broadcast::Receiver<String> {
self.notification_tx.subscribe()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_session_new() {
let session = Session::new("test-id".to_string());
assert_eq!(session.id(), "test-id");
}
#[test]
fn test_session_notification_channel() {
let session = Session::new("test-id".to_string());
let mut rx = session.subscribe_notifications();
let tx = session.notification_tx();
tx.send("hello".to_string()).unwrap();
let msg = rx.try_recv().unwrap();
assert_eq!(msg, "hello");
}
}