verdure-context 0.0.5

An ecosystem framework for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
//! Event broadcasting system
//!
//! This module provides a comprehensive event broadcasting system for the Verdure context.
//! It supports application-wide event publishing and subscription, enabling decoupled
//! communication between different parts of the application.

use dashmap::DashMap;
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::sync::Arc;

/// Built-in context lifecycle events

/// Event fired when the application context starts initialization
///
/// This event is published at the very beginning of the context initialization
/// process, before any actual initialization work begins.
#[derive(Debug, Clone)]
pub struct ContextInitializingEvent {
    /// Number of configuration sources to be loaded
    pub config_sources_count: usize,
    /// Initialization start timestamp
    pub timestamp: std::time::SystemTime,
}

impl Event for ContextInitializingEvent {
    fn name(&self) -> &'static str {
        "ContextInitializing"
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }
}

/// Event fired when the application context is initialized
///
/// This event is published after the context has been fully initialized,
/// including all configuration sources and the IoC container.
#[derive(Debug, Clone)]
pub struct ContextInitializedEvent {
    /// Number of configuration sources loaded
    pub config_sources_count: usize,
    /// Initialization timestamp
    pub timestamp: std::time::SystemTime,
}

impl Event for ContextInitializedEvent {
    fn name(&self) -> &'static str {
        "ContextInitialized"
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }
}

/// Event fired when configuration is changed at runtime
///
/// This event is published when configuration values are updated after
/// the initial context setup.
#[derive(Debug, Clone)]
pub struct ConfigurationChangedEvent {
    /// The configuration key that was changed
    pub key: String,
    /// The old value (if any)
    pub old_value: Option<String>,
    /// The new value
    pub new_value: String,
    /// Change timestamp
    pub timestamp: std::time::SystemTime,
}

impl Event for ConfigurationChangedEvent {
    fn name(&self) -> &'static str {
        "ConfigurationChanged"
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }
}

/// Event trait that all events must implement
///
/// This trait allows events to be stored and transmitted in a type-safe manner
/// while supporting downcasting to the concrete event type.
///
/// # Examples
///
/// ```rust
/// use verdure_context::Event;
/// use std::any::Any;
///
/// #[derive(Debug, Clone)]
/// struct UserCreatedEvent {
///     pub user_id: u64,
///     pub username: String,
/// }
///
/// impl Event for UserCreatedEvent {
///     fn name(&self) -> &'static str {
///         "UserCreated"
///     }
///     
///     fn as_any(&self) -> &dyn Any {
///         self
///     }
///     
///     fn into_any(self: Box<Self>) -> Box<dyn Any> {
///         self
///     }
/// }
/// ```
pub trait Event: Any + Send + Sync {
    /// Returns the name of the event type
    ///
    /// This should be a unique identifier for the event type.
    fn name(&self) -> &'static str;

    /// Returns the event as `Any` for downcasting
    fn as_any(&self) -> &dyn Any;

    /// Converts the event to `Any` for downcasting (consuming)
    fn into_any(self: Box<Self>) -> Box<dyn Any>;
}

/// Event listener trait
///
/// Implement this trait to handle specific event types. The listener will be
/// called whenever an event of the specified type is published.
///
/// # Examples
///
/// ```rust
/// use verdure_context::{Event, EventListener};
/// use std::any::Any;
///
/// #[derive(Debug, Clone)]
/// struct MyEvent {
///     data: String,
/// }
///
/// impl Event for MyEvent {
///     fn name(&self) -> &'static str {
///         "MyEvent"
///     }
///     
///     fn as_any(&self) -> &dyn Any {
///         self
///     }
///     
///     fn into_any(self: Box<Self>) -> Box<dyn Any> {
///         self
///     }
/// }
///
/// struct MyEventListener;
///
/// impl EventListener<MyEvent> for MyEventListener {
///     fn on_event(&self, event: &MyEvent) {
///         println!("Received event with data: {}", event.data);
///     }
/// }
/// ```
pub trait EventListener<T: Event>: Send + Sync {
    /// Called when an event of type `T` is published
    ///
    /// # Arguments
    ///
    /// * `event` - The event instance
    fn on_event(&self, event: &T);
}

/// Type-erased event listener
///
/// This allows storing listeners of different event types in the same collection.
pub trait AnyEventListener: Send + Sync {
    /// Handles an event if the listener is registered for this event type
    ///
    /// # Arguments
    ///
    /// * `event` - The event to handle
    ///
    /// # Returns
    ///
    /// `true` if the listener handled the event, `false` otherwise
    fn handle_event(&self, event: &dyn Event) -> bool;

    /// Returns the TypeId of the event type this listener handles
    fn event_type_id(&self) -> TypeId;
}

/// Context-aware event listener trait for lifecycle events
///
/// This trait allows event listeners to receive a reference to the ApplicationContext
/// along with the event, enabling them to interact with the context during lifecycle events.
///
/// # Examples
///
/// ```rust
/// use verdure_context::{ContextAwareEventListener, ContextInitializedEvent, ApplicationContext};
///
/// struct StartupListener;
///
/// impl ContextAwareEventListener<ContextInitializedEvent> for StartupListener {
///     fn on_context_event(&self, event: &ContextInitializedEvent, context: &ApplicationContext) {
///         println!("Context initialized with {} sources", event.config_sources_count);
///         println!("App name: {}", context.get_config("app.name"));
///         
///         // Can access IoC container
///         let container = context.container();
///         // Can access configuration
///         let environment = context.environment();
///         
///         println!("Running in environment: {}", environment);
///     }
/// }
/// ```
pub trait ContextAwareEventListener<T: Event>: Send + Sync {
    /// Called when an event of type `T` is published, with access to the ApplicationContext
    ///
    /// # Arguments
    ///
    /// * `event` - The event instance
    /// * `context` - Reference to the ApplicationContext
    fn on_context_event(&self, event: &T, context: &crate::context::ApplicationContext);
}

/// Type-erased context-aware event listener
///
/// This allows storing context-aware listeners of different event types in the same collection.
///
/// This allows storing listeners of different event types in the same collection.
/// Type-erased context-aware event listener
///
/// This allows storing context-aware listeners of different event types in the same collection.
pub trait AnyContextAwareEventListener: Send + Sync {
    /// Handles an event with context access if the listener is registered for this event type
    ///
    /// # Arguments
    ///
    /// * `event` - The event to handle
    /// * `context` - Reference to the ApplicationContext
    ///
    /// # Returns
    ///
    /// `true` if the listener handled the event, `false` otherwise
    fn handle_context_event(
        &self,
        event: &dyn Event,
        context: &crate::context::ApplicationContext,
    ) -> bool;

    /// Returns the TypeId of the event type this listener handles
    fn event_type_id(&self) -> TypeId;
}

/// Implementation of `AnyContextAwareEventListener` for specific context-aware event listeners
struct TypedContextAwareEventListener<T: Event, L: ContextAwareEventListener<T>> {
    listener: L,
    _phantom: std::marker::PhantomData<T>,
}

impl<T: Event, L: ContextAwareEventListener<T>> TypedContextAwareEventListener<T, L> {
    fn new(listener: L) -> Self {
        Self {
            listener,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<T: Event + 'static, L: ContextAwareEventListener<T>> AnyContextAwareEventListener
    for TypedContextAwareEventListener<T, L>
{
    fn handle_context_event(
        &self,
        event: &dyn Event,
        context: &crate::context::ApplicationContext,
    ) -> bool {
        if let Some(typed_event) = event.as_any().downcast_ref::<T>() {
            self.listener.on_context_event(typed_event, context);
            true
        } else {
            false
        }
    }

    fn event_type_id(&self) -> TypeId {
        TypeId::of::<T>()
    }
}

/// Implementation of `AnyEventListener` for specific event listeners
struct TypedEventListener<T: Event, L: EventListener<T>> {
    listener: L,
    _phantom: std::marker::PhantomData<T>,
}

impl<T: Event + 'static, L: EventListener<T>> TypedEventListener<T, L> {
    fn new(listener: L) -> Self {
        Self {
            listener,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<T: Event + 'static, L: EventListener<T>> AnyEventListener for TypedEventListener<T, L> {
    fn handle_event(&self, event: &dyn Event) -> bool {
        if let Some(typed_event) = event.as_any().downcast_ref::<T>() {
            self.listener.on_event(typed_event);
            true
        } else {
            false
        }
    }

    fn event_type_id(&self) -> TypeId {
        TypeId::of::<T>()
    }
}

/// Event publisher for broadcasting events
///
/// `EventPublisher` manages event listeners and provides functionality to publish
/// events to all registered listeners. It supports type-safe event handling and
/// allows multiple listeners for the same event type.
///
/// # Examples
///
/// ```rust
/// use verdure_context::{EventPublisher, Event, EventListener};
/// use std::any::Any;
///
/// #[derive(Debug, Clone)]
/// struct TestEvent {
///     message: String,
/// }
///
/// impl Event for TestEvent {
///     fn name(&self) -> &'static str {
///         "TestEvent"
///     }
///     
///     fn as_any(&self) -> &dyn Any {
///         self
///     }
///     
///     fn into_any(self: Box<Self>) -> Box<dyn Any> {
///         self
///     }
/// }
///
/// struct TestListener;
///
/// impl EventListener<TestEvent> for TestListener {
///     fn on_event(&self, event: &TestEvent) {
///         println!("Received: {}", event.message);
///     }
/// }
///
/// let mut publisher = EventPublisher::new();
/// publisher.subscribe(TestListener);
///
/// let event = TestEvent {
///     message: "Hello, World!".to_string(),
/// };
/// publisher.publish(&event);
/// ```
pub struct EventPublisher {
    /// Event listeners organized by event type
    listeners: DashMap<TypeId, Vec<Arc<dyn AnyEventListener>>>,
    /// Context-aware event listeners organized by event type
    context_aware_listeners: DashMap<TypeId, Vec<Arc<dyn AnyContextAwareEventListener>>>,
}

impl EventPublisher {
    /// Creates a new event publisher
    ///
    /// # Examples
    ///
    /// ```rust
    /// use verdure_context::EventPublisher;
    ///
    /// let publisher = EventPublisher::new();
    /// ```
    pub fn new() -> Self {
        Self {
            listeners: DashMap::new(),
            context_aware_listeners: DashMap::new(),
        }
    }

    /// Subscribes a context-aware listener to events of type `T`
    ///
    /// Context-aware listeners receive both the event and a reference to the ApplicationContext,
    /// allowing them to interact with the context during event handling.
    ///
    /// # Arguments
    ///
    /// * `listener` - The context-aware event listener to register
    ///
    /// # Examples
    ///
    /// ```rust
    /// use verdure_context::{EventPublisher, ContextInitializedEvent, ContextAwareEventListener, ApplicationContext};
    ///
    /// struct StartupListener;
    ///
    /// impl ContextAwareEventListener<ContextInitializedEvent> for StartupListener {
    ///     fn on_context_event(&self, event: &ContextInitializedEvent, context: &ApplicationContext) {
    ///         println!("Context initialized with {} sources", event.config_sources_count);
    ///         println!("App name: {}", context.get_config("app.name"));
    ///     }
    /// }
    ///
    /// let mut publisher = EventPublisher::new();
    /// publisher.subscribe_context_aware(StartupListener);
    /// ```
    pub fn subscribe_context_aware<
        T: Event + 'static,
        L: ContextAwareEventListener<T> + 'static,
    >(
        &self,
        listener: L,
    ) {
        let type_id = TypeId::of::<T>();
        let typed_listener = Arc::new(TypedContextAwareEventListener::new(listener));

        self.context_aware_listeners
            .entry(type_id)
            .or_insert_with(Vec::new)
            .push(typed_listener);
    }
    ///
    /// # Arguments
    ///
    /// * `listener` - The event listener to register
    ///
    /// # Examples
    ///
    /// ```rust
    /// use verdure_context::{EventPublisher, Event, EventListener};
    /// use std::any::Any;
    ///
    /// #[derive(Debug, Clone)]
    /// struct MyEvent;
    ///
    /// impl Event for MyEvent {
    ///     fn name(&self) -> &'static str { "MyEvent" }
    ///     fn as_any(&self) -> &dyn Any { self }
    ///     fn into_any(self: Box<Self>) -> Box<dyn Any> { self }
    /// }
    ///
    /// struct MyListener;
    ///
    /// impl EventListener<MyEvent> for MyListener {
    ///     fn on_event(&self, _event: &MyEvent) {
    ///         println!("Event received!");
    ///     }
    /// }
    ///
    /// let mut publisher = EventPublisher::new();
    /// publisher.subscribe(MyListener);
    /// ```
    pub fn subscribe<T: Event + 'static, L: EventListener<T> + 'static>(&self, listener: L) {
        let type_id = TypeId::of::<T>();
        let typed_listener = Arc::new(TypedEventListener::new(listener));

        self.listeners
            .entry(type_id)
            .or_insert_with(Vec::new)
            .push(typed_listener);
    }

    /// Publishes an event to all registered listeners with context access
    ///
    /// This method publishes the event to both regular listeners and context-aware listeners.
    /// Context-aware listeners will receive a reference to the ApplicationContext.
    ///
    /// # Arguments
    ///
    /// * `event` - The event to publish
    /// * `context` - Reference to the ApplicationContext
    pub fn publish_with_context<T: Event + 'static>(
        &self,
        event: &T,
        context: &crate::context::ApplicationContext,
    ) {
        let type_id = TypeId::of::<T>();

        // Publish to regular listeners
        if let Some(listeners) = self.listeners.get(&type_id) {
            for listener in listeners.iter() {
                listener.handle_event(event);
            }
        }

        // Publish to context-aware listeners
        if let Some(context_listeners) = self.context_aware_listeners.get(&type_id) {
            for listener in context_listeners.iter() {
                listener.handle_context_event(event, context);
            }
        }
    }
    ///
    /// # Arguments
    ///
    /// * `event` - The event to publish
    ///
    /// # Examples
    ///
    /// ```rust
    /// use verdure_context::{EventPublisher, Event, EventListener};
    /// use std::any::Any;
    ///
    /// #[derive(Debug, Clone)]
    /// struct NotificationEvent {
    ///     message: String,
    /// }
    ///
    /// impl Event for NotificationEvent {
    ///     fn name(&self) -> &'static str { "Notification" }
    ///     fn as_any(&self) -> &dyn Any { self }
    ///     fn into_any(self: Box<Self>) -> Box<dyn Any> { self }
    /// }
    ///
    /// let publisher = EventPublisher::new();
    /// let event = NotificationEvent {
    ///     message: "System startup complete".to_string(),
    /// };
    ///
    /// publisher.publish(&event);
    /// ```
    pub fn publish<T: Event + 'static>(&self, event: &T) {
        let type_id = TypeId::of::<T>();

        if let Some(listeners) = self.listeners.get(&type_id) {
            for listener in listeners.iter() {
                listener.handle_event(event);
            }
        }
    }

    /// Gets the number of listeners for a specific event type
    ///
    /// # Returns
    ///
    /// The number of listeners registered for the event type `T`
    ///
    /// # Examples
    ///
    /// ```rust
    /// use verdure_context::{EventPublisher, Event, EventListener};
    /// use std::any::Any;
    ///
    /// #[derive(Debug, Clone)]
    /// struct CountEvent;
    ///
    /// impl Event for CountEvent {
    ///     fn name(&self) -> &'static str { "CountEvent" }
    ///     fn as_any(&self) -> &dyn Any { self }
    ///     fn into_any(self: Box<Self>) -> Box<dyn Any> { self }
    /// }
    ///
    /// struct CountListener;
    ///
    /// impl EventListener<CountEvent> for CountListener {
    ///     fn on_event(&self, _event: &CountEvent) {}
    /// }
    ///
    /// let mut publisher = EventPublisher::new();
    /// assert_eq!(publisher.listener_count::<CountEvent>(), 0);
    ///
    /// publisher.subscribe(CountListener);
    /// assert_eq!(publisher.listener_count::<CountEvent>(), 1);
    /// ```
    pub fn listener_count<T: Event + 'static>(&self) -> usize {
        let type_id = TypeId::of::<T>();
        self.listeners
            .get(&type_id)
            .map(|listeners| listeners.len())
            .unwrap_or(0)
    }

    /// Removes all listeners for all event types
    ///
    /// # Examples
    ///
    /// ```rust
    /// use verdure_context::EventPublisher;
    ///
    /// let mut publisher = EventPublisher::new();
    /// publisher.clear_all_listeners();
    /// ```
    pub fn clear_all_listeners(&mut self) {
        self.listeners.clear();
    }

    /// Gets statistics about registered listeners
    ///
    /// # Returns
    ///
    /// A map of event type names to listener counts
    ///
    /// # Examples
    ///
    /// ```rust
    /// use verdure_context::EventPublisher;
    ///
    /// let publisher = EventPublisher::new();
    /// let stats = publisher.listener_statistics();
    /// println!("Listener statistics: {:?}", stats);
    /// ```
    pub fn listener_statistics(&self) -> HashMap<String, usize> {
        let mut stats = HashMap::new();

        for entry in self.listeners.iter() {
            let type_id = entry.key();
            let listeners = entry.value();

            // We can't easily get the type name from TypeId, so we'll use the TypeId debug format
            let type_name = format!("{:?}", type_id);
            stats.insert(type_name, listeners.len());
        }

        stats
    }
}

impl Default for EventPublisher {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[derive(Debug, Clone)]
    struct TestEvent {
        message: String,
    }

    impl Event for TestEvent {
        fn name(&self) -> &'static str {
            "TestEvent"
        }

        fn as_any(&self) -> &dyn Any {
            self
        }

        fn into_any(self: Box<Self>) -> Box<dyn Any> {
            self
        }
    }

    #[derive(Debug, Clone)]
    struct AnotherEvent {
        value: i32,
    }

    impl Event for AnotherEvent {
        fn name(&self) -> &'static str {
            "AnotherEvent"
        }

        fn as_any(&self) -> &dyn Any {
            self
        }

        fn into_any(self: Box<Self>) -> Box<dyn Any> {
            self
        }
    }

    static TEST_COUNTER: AtomicUsize = AtomicUsize::new(0);

    struct TestListener;

    impl EventListener<TestEvent> for TestListener {
        fn on_event(&self, _event: &TestEvent) {
            TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
        }
    }

    struct AnotherListener;

    impl EventListener<AnotherEvent> for AnotherListener {
        fn on_event(&self, _event: &AnotherEvent) {
            TEST_COUNTER.fetch_add(10, Ordering::SeqCst);
        }
    }

    #[test]
    fn test_event_publisher_creation() {
        let publisher = EventPublisher::new();
        assert_eq!(publisher.listener_count::<TestEvent>(), 0);
    }

    #[test]
    fn test_event_subscription() {
        let publisher = EventPublisher::new();
        publisher.subscribe(TestListener);

        assert_eq!(publisher.listener_count::<TestEvent>(), 1);
        assert_eq!(publisher.listener_count::<AnotherEvent>(), 0);
    }

    #[test]
    fn test_event_publishing() {
        TEST_COUNTER.store(0, Ordering::SeqCst);

        let publisher = EventPublisher::new();
        publisher.subscribe(TestListener);

        let event = TestEvent {
            message: "test".to_string(),
        };

        publisher.publish(&event);

        assert_eq!(TEST_COUNTER.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn test_multiple_listeners_same_event() {
        TEST_COUNTER.store(0, Ordering::SeqCst);

        let publisher = EventPublisher::new();
        publisher.subscribe(TestListener);
        publisher.subscribe(TestListener);

        let event = TestEvent {
            message: "test".to_string(),
        };

        publisher.publish(&event);

        assert_eq!(TEST_COUNTER.load(Ordering::SeqCst), 2);
        assert_eq!(publisher.listener_count::<TestEvent>(), 2);
    }

    #[test]
    fn test_different_event_types() {
        TEST_COUNTER.store(0, Ordering::SeqCst);

        let publisher = EventPublisher::new();
        publisher.subscribe(TestListener);
        publisher.subscribe(AnotherListener);

        let test_event = TestEvent {
            message: "test".to_string(),
        };
        let another_event = AnotherEvent { value: 42 };

        publisher.publish(&test_event);
        publisher.publish(&another_event);

        // TestListener adds 1, AnotherListener adds 10
        assert_eq!(TEST_COUNTER.load(Ordering::SeqCst), 11);
    }

    #[test]
    fn test_event_without_listeners() {
        let publisher = EventPublisher::new();
        let event = TestEvent {
            message: "no listeners".to_string(),
        };

        // Should not panic
        publisher.publish(&event);
    }

    #[test]
    fn test_clear_listeners() {
        let mut publisher = EventPublisher::new();
        publisher.subscribe(TestListener);
        publisher.subscribe(AnotherListener);

        assert_eq!(publisher.listener_count::<TestEvent>(), 1);
        assert_eq!(publisher.listener_count::<AnotherEvent>(), 1);

        publisher.clear_all_listeners();

        assert_eq!(publisher.listener_count::<TestEvent>(), 0);
        assert_eq!(publisher.listener_count::<AnotherEvent>(), 0);
    }

    #[test]
    fn test_listener_statistics() {
        let publisher = EventPublisher::new();
        publisher.subscribe(TestListener);
        publisher.subscribe(AnotherListener);

        let stats = publisher.listener_statistics();
        assert_eq!(stats.len(), 2);
    }

    #[test]
    fn test_event_trait_methods() {
        let event = TestEvent {
            message: "test".to_string(),
        };

        assert_eq!(event.name(), "TestEvent");
        assert!(event.as_any().is::<TestEvent>());
    }
}