4
4
using ReactiveDomain . Testing ;
5
5
using Xunit ;
6
6
7
- namespace ReactiveDomain . Foundation . Tests {
7
+ namespace ReactiveDomain . Foundation . Tests
8
+ {
8
9
// ReSharper disable once InconsistentNaming
9
10
public class when_using_read_model_base :
10
11
ReadModelBase ,
11
12
IHandle < when_using_read_model_base . ReadModelTestEvent > ,
12
- IClassFixture < StreamStoreConnectionFixture > {
13
+ IClassFixture < StreamStoreConnectionFixture >
14
+ {
13
15
14
16
private static IStreamStoreConnection _conn ;
15
17
private static readonly IEventSerializer Serializer =
@@ -22,7 +24,8 @@ public class when_using_read_model_base :
22
24
23
25
24
26
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
+ {
26
29
_conn = fixture . Connection ;
27
30
_conn . Connect ( ) ;
28
31
@@ -43,47 +46,53 @@ private void AppendEvents(
43
46
int numEventsToBeSent ,
44
47
IStreamStoreConnection conn ,
45
48
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
+ {
48
53
var evt = new ReadModelTestEvent ( evtNumber , value ) ;
49
54
conn . AppendToStream ( streamName , ExpectedVersion . Any , null , Serializer . Serialize ( evt ) ) ;
50
55
}
51
56
}
52
57
[ Fact ]
53
- public void can_start_streams_by_aggregate ( ) {
58
+ public void can_start_streams_by_aggregate ( )
59
+ {
54
60
var aggId = Guid . NewGuid ( ) ;
55
61
var s1 = Namer . GenerateForAggregate ( typeof ( TestAggregate ) , aggId ) ;
56
62
AppendEvents ( 1 , _conn , s1 , 7 ) ;
57
63
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
59
65
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 7 ) ;
60
66
}
61
67
[ Fact ]
62
- public void can_start_streams_by_aggregate_category ( ) {
63
-
68
+ public void can_start_streams_by_aggregate_category ( )
69
+ {
70
+
64
71
var s1 = Namer . GenerateForAggregate ( typeof ( ReadModelTestCategoryAggregate ) , Guid . NewGuid ( ) ) ;
65
72
AppendEvents ( 1 , _conn , s1 , 7 ) ;
66
73
var s2 = Namer . GenerateForAggregate ( typeof ( ReadModelTestCategoryAggregate ) , Guid . NewGuid ( ) ) ;
67
74
AppendEvents ( 1 , _conn , s2 , 5 ) ;
68
- Start < ReadModelTestCategoryAggregate > ( null , true ) ;
75
+ Start < ReadModelTestCategoryAggregate > ( null , true ) ;
69
76
70
- AssertEx . IsOrBecomesTrue ( ( ) => Count == 2 , 1000 , msg : $ "Expected 2 got { Count } ") ;
77
+ AssertEx . IsModelVersion ( this , 3 , 1000 , msg : $ "Expected 3 got { Version } ") ;
71
78
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 12 ) ;
72
79
}
73
80
[ Fact ]
74
- public void can_read_one_stream ( ) {
81
+ public void can_read_one_stream ( )
82
+ {
75
83
Start ( _stream1 ) ;
76
- AssertEx . IsOrBecomesTrue ( ( ) => Count == 10 , 1000 , msg : $ "Expected 10 got { Count } ") ;
84
+ AssertEx . IsModelVersion ( this , 11 , 1000 , msg : $ "Expected 11 got { Version } ") ;
77
85
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 20 ) ;
78
86
//confirm checkpoints
79
87
Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
80
88
Assert . Equal ( 9 , GetCheckpoint ( ) [ 0 ] . Item2 ) ;
81
89
}
82
90
[ Fact ]
83
- public void can_read_two_streams ( ) {
91
+ public void can_read_two_streams ( )
92
+ {
84
93
Start ( _stream1 ) ;
85
94
Start ( _stream2 ) ;
86
- AssertEx . IsOrBecomesTrue ( ( ) => Count == 20 , 1000 , msg : $ "Expected 20 got { Count } ") ;
95
+ AssertEx . IsModelVersion ( this , 22 , 1000 , msg : $ "Expected 22 got { Version } ") ;
87
96
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 50 ) ;
88
97
//confirm checkpoints
89
98
Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
@@ -92,45 +101,49 @@ public void can_read_two_streams() {
92
101
Assert . Equal ( 9 , GetCheckpoint ( ) [ 1 ] . Item2 ) ;
93
102
}
94
103
[ Fact ]
95
- public void can_wait_for_one_stream_to_go_live ( ) {
104
+ public void can_wait_for_one_stream_to_go_live ( )
105
+ {
96
106
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 } ") ;
98
108
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 20 , 100 ) ;
99
109
}
100
110
[ Fact ]
101
- public void can_wait_for_two_streams_to_go_live ( ) {
111
+ public void can_wait_for_two_streams_to_go_live ( )
112
+ {
102
113
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 } ") ;
104
115
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 20 , 100 ) ;
105
116
106
117
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 } ") ;
108
119
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 50 , 100 ) ;
109
120
}
110
121
[ Fact ]
111
- public void can_listen_to_one_stream ( ) {
122
+ public void can_listen_to_one_stream ( )
123
+ {
112
124
Start ( _stream1 ) ;
113
- AssertEx . IsOrBecomesTrue ( ( ) => Count == 10 , 1000 , msg : $ "Expected 10 got { Count } ") ;
125
+ AssertEx . IsModelVersion ( this , 11 , 1000 , msg : $ "Expected 11 got { Version } ") ;
114
126
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 20 ) ;
115
127
//add more messages
116
128
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 } ") ;
118
130
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 70 ) ;
119
131
//confirm checkpoints
120
132
Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
121
133
Assert . Equal ( 19 , GetCheckpoint ( ) [ 0 ] . Item2 ) ; Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
122
134
Assert . Equal ( 19 , GetCheckpoint ( ) [ 0 ] . Item2 ) ;
123
135
}
124
136
[ Fact ]
125
- public void can_listen_to_two_streams ( ) {
137
+ public void can_listen_to_two_streams ( )
138
+ {
126
139
Start ( _stream1 ) ;
127
140
Start ( _stream2 ) ;
128
- AssertEx . IsOrBecomesTrue ( ( ) => Count == 20 , 1000 , msg : $ "Expected 20 got { Count } ") ;
141
+ AssertEx . IsModelVersion ( this , 22 , 1000 , msg : $ "Expected 22 got { Version } ") ;
129
142
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 50 ) ;
130
143
//add more messages
131
144
AppendEvents ( 10 , _conn , _stream1 , 5 ) ;
132
145
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 } ") ;
134
147
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 170 ) ;
135
148
//confirm checkpoints
136
149
Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
@@ -139,40 +152,40 @@ public void can_listen_to_two_streams() {
139
152
Assert . Equal ( 19 , GetCheckpoint ( ) [ 1 ] . Item2 ) ;
140
153
}
141
154
[ Fact ]
142
- public void can_use_checkpoint_on_one_stream ( ) {
155
+ public void can_use_checkpoint_on_one_stream ( )
156
+ {
143
157
//restore state
144
158
var checkPoint = 8L ; //Zero based, ignore the first 9 events
145
- Count = 9 ;
146
159
Sum = 18 ;
147
160
//start at the checkpoint
148
161
Start ( _stream1 , checkPoint ) ;
149
162
//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 } ") ;
151
164
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 20 ) ;
152
165
//add more messages
153
166
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 } ") ;
155
168
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 70 ) ;
156
169
//confirm checkpoints
157
170
Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
158
171
Assert . Equal ( 19 , GetCheckpoint ( ) [ 0 ] . Item2 ) ;
159
172
}
160
173
[ Fact ]
161
- public void can_use_checkpoint_on_two_streams ( ) {
174
+ public void can_use_checkpoint_on_two_streams ( )
175
+ {
162
176
//restore state
163
177
var checkPoint1 = 8L ; //Zero based, ignore the first 9 events
164
178
var checkPoint2 = 5L ; //Zero based, ignore the first 6 events
165
- Count = ( 9 ) + ( 6 ) ;
166
179
Sum = ( 9 * 2 ) + ( 6 * 3 ) ;
167
180
Start ( _stream1 , checkPoint1 ) ;
168
181
Start ( _stream2 , checkPoint2 ) ;
169
182
//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 } ") ;
171
184
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 50 , msg : $ "Expected 50 got { Sum } ") ;
172
185
//add more messages
173
186
AppendEvents ( 10 , _conn , _stream1 , 5 ) ;
174
187
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 } ") ;
176
189
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 170 ) ;
177
190
//confirm checkpoints
178
191
Assert . Equal ( _stream1 , GetCheckpoint ( ) [ 0 ] . Item1 ) ;
@@ -181,38 +194,40 @@ public void can_use_checkpoint_on_two_streams() {
181
194
Assert . Equal ( 19 , GetCheckpoint ( ) [ 1 ] . Item2 ) ;
182
195
}
183
196
[ 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 ) ;
186
200
//weird but true
187
201
//n.b. Don't do this on purpose
188
202
Start ( _stream1 ) ;
189
203
Start ( _stream1 ) ;
190
204
//double events
191
- AssertEx . IsOrBecomesTrue ( ( ) => Count == 20 , 1000 , msg : $ "Expected 20 got { Count } ") ;
205
+ AssertEx . IsModelVersion ( this , 22 , 1000 , msg : $ "Expected 22 got { Version } ") ;
192
206
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 40 ) ;
193
207
//even more doubled events
194
208
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 } ") ;
196
210
AssertEx . IsOrBecomesTrue ( ( ) => Sum == 140 ) ;
197
211
}
198
212
199
213
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
+ {
202
216
Sum += @event . Value ;
203
- Count ++ ;
204
217
}
205
- public class ReadModelTestEvent : Event {
218
+ public class ReadModelTestEvent : Event
219
+ {
206
220
public readonly int Number ;
207
221
public readonly int Value ;
208
222
209
223
public ReadModelTestEvent (
210
224
int number ,
211
- int value ) {
225
+ int value )
226
+ {
212
227
Number = number ;
213
228
Value = value ;
214
229
}
215
230
}
216
- public class ReadModelTestCategoryAggregate : EventDrivenStateMachine { }
231
+ public class ReadModelTestCategoryAggregate : EventDrivenStateMachine { }
217
232
}
218
233
}
0 commit comments