nizaam-core 1.0.1

Domain-agnostic Rust infrastructure core for building secure, observable, reliable, and extensible Nizaam engines.
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
//! Publisher coordination for internal Nizaam Core events.
//!
//! The publisher owns the publication boundary for one explicitly owned
//! publisher:
//!
//! ```text
//! Owner
//!   ↓
//! EventPublisher
//!   ├── subscription registration
//!   ├── subscription removal
//!   ├── matching
//!   ├── publication admission
//!   └── publisher lifecycle
//! ```
//!
//! Delivery execution remains the responsibility of `delivery.rs`.
//! This module does not create queues, workers, retry mechanisms, ACK/NACK
//! handling, persistence, replay, transport, or authorization frameworks.
//!
//! The Event subsystem lifecycle from `events::lifecycle` and the lifecycle of
//! an individual publisher are deliberately separate:
//!
//! ```text
//! EventLifecycle
//!     → subsystem-wide Event admission
//!
//! PublisherLifecycle
//!     → one publisher's lifetime
//! ```
//!
//! A publication is represented internally as an immutable event plus the
//! subscriptions that matched it. `delivery.rs` can consume that handoff
//! without acquiring the publisher's registry lock.

use std::fmt;
use std::sync::{
    Arc, RwLock,
    atomic::{AtomicU8, Ordering},
};

use crate::{
    events::{Event, EventLifecycle, EventSubscription, SubscriptionLifecycleState},
    operation::CancellationToken,
};

const CREATED: u8 = 0;
const ACTIVE: u8 = 1;
const CLOSED: u8 = 2;

/// Lifecycle state of one event publisher.
///
/// This is deliberately separate from the Event subsystem lifecycle.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PublisherLifecycleState {
    /// The publisher exists but has not been activated.
    Created,

    /// The publisher may accept subscriptions and event publication.
    Active,

    /// The publisher is permanently closed.
    Closed,
}

impl PublisherLifecycleState {
    /// Returns whether the publisher is currently active.
    pub const fn is_active(self) -> bool {
        matches!(self, Self::Active)
    }

    /// Returns whether the publisher has reached its terminal state.
    pub const fn is_terminal(self) -> bool {
        matches!(self, Self::Closed)
    }

    /// Returns whether the requested lifecycle transition is valid.
    pub const fn can_transition_to(self, next: Self) -> bool {
        matches!(
            (self, next),
            (Self::Created, Self::Created)
                | (Self::Active, Self::Active)
                | (Self::Closed, Self::Closed)
                | (Self::Created, Self::Active)
                | (Self::Created, Self::Closed)
                | (Self::Active, Self::Closed)
        )
    }
}

/// Error returned when an invalid publisher lifecycle transition is requested.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PublisherLifecycleError {
    from: PublisherLifecycleState,
    to: PublisherLifecycleState,
}

impl PublisherLifecycleError {
    /// Creates a lifecycle transition error.
    pub const fn new(from: PublisherLifecycleState, to: PublisherLifecycleState) -> Self {
        Self { from, to }
    }

    /// Returns the state from which the transition was requested.
    pub const fn from(self) -> PublisherLifecycleState {
        self.from
    }

    /// Returns the requested destination state.
    pub const fn to(self) -> PublisherLifecycleState {
        self.to
    }
}

impl fmt::Display for PublisherLifecycleError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "invalid event publisher lifecycle transition from {:?} to {:?}",
            self.from, self.to
        )
    }
}

impl std::error::Error for PublisherLifecycleError {}

/// Error returned by publisher operations.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PublisherError {
    /// The publisher has not yet been activated.
    NotActive,

    /// The publisher has already been closed.
    Closed,

    /// The Event subsystem is no longer admitting publications.
    EventSubsystemUnavailable,

    /// The owning cancellation scope has already been cancelled.
    OwnerCancelled,

    /// The supplied subscription is already terminal.
    SubscriptionAlreadyTerminated,

    /// The subscription is already registered with this publisher.
    SubscriptionAlreadyRegistered,
}

impl fmt::Display for PublisherError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotActive => formatter.write_str("event publisher is not active"),
            Self::Closed => formatter.write_str("event publisher is closed"),
            Self::EventSubsystemUnavailable => {
                formatter.write_str("event subsystem is not accepting publications")
            }
            Self::OwnerCancelled => formatter.write_str("event publisher owner has been cancelled"),
            Self::SubscriptionAlreadyTerminated => {
                formatter.write_str("event subscription is already terminated")
            }
            Self::SubscriptionAlreadyRegistered => {
                formatter.write_str("event subscription is already registered")
            }
        }
    }
}

impl std::error::Error for PublisherError {}

/// The publication result handed from Publisher to Delivery.
///
/// The publisher owns selection. Delivery owns execution.
///
/// This representation remains inside the crate-private Event subsystem so
/// unrelated transport and SDK layers cannot depend on the handoff details.
#[derive(Debug)]
pub struct Publication {
    event: Arc<Event>,
    subscriptions: Vec<Arc<EventSubscription>>,
}

impl Publication {
    /// Creates a publication handoff.
    fn new(event: Arc<Event>, subscriptions: Vec<Arc<EventSubscription>>) -> Self {
        Self {
            event,
            subscriptions,
        }
    }

    /// Returns the immutable event occurrence.
    pub fn event(&self) -> &Arc<Event> {
        &self.event
    }

    /// Returns the matching subscriptions selected at publication time.
    pub fn subscriptions(&self) -> &[Arc<EventSubscription>] {
        &self.subscriptions
    }

    /// Returns the number of matching subscriptions.
    pub fn subscription_count(&self) -> usize {
        self.subscriptions.len()
    }

    /// Returns whether no subscription matched this publication.
    pub fn is_unobserved(&self) -> bool {
        self.subscriptions.is_empty()
    }
}

/// One explicitly owned Event publisher.
///
/// A publisher owns the registration relationship with its subscriptions.
/// Delivery workers are not stored here and are created by the Delivery layer.
pub struct EventPublisher {
    lifecycle: AtomicU8,
    event_lifecycle: Arc<EventLifecycle>,
    owner_cancellation: CancellationToken,
    subscriptions: RwLock<Vec<Arc<EventSubscription>>>,
}

impl fmt::Debug for EventPublisher {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("EventPublisher")
            .field("lifecycle", &self.state())
            .field("owner_cancelled", &self.owner_cancellation.is_cancelled())
            .field("subscription_count", &self.subscription_count())
            .finish_non_exhaustive()
    }
}

impl EventPublisher {
    /// Creates a publisher in the `Created` state.
    ///
    /// The publisher observes the Event subsystem lifecycle and derives its
    /// owned lifetime from the supplied cancellation scope.
    pub fn new(
        event_lifecycle: Arc<EventLifecycle>,
        owner_cancellation: &CancellationToken,
    ) -> Self {
        Self {
            lifecycle: AtomicU8::new(CREATED),
            event_lifecycle,
            owner_cancellation: owner_cancellation.clone(),
            subscriptions: RwLock::new(Vec::new()),
        }
    }

    /// Returns the current publisher lifecycle state.
    pub fn state(&self) -> PublisherLifecycleState {
        match self.lifecycle.load(Ordering::Acquire) {
            CREATED => PublisherLifecycleState::Created,
            ACTIVE => PublisherLifecycleState::Active,
            CLOSED => PublisherLifecycleState::Closed,
            _ => unreachable!("event publisher contains an invalid lifecycle state"),
        }
    }

    /// Activates a newly created publisher.
    pub fn activate(&self) -> Result<(), PublisherLifecycleError> {
        if self.owner_cancellation.is_cancelled() {
            return Err(PublisherLifecycleError::new(
                PublisherLifecycleState::Created,
                PublisherLifecycleState::Closed,
            ));
        }

        self.transition_to(PublisherLifecycleState::Active)
    }

    /// Returns whether the publisher is currently active.
    pub fn is_active(&self) -> bool {
        self.state().is_active()
    }

    /// Returns whether the publisher is permanently closed.
    pub fn is_closed(&self) -> bool {
        self.state().is_terminal()
    }

    /// Returns the number of currently registered subscriptions.
    pub fn subscription_count(&self) -> usize {
        self.subscriptions
            .read()
            .expect("event publisher subscription lock poisoned")
            .len()
    }

    /// Registers one explicit subscription with this publisher.
    ///
    /// Registration activates a newly created subscription. Terminal
    /// subscriptions are rejected.
    pub fn subscribe(
        &self,
        subscription: EventSubscription,
    ) -> Result<Arc<EventSubscription>, PublisherError> {
        self.ensure_registration_allowed()?;

        if subscription.is_terminal() || subscription.state() != SubscriptionLifecycleState::Created
        {
            return Err(PublisherError::SubscriptionAlreadyTerminated);
        }

        let subscription = Arc::new(subscription);

        let mut subscriptions = self
            .subscriptions
            .write()
            .expect("event publisher subscription lock poisoned");

        match self.state() {
            PublisherLifecycleState::Closed => return Err(PublisherError::Closed),
            PublisherLifecycleState::Created => return Err(PublisherError::NotActive),
            PublisherLifecycleState::Active => {}
        }

        subscriptions.push(Arc::clone(&subscription));

        drop(subscriptions);

        if let Err(error) = subscription.activate() {
            let mut subscriptions = self
                .subscriptions
                .write()
                .expect("event publisher subscription lock poisoned");

            subscriptions.retain(|registered| !Arc::ptr_eq(registered, &subscription));

            return Err(match error.from() {
                SubscriptionLifecycleState::Cancelled | SubscriptionLifecycleState::Closed => {
                    PublisherError::SubscriptionAlreadyTerminated
                }
                _ => PublisherError::SubscriptionAlreadyTerminated,
            });
        }

        Ok(subscription)
    }

    /// Removes a previously registered subscription.
    ///
    /// Removal prevents future publications from selecting the subscription.
    /// Delivery cleanup remains the responsibility of the Delivery layer.
    pub fn unsubscribe(&self, subscription: &Arc<EventSubscription>) -> bool {
        let removed = {
            let mut subscriptions = self
                .subscriptions
                .write()
                .expect("event publisher subscription lock poisoned");

            let original_len = subscriptions.len();

            subscriptions.retain(|registered| !Arc::ptr_eq(registered, subscription));

            subscriptions.len() != original_len
        };

        if removed {
            let _ = subscription.close();
        }

        removed
    }

    /// Publishes one immutable Event occurrence and returns the matching
    /// subscription handoff for Delivery.
    ///
    /// This method never invokes a subscriber handler. It snapshots matching
    /// subscriptions and releases the registry lock before returning.
    pub fn publish(&self, event: Event) -> Result<Publication, PublisherError> {
        self.ensure_publication_allowed()?;

        let event = Arc::new(event);

        let subscriptions = {
            let subscriptions = self
                .subscriptions
                .read()
                .expect("event publisher subscription lock poisoned");

            match self.state() {
                PublisherLifecycleState::Closed => return Err(PublisherError::Closed),
                PublisherLifecycleState::Created => return Err(PublisherError::NotActive),
                PublisherLifecycleState::Active => {}
            }

            subscriptions
                .iter()
                .filter(|subscription| {
                    subscription.matches(event.event_name(), event.event_type(), event.scope())
                })
                .cloned()
                .collect()
        };

        Ok(Publication::new(event, subscriptions))
    }

    /// Closes this publisher permanently.
    ///
    /// Closing stops new publications and terminates publisher-owned
    /// subscriptions. Delivery resources are cleaned up by Delivery.
    pub fn close(&self) {
        let subscriptions = {
            let mut registered = self
                .subscriptions
                .write()
                .expect("event publisher subscription lock poisoned");

            if self.lifecycle.swap(CLOSED, Ordering::AcqRel) == CLOSED {
                return;
            }

            std::mem::take(&mut *registered)
        };

        for subscription in subscriptions {
            let _ = subscription.cancel();
        }
    }

    fn ensure_registration_allowed(&self) -> Result<(), PublisherError> {
        self.refresh_owner_state();

        match self.state() {
            PublisherLifecycleState::Created => Err(PublisherError::NotActive),
            PublisherLifecycleState::Closed => Err(PublisherError::Closed),
            PublisherLifecycleState::Active => {
                if !self.event_lifecycle.allows_publication() {
                    Err(PublisherError::EventSubsystemUnavailable)
                } else {
                    Ok(())
                }
            }
        }
    }

    fn ensure_publication_allowed(&self) -> Result<(), PublisherError> {
        self.refresh_owner_state();

        match self.state() {
            PublisherLifecycleState::Created => Err(PublisherError::NotActive),
            PublisherLifecycleState::Closed => Err(PublisherError::Closed),
            PublisherLifecycleState::Active => {
                if !self.event_lifecycle.allows_publication() {
                    return Err(PublisherError::EventSubsystemUnavailable);
                }

                Ok(())
            }
        }
    }

    fn refresh_owner_state(&self) {
        if self.owner_cancellation.is_cancelled() {
            self.close();
        }
    }

    fn transition_to(&self, next: PublisherLifecycleState) -> Result<(), PublisherLifecycleError> {
        loop {
            let current = self.state();

            if current == next {
                return Ok(());
            }

            if !current.can_transition_to(next) {
                return Err(PublisherLifecycleError::new(current, next));
            }

            match self.lifecycle.compare_exchange(
                encode_state(current),
                encode_state(next),
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => return Ok(()),
                Err(_) => continue,
            }
        }
    }
}

fn encode_state(state: PublisherLifecycleState) -> u8 {
    match state {
        PublisherLifecycleState::Created => CREATED,
        PublisherLifecycleState::Active => ACTIVE,
        PublisherLifecycleState::Closed => CLOSED,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        events::{Event, EventName, Scope},
        identity::EventId,
        operation::CancellationToken,
    };

    fn event_lifecycle() -> Arc<EventLifecycle> {
        Arc::new(EventLifecycle::new())
    }

    fn publisher() -> EventPublisher {
        EventPublisher::new(event_lifecycle(), &CancellationToken::new())
    }

    fn event() -> Event {
        Event::new(
            EventId::new("event-1").unwrap(),
            EventName::new("test.event").unwrap(),
            "test.event",
            Scope::new("engine:test").unwrap(),
        )
        .unwrap()
    }

    #[test]
    fn publisher_starts_created() {
        let publisher = publisher();

        assert_eq!(publisher.state(), PublisherLifecycleState::Created);
        assert!(!publisher.is_active());
        assert!(!publisher.is_closed());
    }

    #[test]
    fn publisher_activates() {
        let publisher = publisher();

        publisher.activate().unwrap();

        assert_eq!(publisher.state(), PublisherLifecycleState::Active);
        assert!(publisher.is_active());
    }

    #[test]
    fn publisher_activation_is_idempotent() {
        let publisher = publisher();

        publisher.activate().unwrap();
        publisher.activate().unwrap();

        assert_eq!(publisher.state(), PublisherLifecycleState::Active);
    }

    #[test]
    fn created_publisher_cannot_publish() {
        let publisher = publisher();

        assert!(matches!(
            publisher.publish(event()),
            Err(PublisherError::NotActive)
        ));
    }

    #[test]
    fn active_publisher_can_publish() {
        let publisher = publisher();

        publisher.activate().unwrap();

        let publication = publisher.publish(event()).unwrap();

        assert_eq!(publication.event().event_id().as_str(), "event-1");
    }

    #[test]
    fn publisher_close_is_terminal() {
        let publisher = publisher();

        publisher.activate().unwrap();
        publisher.close();
        publisher.close();

        assert_eq!(publisher.state(), PublisherLifecycleState::Closed);
        assert!(publisher.is_closed());
        assert!(!publisher.is_active());
    }

    #[test]
    fn closed_publisher_rejects_publication() {
        let publisher = publisher();

        publisher.activate().unwrap();
        publisher.close();

        assert!(matches!(
            publisher.publish(event()),
            Err(PublisherError::Closed)
        ));
    }

    #[test]
    fn closed_publisher_rejects_subscription() {
        let publisher = publisher();
        let owner = CancellationToken::new();

        publisher.activate().unwrap();
        publisher.close();

        let subscription = EventSubscription::new(
            EventName::new("operation.completed").unwrap(),
            "operation.completed",
            Scope::new("engine:test").unwrap(),
            |_event: &Event| {},
            &owner,
        )
        .unwrap();

        assert!(matches!(
            publisher.subscribe(subscription),
            Err(PublisherError::Closed)
        ));
    }

    #[test]
    fn closed_publisher_recheck_prevents_late_subscription_admission() {
        let publisher = publisher();
        publisher.activate().unwrap();
        publisher.close();

        let owner = CancellationToken::new();
        let subscription = EventSubscription::new(
            EventName::new("operation.completed").unwrap(),
            "operation.completed",
            Scope::new("engine:test").unwrap(),
            |_event: &Event| {},
            &owner,
        )
        .unwrap();

        assert!(matches!(
            publisher.subscribe(subscription),
            Err(PublisherError::Closed)
        ));
        assert_eq!(publisher.subscription_count(), 0);
    }

    #[test]
    fn closed_publisher_recheck_prevents_late_publication_handoff() {
        let publisher = publisher();
        publisher.activate().unwrap();
        publisher.close();

        assert!(matches!(
            publisher.publish(event()),
            Err(PublisherError::Closed)
        ));
    }

    #[test]
    fn publisher_owner_cancellation_closes_publisher() {
        let owner = CancellationToken::new();

        let publisher = EventPublisher::new(event_lifecycle(), &owner);

        publisher.activate().unwrap();
        owner.cancel();

        assert!(matches!(
            publisher.publish(event()),
            Err(PublisherError::Closed)
        ));
        assert!(publisher.is_closed());
    }

    #[test]
    fn publisher_rejects_publication_when_event_subsystem_is_draining() {
        let lifecycle = event_lifecycle();
        let publisher = EventPublisher::new(Arc::clone(&lifecycle), &CancellationToken::new());

        publisher.activate().unwrap();
        lifecycle.begin_draining();

        assert!(matches!(
            publisher.publish(event()),
            Err(PublisherError::EventSubsystemUnavailable)
        ));
    }

    #[test]
    fn publisher_rejects_publication_when_event_subsystem_is_stopped() {
        let lifecycle = event_lifecycle();
        let publisher = EventPublisher::new(Arc::clone(&lifecycle), &CancellationToken::new());

        publisher.activate().unwrap();
        lifecycle.stop();

        assert!(matches!(
            publisher.publish(event()),
            Err(PublisherError::EventSubsystemUnavailable)
        ));
    }

    #[test]
    fn subscription_registration_activates_subscription() {
        let publisher = publisher();
        let owner = CancellationToken::new();

        publisher.activate().unwrap();

        let subscription = EventSubscription::new(
            EventName::new("operation.completed").unwrap(),
            "operation.completed",
            Scope::new("engine:test").unwrap(),
            |_event: &Event| {},
            &owner,
        )
        .unwrap();

        let registered = publisher.subscribe(subscription).unwrap();

        assert_eq!(registered.state(), SubscriptionLifecycleState::Active);
        assert_eq!(publisher.subscription_count(), 1);
    }

    #[test]
    fn unsubscribe_removes_subscription() {
        let publisher = publisher();
        let owner = CancellationToken::new();

        publisher.activate().unwrap();

        let subscription = EventSubscription::new(
            EventName::new("operation.completed").unwrap(),
            "operation.completed",
            Scope::new("engine:test").unwrap(),
            |_event: &Event| {},
            &owner,
        )
        .unwrap();

        let registered = publisher.subscribe(subscription).unwrap();

        assert!(publisher.unsubscribe(&registered));
        assert_eq!(publisher.subscription_count(), 0);
        assert_eq!(registered.state(), SubscriptionLifecycleState::Closed);
    }

    #[test]
    fn unsubscribe_unknown_subscription_returns_false() {
        let publisher = publisher();
        let owner = CancellationToken::new();

        publisher.activate().unwrap();

        let subscription = EventSubscription::new(
            EventName::new("operation.completed").unwrap(),
            "operation.completed",
            Scope::new("engine:test").unwrap(),
            |_event: &Event| {},
            &owner,
        )
        .unwrap();

        let subscription = Arc::new(subscription);

        assert!(!publisher.unsubscribe(&subscription));
        assert_eq!(publisher.subscription_count(), 0);
    }

    #[test]
    fn closing_publisher_terminates_registered_subscriptions() {
        let publisher = publisher();
        let owner = CancellationToken::new();

        publisher.activate().unwrap();

        let subscription = EventSubscription::new(
            EventName::new("operation.completed").unwrap(),
            "operation.completed",
            Scope::new("engine:test").unwrap(),
            |_event: &Event| {},
            &owner,
        )
        .unwrap();

        let registered = publisher.subscribe(subscription).unwrap();

        publisher.close();

        assert_eq!(publisher.subscription_count(), 0);
        assert!(registered.is_cancelled());
    }

    #[test]
    fn publication_with_no_matching_subscriptions_is_valid() {
        let publisher = publisher();

        publisher.activate().unwrap();

        let publication = publisher.publish(event()).unwrap();

        assert!(publication.is_unobserved());
        assert_eq!(publication.subscription_count(), 0);
    }

    #[test]
    fn publication_preserves_one_event_identity() {
        let publisher = publisher();

        publisher.activate().unwrap();

        let publication = publisher.publish(event()).unwrap();

        assert_eq!(publication.event().event_id().as_str(), "event-1");
    }

    #[test]
    fn publisher_lifecycle_terminal_transition_cannot_reopen() {
        let publisher = publisher();

        publisher.activate().unwrap();
        publisher.close();

        assert!(publisher.activate().is_err());
        assert_eq!(publisher.state(), PublisherLifecycleState::Closed);
    }

    #[test]
    fn concurrent_close_is_safe() {
        use std::thread;

        let publisher = Arc::new(publisher());
        publisher.activate().unwrap();

        let mut handles = Vec::new();

        for _ in 0..16 {
            let publisher = Arc::clone(&publisher);

            handles.push(thread::spawn(move || {
                publisher.close();
            }));
        }

        for handle in handles {
            handle.join().unwrap();
        }

        assert_eq!(publisher.state(), PublisherLifecycleState::Closed);
        assert_eq!(publisher.subscription_count(), 0);
    }
}