1
+ using FluentAssertions ;
2
+ using Microsoft . Diagnostics . Tracing . Analysis . GC ;
3
+ using Microsoft . Diagnostics . Tracing . Parsers . GCDynamic ;
4
+ using GC . Analysis . API . DynamicEvents ;
5
+ using System . Reflection ;
6
+
7
+ namespace GC . Analysis . API . UnitTests
8
+ {
9
+ [ TestClass ]
10
+ public class DynamicEventTests
11
+ {
12
+ [ TestMethod ]
13
+ public void TestDuplicatedSchema ( )
14
+ {
15
+ Action test = ( ) =>
16
+ {
17
+ DynamicEventSchema . Set (
18
+ new List < DynamicEventSchema >
19
+ {
20
+ new DynamicEventSchema
21
+ {
22
+ DynamicEventName = "SampleEventName" ,
23
+ Fields = new List < KeyValuePair < string , Type > >
24
+ {
25
+ new KeyValuePair < string , Type > ( "version" , typeof ( ushort ) ) ,
26
+ new KeyValuePair < string , Type > ( "Number" , typeof ( ulong ) ) ,
27
+ }
28
+ } ,
29
+ new DynamicEventSchema
30
+ {
31
+ DynamicEventName = "SampleEventName" ,
32
+ Fields = new List < KeyValuePair < string , Type > >
33
+ {
34
+ new KeyValuePair < string , Type > ( "version" , typeof ( ushort ) ) ,
35
+ new KeyValuePair < string , Type > ( "Number" , typeof ( ulong ) ) ,
36
+ }
37
+ }
38
+ }
39
+ ) ;
40
+ } ;
41
+ test . Should ( ) . Throw < Exception > ( ) ;
42
+ }
43
+
44
+ [ TestMethod ]
45
+ public void TestDuplicatedFields ( )
46
+ {
47
+ Action test = ( ) =>
48
+ {
49
+ DynamicEventSchema . Set (
50
+ new List < DynamicEventSchema >
51
+ {
52
+ new DynamicEventSchema
53
+ {
54
+ DynamicEventName = "SampleEventName" ,
55
+ Fields = new List < KeyValuePair < string , Type > >
56
+ {
57
+ new KeyValuePair < string , Type > ( "version" , typeof ( ushort ) ) ,
58
+ new KeyValuePair < string , Type > ( "version" , typeof ( ulong ) ) ,
59
+ }
60
+ } ,
61
+ }
62
+ ) ;
63
+ } ;
64
+ test . Should ( ) . Throw < Exception > ( ) ;
65
+ }
66
+
67
+ [ TestMethod ]
68
+ public void TestUnsupportedType ( )
69
+ {
70
+ Action test = ( ) =>
71
+ {
72
+ DynamicEventSchema . Set (
73
+ new List < DynamicEventSchema >
74
+ {
75
+ new DynamicEventSchema
76
+ {
77
+ DynamicEventName = "SampleEventName" ,
78
+ Fields = new List < KeyValuePair < string , Type > >
79
+ {
80
+ new KeyValuePair < string , Type > ( "version" , typeof ( DateTime ) ) ,
81
+ }
82
+ } ,
83
+ }
84
+ ) ;
85
+ } ;
86
+ test . Should ( ) . Throw < Exception > ( ) ;
87
+ }
88
+
89
+ [ TestMethod ]
90
+ public void TestNegativeMinOccurrence ( )
91
+ {
92
+ Action test = ( ) =>
93
+ {
94
+ DynamicEventSchema . Set (
95
+ new List < DynamicEventSchema >
96
+ {
97
+ new DynamicEventSchema
98
+ {
99
+ DynamicEventName = "SampleEventName" ,
100
+ MinOccurrence = - 1 ,
101
+ Fields = new List < KeyValuePair < string , Type > >
102
+ {
103
+ new KeyValuePair < string , Type > ( "version" , typeof ( ushort ) ) ,
104
+ }
105
+ } ,
106
+ }
107
+ ) ;
108
+ } ;
109
+ test . Should ( ) . Throw < Exception > ( ) ;
110
+ }
111
+
112
+ [ TestMethod ]
113
+ public void TestSmallerMaxOccurrence ( )
114
+ {
115
+ Action test = ( ) =>
116
+ {
117
+ DynamicEventSchema . Set (
118
+ new List < DynamicEventSchema >
119
+ {
120
+ new DynamicEventSchema
121
+ {
122
+ DynamicEventName = "SampleEventName" ,
123
+ MinOccurrence = 1 ,
124
+ MaxOccurrence = 0 ,
125
+ Fields = new List < KeyValuePair < string , Type > >
126
+ {
127
+ new KeyValuePair < string , Type > ( "version" , typeof ( ushort ) ) ,
128
+ }
129
+ } ,
130
+ }
131
+ ) ;
132
+ } ;
133
+ test . Should ( ) . Throw < Exception > ( ) ;
134
+ }
135
+
136
+ private List < DynamicEventSchema > correctSingleSchema = new List < DynamicEventSchema >
137
+ {
138
+ new DynamicEventSchema
139
+ {
140
+ DynamicEventName = "SampleEventName" ,
141
+ MinOccurrence = 1 ,
142
+ Fields = new List < KeyValuePair < string , Type > >
143
+ {
144
+ new KeyValuePair < string , Type > ( "version" , typeof ( ushort ) ) ,
145
+ new KeyValuePair < string , Type > ( "Number" , typeof ( ulong ) ) ,
146
+ }
147
+ } ,
148
+ } ;
149
+
150
+ private DynamicEvent sampleEvent = new DynamicEvent (
151
+ "SampleEventName" ,
152
+ DateTime . Now ,
153
+ new byte [ ] { 1 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
154
+ ) ;
155
+
156
+ private DynamicEvent unknownEvent = new DynamicEvent (
157
+ "UnknownEventName" ,
158
+ DateTime . Now ,
159
+ new byte [ ] { 1 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
160
+ ) ;
161
+
162
+ [ TestMethod ]
163
+ public void TestMissedSingleEvent ( )
164
+ {
165
+ DynamicEventSchema . Set ( correctSingleSchema ) ;
166
+ List < DynamicEvent > dynamicEvents = new List < DynamicEvent > ( ) ;
167
+ Action test = ( ) =>
168
+ {
169
+ dynamic index = new DynamicIndex ( dynamicEvents ) ;
170
+ } ;
171
+ test . Should ( ) . Throw < Exception > ( ) ;
172
+ }
173
+
174
+ [ TestMethod ]
175
+ public void TestDuplicatedSingleEvent ( )
176
+ {
177
+ DynamicEventSchema . Set ( correctSingleSchema ) ;
178
+ List < DynamicEvent > dynamicEvents = new List < DynamicEvent >
179
+ {
180
+ sampleEvent ,
181
+ sampleEvent
182
+ } ;
183
+ Action test = ( ) =>
184
+ {
185
+ dynamic index = new DynamicIndex ( dynamicEvents ) ;
186
+ } ;
187
+ test . Should ( ) . Throw < Exception > ( ) ;
188
+ }
189
+
190
+ [ TestMethod ]
191
+ public void TestSingleEvent ( )
192
+ {
193
+ DynamicEventSchema . Set ( correctSingleSchema ) ;
194
+ List < DynamicEvent > dynamicEvents = new List < DynamicEvent >
195
+ {
196
+ sampleEvent
197
+ } ;
198
+ dynamic index = new DynamicIndex ( dynamicEvents ) ;
199
+
200
+ ( ( int ) index . SampleEventName . version ) . Should ( ) . Be ( 1 ) ;
201
+ ( ( int ) index . SampleEventName . Number ) . Should ( ) . Be ( 2 ) ;
202
+ string pattern = @"
203
+ SampleEventName
204
+ version : 1
205
+ Number : 2
206
+ TimeStamp : *
207
+ " . Trim ( ) ;
208
+ ( ( string ) index . SampleEventName . ToString ( ) ) . Should ( ) . Match ( pattern ) ;
209
+ }
210
+
211
+ private List < DynamicEventSchema > correctMultipleSchema = new List < DynamicEventSchema >
212
+ {
213
+ new DynamicEventSchema
214
+ {
215
+ DynamicEventName = "SampleEventName" ,
216
+ MinOccurrence = 1 ,
217
+ MaxOccurrence = 2 ,
218
+ Fields = new List < KeyValuePair < string , Type > >
219
+ {
220
+ new KeyValuePair < string , Type > ( "version" , typeof ( ushort ) ) ,
221
+ new KeyValuePair < string , Type > ( "Number" , typeof ( ulong ) ) ,
222
+ }
223
+ } ,
224
+ } ;
225
+
226
+ [ TestMethod ]
227
+ public void TestMissedMultipleEvent ( )
228
+ {
229
+ DynamicEventSchema . Set ( correctMultipleSchema ) ;
230
+ List < DynamicEvent > dynamicEvents = new List < DynamicEvent > ( ) ;
231
+ Action test = ( ) =>
232
+ {
233
+ dynamic index = new DynamicIndex ( dynamicEvents ) ;
234
+ } ;
235
+ test . Should ( ) . Throw < Exception > ( ) ;
236
+ }
237
+
238
+ [ TestMethod ]
239
+ public void TestTooManyMultipleEvents ( )
240
+ {
241
+ DynamicEventSchema . Set ( correctMultipleSchema ) ;
242
+ List < DynamicEvent > dynamicEvents = new List < DynamicEvent >
243
+ {
244
+ sampleEvent ,
245
+ sampleEvent ,
246
+ sampleEvent ,
247
+ } ;
248
+ Action test = ( ) =>
249
+ {
250
+ dynamic index = new DynamicIndex ( dynamicEvents ) ;
251
+ } ;
252
+ test . Should ( ) . Throw < Exception > ( ) ;
253
+ }
254
+
255
+ [ TestMethod ]
256
+ public void TestMultipleEvents ( )
257
+ {
258
+ DynamicEventSchema . Set ( correctMultipleSchema ) ;
259
+ List < DynamicEvent > dynamicEvents = new List < DynamicEvent >
260
+ {
261
+ sampleEvent ,
262
+ sampleEvent ,
263
+ } ;
264
+ dynamic index = new DynamicIndex ( dynamicEvents ) ;
265
+
266
+ ( ( int ) index . SampleEventName [ 0 ] . version ) . Should ( ) . Be ( 1 ) ;
267
+ ( ( int ) index . SampleEventName [ 0 ] . Number ) . Should ( ) . Be ( 2 ) ;
268
+ ( ( int ) index . SampleEventName [ 1 ] . version ) . Should ( ) . Be ( 1 ) ;
269
+ ( ( int ) index . SampleEventName [ 1 ] . Number ) . Should ( ) . Be ( 2 ) ;
270
+ }
271
+
272
+ [ TestMethod ]
273
+ public void TestOptionalEvent ( )
274
+ {
275
+ DynamicEventSchema . Set (
276
+ new List < DynamicEventSchema >
277
+ {
278
+ new DynamicEventSchema
279
+ {
280
+ DynamicEventName = "SampleEventName" ,
281
+ Fields = new List < KeyValuePair < string , Type > >
282
+ {
283
+ new KeyValuePair < string , Type > ( "version" , typeof ( ushort ) ) ,
284
+ new KeyValuePair < string , Type > ( "Number" , typeof ( ulong ) ) ,
285
+ }
286
+ } ,
287
+ }
288
+ ) ;
289
+ List < DynamicEvent > dynamicEvents = new List < DynamicEvent > ( ) ;
290
+ dynamic index = new DynamicIndex ( dynamicEvents ) ;
291
+ ( ( bool ) ( index . SampleEventName == null ) ) . Should ( ) . Be ( true ) ;
292
+ }
293
+
294
+ [ TestMethod ]
295
+ public void TestForgivingUnknownEvent ( )
296
+ {
297
+ DynamicEventSchema . Set ( correctSingleSchema ) ;
298
+ List < DynamicEvent > dynamicEvents = new List < DynamicEvent >
299
+ {
300
+ sampleEvent ,
301
+ unknownEvent
302
+ } ;
303
+ dynamic index = new DynamicIndex ( dynamicEvents ) ;
304
+ // As long as we don't throw exception, this is forgiving.
305
+ }
306
+
307
+ [ TestMethod ]
308
+ public void TestReportingUnknownEvent ( )
309
+ {
310
+ DynamicEventSchema . Set ( correctSingleSchema , false ) ;
311
+ List < DynamicEvent > dynamicEvents = new List < DynamicEvent >
312
+ {
313
+ sampleEvent ,
314
+ unknownEvent
315
+ } ;
316
+ Action test = ( ) =>
317
+ {
318
+ dynamic index = new DynamicIndex ( dynamicEvents ) ;
319
+ } ;
320
+ test . Should ( ) . Throw < Exception > ( ) ;
321
+ }
322
+ }
323
+ }
0 commit comments