13
13
* limitations under the License.
14
14
*/
15
15
16
- using System . Diagnostics ;
16
+ using System ;
17
17
using System . Linq ;
18
+ using System . Threading ;
18
19
using MongoDB . Bson . Serialization ;
19
20
using MongoDB . Bson . Serialization . Conventions ;
20
21
using MongoDB . Bson . TestHelpers . XunitExtensions ;
@@ -29,16 +30,17 @@ public class ConventionRunnerTests
29
30
30
31
public ConventionRunnerTests ( )
31
32
{
32
- var stopwatch = new Stopwatch ( ) ;
33
+ int orderIndex = 0 ;
34
+
33
35
_pack = new ConventionPack ( ) ;
34
36
_pack . AddRange ( new IConvention [ ]
35
37
{
36
- new TrackingBeforeConvention ( stopwatch ) { Name = "1" } ,
37
- new TrackingMemberConvention ( stopwatch ) { Name = "3" } ,
38
- new TrackingAfterConvention ( stopwatch ) { Name = "5" } ,
39
- new TrackingMemberConvention ( stopwatch ) { Name = "4" } ,
40
- new TrackingAfterConvention ( stopwatch ) { Name = "6" } ,
41
- new TrackingBeforeConvention ( stopwatch ) { Name = "2" } ,
38
+ new TrackingBeforeConvention ( GetRunOrderIndex ) { Name = "1" } ,
39
+ new TrackingMemberConvention ( GetRunOrderIndex ) { Name = "3" } ,
40
+ new TrackingAfterConvention ( GetRunOrderIndex ) { Name = "5" } ,
41
+ new TrackingMemberConvention ( GetRunOrderIndex ) { Name = "4" } ,
42
+ new TrackingAfterConvention ( GetRunOrderIndex ) { Name = "6" } ,
43
+ new TrackingBeforeConvention ( GetRunOrderIndex ) { Name = "2" } ,
42
44
} ) ;
43
45
_subject = new ConventionRunner ( _pack ) ;
44
46
@@ -48,9 +50,9 @@ public ConventionRunnerTests()
48
50
cm . MapMember ( t => t . Prop2 ) ;
49
51
} ) ;
50
52
51
- stopwatch . Start ( ) ;
52
53
_subject . Apply ( classMap ) ;
53
- stopwatch . Stop ( ) ;
54
+
55
+ int GetRunOrderIndex ( ) => Interlocked . Increment ( ref orderIndex ) ;
54
56
}
55
57
56
58
[ Fact ]
@@ -63,7 +65,7 @@ public void TestThatItRunsAllConventions()
63
65
[ Fact ]
64
66
public void TestThatItRunsConventionsInTheProperOrder ( )
65
67
{
66
- var conventions = _pack . Conventions . OfType < ITrackRun > ( ) . OrderBy ( x => x . RunTicks ) . ToList ( ) ;
68
+ var conventions = _pack . Conventions . OfType < ITrackRun > ( ) . OrderBy ( x => x . RunOrder ) . ToList ( ) ;
67
69
for ( int i = 1 ; i < conventions . Count ; i ++ )
68
70
{
69
71
if ( conventions [ i - 1 ] . Name != i . ToString ( ) )
@@ -116,81 +118,81 @@ private interface ITrackRun
116
118
117
119
int RunCount { get ; }
118
120
119
- long RunTicks { get ; }
121
+ long RunOrder { get ; }
120
122
}
121
123
122
124
private class TrackingBeforeConvention : IClassMapConvention , ITrackRun
123
125
{
124
- private readonly Stopwatch _stopwatch ;
126
+ private readonly Func < int > _orderIndexProvider ;
125
127
126
- public TrackingBeforeConvention ( Stopwatch stopwatch )
128
+ public TrackingBeforeConvention ( Func < int > orderIndexProvider )
127
129
{
128
- _stopwatch = stopwatch ;
130
+ _orderIndexProvider = orderIndexProvider ;
129
131
}
130
132
131
133
public bool IsRun { get ; set ; }
132
134
133
135
public int RunCount { get ; set ; }
134
136
135
- public long RunTicks { get ; set ; }
137
+ public long RunOrder { get ; set ; }
136
138
137
139
public string Name { get ; set ; }
138
140
139
141
public void Apply ( BsonClassMap classMap )
140
142
{
141
143
IsRun = true ;
142
144
RunCount ++ ;
143
- RunTicks = _stopwatch . ElapsedTicks ;
145
+ RunOrder = _orderIndexProvider ( ) ;
144
146
}
145
147
}
146
148
147
149
private class TrackingMemberConvention : IMemberMapConvention , ITrackRun
148
150
{
149
- private readonly Stopwatch _stopwatch ;
151
+ private readonly Func < int > _orderIndexProvider ;
150
152
151
- public TrackingMemberConvention ( Stopwatch stopwatch )
153
+ public TrackingMemberConvention ( Func < int > orderIndexProvider )
152
154
{
153
- _stopwatch = stopwatch ;
155
+ _orderIndexProvider = orderIndexProvider ;
154
156
}
155
157
156
158
public bool IsRun { get ; set ; }
157
159
158
160
public int RunCount { get ; set ; }
159
161
160
- public long RunTicks { get ; set ; }
162
+ public long RunOrder { get ; set ; }
161
163
162
164
public string Name { get ; set ; }
163
165
164
166
public void Apply ( BsonMemberMap memberMap )
165
167
{
166
168
IsRun = true ;
167
169
RunCount ++ ;
168
- RunTicks = _stopwatch . ElapsedTicks ;
170
+ RunOrder = _orderIndexProvider ( ) ;
169
171
}
170
172
}
171
173
172
174
private class TrackingAfterConvention : IPostProcessingConvention , ITrackRun
173
175
{
174
- private readonly Stopwatch _stopwatch ;
176
+ private readonly Func < int > _orderIndexProvider ;
175
177
176
- public TrackingAfterConvention ( Stopwatch stopwatch )
178
+ public TrackingAfterConvention ( Func < int > orderIndexProvider )
177
179
{
178
- _stopwatch = stopwatch ;
180
+ _orderIndexProvider = orderIndexProvider ;
179
181
}
180
182
181
183
public bool IsRun { get ; set ; }
182
184
183
185
public int RunCount { get ; set ; }
184
186
185
- public long RunTicks { get ; set ; }
187
+ public long RunOrder { get ; set ; }
186
188
187
189
public string Name { get ; set ; }
188
190
189
191
public void PostProcess ( BsonClassMap classMap )
190
192
{
191
193
IsRun = true ;
192
194
RunCount ++ ;
193
- RunTicks = _stopwatch . ElapsedTicks ;
195
+ RunOrder = _orderIndexProvider ( ) ;
194
196
}
195
197
}
196
198
}
0 commit comments