44using ReactiveDomain . Testing ;
55using Xunit ;
66
7- namespace ReactiveDomain . Foundation . Tests {
7+ namespace ReactiveDomain . Foundation . Tests
8+ {
89 // ReSharper disable once InconsistentNaming
910 public class when_using_read_model_base :
1011 ReadModelBase ,
1112 IHandle < when_using_read_model_base . ReadModelTestEvent > ,
12- IClassFixture < StreamStoreConnectionFixture > {
13+ IClassFixture < StreamStoreConnectionFixture >
14+ {
1315
1416 private static IStreamStoreConnection _conn ;
1517 private static readonly IEventSerializer Serializer =
@@ -22,7 +24,8 @@ public class when_using_read_model_base :
2224
2325
2426 public when_using_read_model_base ( StreamStoreConnectionFixture fixture )
25- : base ( nameof ( when_using_read_model_base ) , new ConfiguredConnection ( fixture . Connection , Namer , Serializer ) ) {
27+ : base ( nameof ( when_using_read_model_base ) , new ConfiguredConnection ( fixture . Connection , Namer , Serializer ) )
28+ {
2629 _conn = fixture . Connection ;
2730 _conn . Connect ( ) ;
2831
@@ -43,47 +46,53 @@ private void AppendEvents(
4346 int numEventsToBeSent ,
4447 IStreamStoreConnection conn ,
4548 string streamName ,
46- int value ) {
47- for ( int evtNumber = 0 ; evtNumber < numEventsToBeSent ; evtNumber ++ ) {
49+ int value )
50+ {
51+ for ( int evtNumber = 0 ; evtNumber < numEventsToBeSent ; evtNumber ++ )
52+ {
4853 var evt = new ReadModelTestEvent ( evtNumber , value ) ;
4954 conn . AppendToStream ( streamName , ExpectedVersion . Any , null , Serializer . Serialize ( evt ) ) ;
5055 }
5156 }
5257 [ Fact ]
53- public void can_start_streams_by_aggregate ( ) {
58+ public void can_start_streams_by_aggregate ( )
59+ {
5460 var aggId = Guid . NewGuid ( ) ;
5561 var s1 = Namer . GenerateForAggregate ( typeof ( TestAggregate ) , aggId ) ;
5662 AppendEvents ( 1 , _conn , s1 , 7 ) ;
5763 Start < TestAggregate > ( aggId ) ;
58- AssertEx . IsOrBecomesTrue ( ( ) => Count == 1 , 1000 , msg : $ "Expected 1 got { Count } ") ;
64+ AssertEx . IsModelVersion ( this , 2 , 1000 , msg : $ "Expected 2 got { Version } ") ; // 1 message + CatchupSubscriptionBecameLive
5965 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 7 ) ;
6066 }
6167 [ Fact ]
62- public void can_start_streams_by_aggregate_category ( ) {
63-
68+ public void can_start_streams_by_aggregate_category ( )
69+ {
70+
6471 var s1 = Namer . GenerateForAggregate ( typeof ( ReadModelTestCategoryAggregate ) , Guid . NewGuid ( ) ) ;
6572 AppendEvents ( 1 , _conn , s1 , 7 ) ;
6673 var s2 = Namer . GenerateForAggregate ( typeof ( ReadModelTestCategoryAggregate ) , Guid . NewGuid ( ) ) ;
6774 AppendEvents ( 1 , _conn , s2 , 5 ) ;
68- Start < ReadModelTestCategoryAggregate > ( null , true ) ;
75+ Start < ReadModelTestCategoryAggregate > ( null , true ) ;
6976
70- AssertEx . IsOrBecomesTrue ( ( ) => Count == 2 , 1000 , msg : $ "Expected 2 got { Count } ") ;
77+ AssertEx . IsModelVersion ( this , 3 , 1000 , msg : $ "Expected 3 got { Version } ") ;
7178 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 12 ) ;
7279 }
7380 [ Fact ]
74- public void can_read_one_stream ( ) {
81+ public void can_read_one_stream ( )
82+ {
7583 Start ( _stream1 ) ;
76- AssertEx . IsOrBecomesTrue ( ( ) => Count == 10 , 1000 , msg : $ "Expected 10 got { Count } ") ;
84+ AssertEx . IsModelVersion ( this , 11 , 1000 , msg : $ "Expected 11 got { Version } ") ;
7785 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 20 ) ;
7886 //confirm checkpoints
7987 Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
8088 Assert . Equal ( 9 , GetCheckpoint ( ) [ 0 ] . Item2 ) ;
8189 }
8290 [ Fact ]
83- public void can_read_two_streams ( ) {
91+ public void can_read_two_streams ( )
92+ {
8493 Start ( _stream1 ) ;
8594 Start ( _stream2 ) ;
86- AssertEx . IsOrBecomesTrue ( ( ) => Count == 20 , 1000 , msg : $ "Expected 20 got { Count } ") ;
95+ AssertEx . IsModelVersion ( this , 22 , 1000 , msg : $ "Expected 22 got { Version } ") ;
8796 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 50 ) ;
8897 //confirm checkpoints
8998 Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
@@ -92,45 +101,49 @@ public void can_read_two_streams() {
92101 Assert . Equal ( 9 , GetCheckpoint ( ) [ 1 ] . Item2 ) ;
93102 }
94103 [ Fact ]
95- public void can_wait_for_one_stream_to_go_live ( ) {
104+ public void can_wait_for_one_stream_to_go_live ( )
105+ {
96106 Start ( _stream1 , null , true ) ;
97- AssertEx . IsOrBecomesTrue ( ( ) => Count == 10 , 100 , msg : $ "Expected 10 got { Count } ") ;
107+ AssertEx . IsModelVersion ( this , 11 , 100 , msg : $ "Expected 11 got { Version } ") ;
98108 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 20 , 100 ) ;
99109 }
100110 [ Fact ]
101- public void can_wait_for_two_streams_to_go_live ( ) {
111+ public void can_wait_for_two_streams_to_go_live ( )
112+ {
102113 Start ( _stream1 , null , true ) ;
103- AssertEx . IsOrBecomesTrue ( ( ) => Count == 10 , 100 , msg : $ "Expected 10 got { Count } ") ;
114+ AssertEx . IsModelVersion ( this , 11 , 100 , msg : $ "Expected 11 got { Version } ") ;
104115 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 20 , 100 ) ;
105116
106117 Start ( _stream2 , null , true ) ;
107- AssertEx . IsOrBecomesTrue ( ( ) => Count == 20 , 100 , msg : $ "Expected 20 got { Count } ") ;
118+ AssertEx . IsModelVersion ( this , 21 , 100 , msg : $ "Expected 21 got { Version } ") ;
108119 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 50 , 100 ) ;
109120 }
110121 [ Fact ]
111- public void can_listen_to_one_stream ( ) {
122+ public void can_listen_to_one_stream ( )
123+ {
112124 Start ( _stream1 ) ;
113- AssertEx . IsOrBecomesTrue ( ( ) => Count == 10 , 1000 , msg : $ "Expected 10 got { Count } ") ;
125+ AssertEx . IsModelVersion ( this , 11 , 1000 , msg : $ "Expected 11 got { Version } ") ;
114126 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 20 ) ;
115127 //add more messages
116128 AppendEvents ( 10 , _conn , _stream1 , 5 ) ;
117- AssertEx . IsOrBecomesTrue ( ( ) => Count == 20 , 1000 , msg : $ "Expected 20 got { Count } ") ;
129+ AssertEx . IsModelVersion ( this , 21 , 1000 , msg : $ "Expected 21 got { Version } ") ;
118130 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 70 ) ;
119131 //confirm checkpoints
120132 Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
121133 Assert . Equal ( 19 , GetCheckpoint ( ) [ 0 ] . Item2 ) ; Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
122134 Assert . Equal ( 19 , GetCheckpoint ( ) [ 0 ] . Item2 ) ;
123135 }
124136 [ Fact ]
125- public void can_listen_to_two_streams ( ) {
137+ public void can_listen_to_two_streams ( )
138+ {
126139 Start ( _stream1 ) ;
127140 Start ( _stream2 ) ;
128- AssertEx . IsOrBecomesTrue ( ( ) => Count == 20 , 1000 , msg : $ "Expected 20 got { Count } ") ;
141+ AssertEx . IsModelVersion ( this , 22 , 1000 , msg : $ "Expected 22 got { Version } ") ;
129142 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 50 ) ;
130143 //add more messages
131144 AppendEvents ( 10 , _conn , _stream1 , 5 ) ;
132145 AppendEvents ( 10 , _conn , _stream2 , 7 ) ;
133- AssertEx . IsOrBecomesTrue ( ( ) => Count == 40 , 1000 , msg : $ "Expected 20 got { Count } ") ;
146+ AssertEx . IsModelVersion ( this , 42 , 1000 , msg : $ "Expected 42 got { Version } ") ;
134147 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 170 ) ;
135148 //confirm checkpoints
136149 Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
@@ -139,40 +152,40 @@ public void can_listen_to_two_streams() {
139152 Assert . Equal ( 19 , GetCheckpoint ( ) [ 1 ] . Item2 ) ;
140153 }
141154 [ Fact ]
142- public void can_use_checkpoint_on_one_stream ( ) {
155+ public void can_use_checkpoint_on_one_stream ( )
156+ {
143157 //restore state
144158 var checkPoint = 8L ; //Zero based, ignore the first 9 events
145- Count = 9 ;
146159 Sum = 18 ;
147160 //start at the checkpoint
148161 Start ( _stream1 , checkPoint ) ;
149162 //add the one recorded event
150- AssertEx . IsOrBecomesTrue ( ( ) => Count == 10 , 100 , msg : $ "Expected 10 got { Count } ") ;
163+ AssertEx . IsModelVersion ( this , 2 , 100 , msg : $ "Expected 2 got { Version } ") ;
151164 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 20 ) ;
152165 //add more messages
153166 AppendEvents ( 10 , _conn , _stream1 , 5 ) ;
154- AssertEx . IsOrBecomesTrue ( ( ) => Count == 20 , 100 , msg : $ "Expected 20 got { Count } ") ;
167+ AssertEx . IsModelVersion ( this , 12 , 100 , msg : $ "Expected 12 got { Version } ") ;
155168 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 70 ) ;
156169 //confirm checkpoints
157170 Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
158171 Assert . Equal ( 19 , GetCheckpoint ( ) [ 0 ] . Item2 ) ;
159172 }
160173 [ Fact ]
161- public void can_use_checkpoint_on_two_streams ( ) {
174+ public void can_use_checkpoint_on_two_streams ( )
175+ {
162176 //restore state
163177 var checkPoint1 = 8L ; //Zero based, ignore the first 9 events
164178 var checkPoint2 = 5L ; //Zero based, ignore the first 6 events
165- Count = ( 9 ) + ( 6 ) ;
166179 Sum = ( 9 * 2 ) + ( 6 * 3 ) ;
167180 Start ( _stream1 , checkPoint1 ) ;
168181 Start ( _stream2 , checkPoint2 ) ;
169182 //add the recorded events 2 on stream 1 & 5 on stream 2
170- AssertEx . IsOrBecomesTrue ( ( ) => Count == 20 , 1000 , msg : $ "Expected 20 got { Count } ") ;
183+ AssertEx . IsModelVersion ( this , 7 , 1000 , msg : $ "Expected 7 got { Version } ") ;
171184 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 50 , msg : $ "Expected 50 got { Sum } ") ;
172185 //add more messages
173186 AppendEvents ( 10 , _conn , _stream1 , 5 ) ;
174187 AppendEvents ( 10 , _conn , _stream2 , 7 ) ;
175- AssertEx . IsOrBecomesTrue ( ( ) => Count == 40 , 1000 , msg : $ "Expected 20 got { Count } ") ;
188+ AssertEx . IsModelVersion ( this , 27 , 1000 , msg : $ "Expected 27 got { Version } ") ;
176189 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 170 ) ;
177190 //confirm checkpoints
178191 Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
@@ -181,38 +194,40 @@ public void can_use_checkpoint_on_two_streams() {
181194 Assert . Equal ( 19 , GetCheckpoint ( ) [ 1 ] . Item2 ) ;
182195 }
183196 [ Fact ]
184- public void can_listen_to_the_same_stream_twice ( ) {
185- Assert . Equal ( 0 , Count ) ;
197+ public void can_listen_to_the_same_stream_twice ( )
198+ {
199+ Assert . Equal ( 0 , Version ) ;
186200 //weird but true
187201 //n.b. Don't do this on purpose
188202 Start ( _stream1 ) ;
189203 Start ( _stream1 ) ;
190204 //double events
191- AssertEx . IsOrBecomesTrue ( ( ) => Count == 20 , 1000 , msg : $ "Expected 20 got { Count } ") ;
205+ AssertEx . IsModelVersion ( this , 22 , 1000 , msg : $ "Expected 22 got { Version } ") ;
192206 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 40 ) ;
193207 //even more doubled events
194208 AppendEvents ( 10 , _conn , _stream1 , 5 ) ;
195- AssertEx . IsOrBecomesTrue ( ( ) => Count == 40 , 2000 , msg : $ "Expected 40 got { Count } ") ;
209+ AssertEx . IsModelVersion ( this , 42 , 2000 , msg : $ "Expected 42 got { Version } ") ;
196210 AssertEx . IsOrBecomesTrue ( ( ) => Sum == 140 ) ;
197211 }
198212
199213 public long Sum { get ; private set ; }
200- public long Count { get ; private set ; }
201- void IHandle < ReadModelTestEvent > . Handle ( ReadModelTestEvent @event ) {
214+ void IHandle < ReadModelTestEvent > . Handle ( ReadModelTestEvent @event )
215+ {
202216 Sum += @event . Value ;
203- Count ++ ;
204217 }
205- public class ReadModelTestEvent : Event {
218+ public class ReadModelTestEvent : Event
219+ {
206220 public readonly int Number ;
207221 public readonly int Value ;
208222
209223 public ReadModelTestEvent (
210224 int number ,
211- int value ) {
225+ int value )
226+ {
212227 Number = number ;
213228 Value = value ;
214229 }
215230 }
216- public class ReadModelTestCategoryAggregate : EventDrivenStateMachine { }
231+ public class ReadModelTestCategoryAggregate : EventDrivenStateMachine { }
217232 }
218233}
0 commit comments