1+ namespace Shared . EventStore . Tests
2+ {
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . Threading ;
6+ using System . Threading . Tasks ;
7+ using EventHandling ;
8+ using General ;
9+ using Logger ;
10+ using Shouldly ;
11+ using SubscriptionWorker ;
12+ using Xunit ;
13+
14+ public class PersistentSubscriptionTests
15+ {
16+ public PersistentSubscriptionTests ( )
17+ {
18+ Shared . Logger . Logger . Initialise ( NullLogger . Instance ) ;
19+
20+ TypeMap . AddType < EstateCreatedEvent > ( "EstateCreatedEvent" ) ;
21+ }
22+
23+ [ Fact ]
24+ public async Task PersistentSubscription_CanBeCreatedAndReceiveEventsSingleEventHandler ( )
25+ {
26+ PersistentSubscriptionDetails persistentSubscriptionDetails = new ( "$ce-test" , "local-1" ) ;
27+ List < IDomainEventHandler > eventHandlers = new ( ) ;
28+ TestDomainEventHandler eventHandler = new ( ) ;
29+ InMemoryPersistentSubscriptionsClient persistentSubscriptionsClient = new ( ) ;
30+ CancellationToken cancellationToken = CancellationToken . None ;
31+
32+ eventHandlers . Add ( eventHandler ) ;
33+
34+ var persistentSubscription = PersistentSubscription . Create ( persistentSubscriptionsClient , persistentSubscriptionDetails , eventHandlers ) ;
35+
36+ await persistentSubscription . ConnectToSubscription ( cancellationToken ) ;
37+
38+ persistentSubscription . Connected . ShouldBeTrue ( ) ;
39+
40+ String @event = "{\r \n \" estateId\" : \" 4fc2692f-067a-443e-8006-335bf2732248\" ,\r \n \" estateName\" : \" Demo Estate\" \r \n }\t " ;
41+
42+ //Manually add events.
43+ persistentSubscriptionsClient . WriteEvent ( @event , "EstateCreatedEvent" , cancellationToken ) ;
44+
45+ //Crude - but a decent start point
46+ eventHandler . DomainEvents . Count . ShouldBe ( 1 ) ;
47+ }
48+
49+ [ Fact ]
50+ public async Task PersistentSubscription_CanBeCreatedAndFilterOutSystemEvent ( )
51+ {
52+ PersistentSubscriptionDetails persistentSubscriptionDetails = new ( "$ce-test" , "local-1" ) ;
53+ List < IDomainEventHandler > eventHandlers = new ( ) ;
54+ TestDomainEventHandler eventHandler = new ( ) ;
55+ InMemoryPersistentSubscriptionsClient persistentSubscriptionsClient = new ( ) ;
56+ CancellationToken cancellationToken = CancellationToken . None ;
57+
58+ eventHandlers . Add ( eventHandler ) ;
59+
60+ var persistentSubscription =
61+ PersistentSubscription . Create ( persistentSubscriptionsClient , persistentSubscriptionDetails , eventHandlers ) ;
62+
63+ await persistentSubscription . ConnectToSubscription ( cancellationToken ) ;
64+
65+ persistentSubscription . Connected . ShouldBeTrue ( ) ;
66+
67+ String @event = "" ;
68+
69+ //Manually add events.
70+ persistentSubscriptionsClient . WriteEvent ( @event , "$" , cancellationToken ) ;
71+
72+ //Crude - but a decent start point
73+ eventHandler . DomainEvents . Count . ShouldBe ( 0 ) ;
74+ }
75+
76+ [ Fact ]
77+ public async Task PersistentSubscription_CanBeCreatedAndReceiveEventsMultipleEventHandler ( )
78+ {
79+ PersistentSubscriptionDetails persistentSubscriptionDetails = new ( "$ce-test" , "local-1" ) ;
80+ List < IDomainEventHandler > eventHandlers = new ( ) ;
81+ TestDomainEventHandler eventHandler1 = new ( ) ;
82+ TestDomainEventHandler eventHandler2 = new ( ) ;
83+ InMemoryPersistentSubscriptionsClient persistentSubscriptionsClient = new ( ) ;
84+ CancellationToken cancellationToken = CancellationToken . None ;
85+
86+ eventHandlers . AddRange ( new [ ] { eventHandler1 , eventHandler2 } ) ;
87+
88+ var persistentSubscription =
89+ PersistentSubscription . Create ( persistentSubscriptionsClient , persistentSubscriptionDetails , eventHandlers ) ;
90+
91+ await persistentSubscription . ConnectToSubscription ( cancellationToken ) ;
92+
93+ persistentSubscription . Connected . ShouldBeTrue ( ) ;
94+
95+ String @event = "{\r \n \" estateId\" : \" 4fc2692f-067a-443e-8006-335bf2732248\" ,\r \n \" estateName\" : \" Demo Estate\" \r \n }\t " ;
96+
97+ //Manually add events.
98+ persistentSubscriptionsClient . WriteEvent ( @event , "EstateCreatedEvent" , cancellationToken ) ;
99+
100+ //Crude - but a decent start point
101+ eventHandler1 . DomainEvents . Count . ShouldBe ( 1 ) ;
102+ eventHandler2 . DomainEvents . Count . ShouldBe ( 1 ) ;
103+ }
104+ }
105+ }
0 commit comments