Skip to content

Commit 9cb3c0d

Browse files
committed
fixed command monitoring tests to only monitor for events they expect.
1 parent 0c52987 commit 9cb3c0d

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/MongoDB.Driver.Core.TestHelpers/EventCapturer.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,22 @@ public class EventCapturer : IEventSubscriber
2424
{
2525
private readonly Queue<object> _capturedEvents;
2626
private readonly IEventSubscriber _subscriber;
27-
private readonly List<Type> _eventsToCapture;
27+
private readonly Dictionary<Type, Func<object, bool>> _eventsToCapture;
2828

2929
public EventCapturer()
3030
{
3131
_capturedEvents = new Queue<object>();
3232
_subscriber = new ReflectionEventSubscriber(new CommandCapturer(this));
33-
_eventsToCapture = new List<Type>();
33+
_eventsToCapture = new Dictionary<Type, Func<object, bool>>();
3434
}
3535

36-
public EventCapturer Capture<TEvent>()
36+
public EventCapturer Capture<TEvent>(Func<TEvent, bool> predicate = null)
3737
{
38-
_eventsToCapture.Add(typeof(TEvent));
38+
if (predicate == null)
39+
{
40+
predicate = o => true;
41+
}
42+
_eventsToCapture.Add(typeof(TEvent), o => predicate((TEvent)o));
3943
return this;
4044
}
4145

@@ -61,22 +65,28 @@ public bool Any()
6165

6266
public bool TryGetEventHandler<TEvent>(out Action<TEvent> handler)
6367
{
64-
if (_eventsToCapture.Count > 0 && !_eventsToCapture.Contains(typeof(TEvent)))
68+
if (_eventsToCapture.Count > 0 && !_eventsToCapture.ContainsKey(typeof(TEvent)))
6569
{
6670
handler = null;
6771
return false;
6872
}
6973

70-
if (!_subscriber.TryGetEventHandler<TEvent>(out handler))
74+
if (!_subscriber.TryGetEventHandler(out handler))
7175
{
7276
handler = e => Capture(e);
7377
}
7478

7579
return true;
7680
}
7781

78-
private void Capture(object @event)
82+
private void Capture<TEvent>(TEvent @event)
7983
{
84+
Func<object, bool> predicate;
85+
if (_eventsToCapture.TryGetValue(typeof(TEvent), out predicate) && !predicate(@event))
86+
{
87+
return;
88+
}
89+
8090
_capturedEvents.Enqueue(@event);
8191
}
8292

src/MongoDB.Driver.Tests/Specifications/command-monitoring/TestRunner.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,22 @@ public class TestRunner
3636
private static EventCapturer __capturedEvents;
3737
private static Dictionary<string, Func<ICrudOperationTest>> __tests;
3838

39+
private static string[] __commandsToCapture;
40+
3941
[TestFixtureSetUp]
4042
public void TestFixtureSetup()
4143
{
44+
__commandsToCapture = new string[]
45+
{
46+
"delete",
47+
"insert",
48+
"update",
49+
"find",
50+
"count",
51+
"killCursors",
52+
"getMore"
53+
};
54+
4255
__tests = new Dictionary<string, Func<ICrudOperationTest>>
4356
{
4457
{ "bulkWrite", () => new BulkWriteTest() },
@@ -57,9 +70,9 @@ public void TestFixtureSetup()
5770
public void TestFixtureSetUp()
5871
{
5972
__capturedEvents = new EventCapturer()
60-
.Capture<CommandStartedEvent>()
61-
.Capture<CommandSucceededEvent>()
62-
.Capture<CommandFailedEvent>();
73+
.Capture<CommandStartedEvent>(e => __commandsToCapture.Contains(e.CommandName))
74+
.Capture<CommandSucceededEvent>(e => __commandsToCapture.Contains(e.CommandName))
75+
.Capture<CommandFailedEvent>(e => __commandsToCapture.Contains(e.CommandName));
6376

6477
var settings = new MongoClientSettings
6578
{

0 commit comments

Comments
 (0)