17
17
using System . Collections . Generic ;
18
18
using MongoDB . Bson ;
19
19
using MongoDB . Driver . Core . Events ;
20
+ using MongoDB . Driver . Core . Misc ;
20
21
21
22
namespace MongoDB . Driver . Core
22
23
{
23
24
public class EventCapturer : IEventSubscriber
24
25
{
25
26
private readonly Queue < object > _capturedEvents ;
27
+ private readonly object _lock = new object ( ) ;
26
28
private readonly IEventSubscriber _subscriber ;
27
29
private readonly Dictionary < Type , Func < object , bool > > _eventsToCapture ;
28
30
@@ -45,22 +47,31 @@ public EventCapturer Capture<TEvent>(Func<TEvent, bool> predicate = null)
45
47
46
48
public void Clear ( )
47
49
{
48
- _capturedEvents . Clear ( ) ;
50
+ lock ( _lock )
51
+ {
52
+ _capturedEvents . Clear ( ) ;
53
+ }
49
54
}
50
55
51
56
public object Next ( )
52
57
{
53
- if ( _capturedEvents . Count == 0 )
58
+ lock ( _lock )
54
59
{
55
- throw new Exception ( "No captured events exist." ) ;
56
- }
60
+ if ( _capturedEvents . Count == 0 )
61
+ {
62
+ throw new Exception ( "No captured events exist." ) ;
63
+ }
57
64
58
- return _capturedEvents . Dequeue ( ) ;
65
+ return _capturedEvents . Dequeue ( ) ;
66
+ }
59
67
}
60
68
61
69
public bool Any ( )
62
70
{
63
- return _capturedEvents . Count > 0 ;
71
+ lock ( _lock )
72
+ {
73
+ return _capturedEvents . Count > 0 ;
74
+ }
64
75
}
65
76
66
77
public bool TryGetEventHandler < TEvent > ( out Action < TEvent > handler )
@@ -81,13 +92,22 @@ public bool TryGetEventHandler<TEvent>(out Action<TEvent> handler)
81
92
82
93
private void Capture < TEvent > ( TEvent @event )
83
94
{
95
+ var obj = @event as object ;
96
+ if ( obj == null )
97
+ {
98
+ throw new ArgumentNullException ( nameof ( @event ) ) ;
99
+ }
100
+
84
101
Func < object , bool > predicate ;
85
102
if ( _eventsToCapture . TryGetValue ( typeof ( TEvent ) , out predicate ) && ! predicate ( @event ) )
86
103
{
87
104
return ;
88
105
}
89
106
90
- _capturedEvents . Enqueue ( @event ) ;
107
+ lock ( _lock )
108
+ {
109
+ _capturedEvents . Enqueue ( @event ) ;
110
+ }
91
111
}
92
112
93
113
private class CommandCapturer
0 commit comments