1717using System . Collections . Generic ;
1818using MongoDB . Bson ;
1919using MongoDB . Driver . Core . Events ;
20+ using MongoDB . Driver . Core . Misc ;
2021
2122namespace MongoDB . Driver . Core
2223{
2324 public class EventCapturer : IEventSubscriber
2425 {
2526 private readonly Queue < object > _capturedEvents ;
27+ private readonly object _lock = new object ( ) ;
2628 private readonly IEventSubscriber _subscriber ;
2729 private readonly Dictionary < Type , Func < object , bool > > _eventsToCapture ;
2830
@@ -45,22 +47,31 @@ public EventCapturer Capture<TEvent>(Func<TEvent, bool> predicate = null)
4547
4648 public void Clear ( )
4749 {
48- _capturedEvents . Clear ( ) ;
50+ lock ( _lock )
51+ {
52+ _capturedEvents . Clear ( ) ;
53+ }
4954 }
5055
5156 public object Next ( )
5257 {
53- if ( _capturedEvents . Count == 0 )
58+ lock ( _lock )
5459 {
55- throw new Exception ( "No captured events exist." ) ;
56- }
60+ if ( _capturedEvents . Count == 0 )
61+ {
62+ throw new Exception ( "No captured events exist." ) ;
63+ }
5764
58- return _capturedEvents . Dequeue ( ) ;
65+ return _capturedEvents . Dequeue ( ) ;
66+ }
5967 }
6068
6169 public bool Any ( )
6270 {
63- return _capturedEvents . Count > 0 ;
71+ lock ( _lock )
72+ {
73+ return _capturedEvents . Count > 0 ;
74+ }
6475 }
6576
6677 public bool TryGetEventHandler < TEvent > ( out Action < TEvent > handler )
@@ -81,13 +92,22 @@ public bool TryGetEventHandler<TEvent>(out Action<TEvent> handler)
8192
8293 private void Capture < TEvent > ( TEvent @event )
8394 {
95+ var obj = @event as object ;
96+ if ( obj == null )
97+ {
98+ throw new ArgumentNullException ( nameof ( @event ) ) ;
99+ }
100+
84101 Func < object , bool > predicate ;
85102 if ( _eventsToCapture . TryGetValue ( typeof ( TEvent ) , out predicate ) && ! predicate ( @event ) )
86103 {
87104 return ;
88105 }
89106
90- _capturedEvents . Enqueue ( @event ) ;
107+ lock ( _lock )
108+ {
109+ _capturedEvents . Enqueue ( @event ) ;
110+ }
91111 }
92112
93113 private class CommandCapturer
0 commit comments