Skip to content

Commit bd47f5d

Browse files
committed
CSHARP3725: Fix TestThatItRunsConventionsInTheProperOrder test
1 parent d01da92 commit bd47f5d

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

tests/MongoDB.Bson.Tests/Serialization/Conventions/ConventionRunnerTests.cs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
* limitations under the License.
1414
*/
1515

16-
using System.Diagnostics;
16+
using System;
1717
using System.Linq;
18+
using System.Threading;
1819
using MongoDB.Bson.Serialization;
1920
using MongoDB.Bson.Serialization.Conventions;
2021
using MongoDB.Bson.TestHelpers.XunitExtensions;
@@ -29,16 +30,17 @@ public class ConventionRunnerTests
2930

3031
public ConventionRunnerTests()
3132
{
32-
var stopwatch = new Stopwatch();
33+
int orderIndex = 0;
34+
3335
_pack = new ConventionPack();
3436
_pack.AddRange(new IConvention[]
3537
{
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" },
4244
});
4345
_subject = new ConventionRunner(_pack);
4446

@@ -48,9 +50,9 @@ public ConventionRunnerTests()
4850
cm.MapMember(t => t.Prop2);
4951
});
5052

51-
stopwatch.Start();
5253
_subject.Apply(classMap);
53-
stopwatch.Stop();
54+
55+
int GetRunOrderIndex() => Interlocked.Increment(ref orderIndex);
5456
}
5557

5658
[Fact]
@@ -63,7 +65,7 @@ public void TestThatItRunsAllConventions()
6365
[Fact]
6466
public void TestThatItRunsConventionsInTheProperOrder()
6567
{
66-
var conventions = _pack.Conventions.OfType<ITrackRun>().OrderBy(x => x.RunTicks).ToList();
68+
var conventions = _pack.Conventions.OfType<ITrackRun>().OrderBy(x => x.RunOrder).ToList();
6769
for (int i = 1; i < conventions.Count; i++)
6870
{
6971
if (conventions[i - 1].Name != i.ToString())
@@ -116,81 +118,81 @@ private interface ITrackRun
116118

117119
int RunCount { get; }
118120

119-
long RunTicks { get; }
121+
long RunOrder { get; }
120122
}
121123

122124
private class TrackingBeforeConvention : IClassMapConvention, ITrackRun
123125
{
124-
private readonly Stopwatch _stopwatch;
126+
private readonly Func<int> _orderIndexProvider;
125127

126-
public TrackingBeforeConvention(Stopwatch stopwatch)
128+
public TrackingBeforeConvention(Func<int> orderIndexProvider)
127129
{
128-
_stopwatch = stopwatch;
130+
_orderIndexProvider = orderIndexProvider;
129131
}
130132

131133
public bool IsRun { get; set; }
132134

133135
public int RunCount { get; set; }
134136

135-
public long RunTicks { get; set; }
137+
public long RunOrder { get; set; }
136138

137139
public string Name { get; set; }
138140

139141
public void Apply(BsonClassMap classMap)
140142
{
141143
IsRun = true;
142144
RunCount++;
143-
RunTicks = _stopwatch.ElapsedTicks;
145+
RunOrder = _orderIndexProvider();
144146
}
145147
}
146148

147149
private class TrackingMemberConvention : IMemberMapConvention, ITrackRun
148150
{
149-
private readonly Stopwatch _stopwatch;
151+
private readonly Func<int> _orderIndexProvider;
150152

151-
public TrackingMemberConvention(Stopwatch stopwatch)
153+
public TrackingMemberConvention(Func<int> orderIndexProvider)
152154
{
153-
_stopwatch = stopwatch;
155+
_orderIndexProvider = orderIndexProvider;
154156
}
155157

156158
public bool IsRun { get; set; }
157159

158160
public int RunCount { get; set; }
159161

160-
public long RunTicks { get; set; }
162+
public long RunOrder { get; set; }
161163

162164
public string Name { get; set; }
163165

164166
public void Apply(BsonMemberMap memberMap)
165167
{
166168
IsRun = true;
167169
RunCount++;
168-
RunTicks = _stopwatch.ElapsedTicks;
170+
RunOrder = _orderIndexProvider();
169171
}
170172
}
171173

172174
private class TrackingAfterConvention : IPostProcessingConvention, ITrackRun
173175
{
174-
private readonly Stopwatch _stopwatch;
176+
private readonly Func<int> _orderIndexProvider;
175177

176-
public TrackingAfterConvention(Stopwatch stopwatch)
178+
public TrackingAfterConvention(Func<int> orderIndexProvider)
177179
{
178-
_stopwatch = stopwatch;
180+
_orderIndexProvider = orderIndexProvider;
179181
}
180182

181183
public bool IsRun { get; set; }
182184

183185
public int RunCount { get; set; }
184186

185-
public long RunTicks { get; set; }
187+
public long RunOrder { get; set; }
186188

187189
public string Name { get; set; }
188190

189191
public void PostProcess(BsonClassMap classMap)
190192
{
191193
IsRun = true;
192194
RunCount++;
193-
RunTicks = _stopwatch.ElapsedTicks;
195+
RunOrder = _orderIndexProvider();
194196
}
195197
}
196198
}

0 commit comments

Comments
 (0)