@@ -53,16 +53,17 @@ public void ChangeTime(Func<DateTime, DateTime> selector)
53
53
public async Task < Auditor > TraceStartup ( ClientCall callTrace = null )
54
54
{
55
55
//synchronous code path
56
- _cluster = _cluster ?? Cluster ( ) ;
56
+ _cluster ??= Cluster ( ) ;
57
57
if ( ! StartedUp ) AssertPoolBeforeStartup ? . Invoke ( _cluster . ConnectionPool ) ;
58
58
AssertPoolBeforeCall ? . Invoke ( _cluster . ConnectionPool ) ;
59
+ // ReSharper disable once MethodHasAsyncOverload
59
60
Response = _cluster . ClientCall ( callTrace ? . RequestOverrides ) ;
60
61
AuditTrail = Response . ApiCall . AuditTrail ;
61
62
if ( ! StartedUp ) AssertPoolAfterStartup ? . Invoke ( _cluster . ConnectionPool ) ;
62
63
AssertPoolAfterCall ? . Invoke ( _cluster . ConnectionPool ) ;
63
64
64
65
//async code path
65
- _clusterAsync = _clusterAsync ?? Cluster ( ) ;
66
+ _clusterAsync ??= Cluster ( ) ;
66
67
if ( ! StartedUp ) AssertPoolBeforeStartup ? . Invoke ( _clusterAsync . ConnectionPool ) ;
67
68
AssertPoolBeforeCall ? . Invoke ( _clusterAsync . ConnectionPool ) ;
68
69
ResponseAsync = await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) . ConfigureAwait ( false ) ;
@@ -71,19 +72,25 @@ public async Task<Auditor> TraceStartup(ClientCall callTrace = null)
71
72
AssertPoolAfterCall ? . Invoke ( _clusterAsync . ConnectionPool ) ;
72
73
return new Auditor ( _cluster , _clusterAsync ) ;
73
74
}
74
-
75
+
75
76
public async Task < Auditor > TraceCall ( ClientCall callTrace , int nthCall = 0 )
76
77
{
77
78
await TraceStartup ( callTrace ) . ConfigureAwait ( false ) ;
78
- return AssertAuditTrails ( callTrace , nthCall ) ;
79
+ return AssertAuditTrails ( callTrace , nthCall , true ) ;
80
+ }
81
+
82
+ public async Task < Auditor > TraceCall ( bool skipProductCheck , ClientCall callTrace , int nthCall = 0 )
83
+ {
84
+ await TraceStartup ( callTrace ) . ConfigureAwait ( false ) ;
85
+ return AssertAuditTrails ( callTrace , nthCall , skipProductCheck ) ;
79
86
}
80
87
81
88
#pragma warning disable 1998 // Async method lacks 'await' operators and will run synchronously
82
89
private async Task TraceException < TException > ( ClientCall callTrace , Action < TException > assert )
83
90
#pragma warning restore 1998 // Async method lacks 'await' operators and will run synchronously
84
91
where TException : ElasticsearchClientException
85
92
{
86
- _cluster = _cluster ?? Cluster ( ) ;
93
+ _cluster ??= Cluster ( ) ;
87
94
_cluster . ClientThrows ( true ) ;
88
95
AssertPoolBeforeCall ? . Invoke ( _cluster . ConnectionPool ) ;
89
96
@@ -94,7 +101,7 @@ private async Task TraceException<TException>(ClientCall callTrace, Action<TExce
94
101
AuditTrail = exception . AuditTrail ;
95
102
AssertPoolAfterCall ? . Invoke ( _cluster . ConnectionPool ) ;
96
103
97
- _clusterAsync = _clusterAsync ?? Cluster ( ) ;
104
+ _clusterAsync ??= Cluster ( ) ;
98
105
_clusterAsync . ClientThrows ( true ) ;
99
106
Func < Task > callAsync = async ( ) => ResponseAsync = await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) . ConfigureAwait ( false ) ;
100
107
exception = await TryCallAsync ( callAsync , assert ) . ConfigureAwait ( false ) ;
@@ -121,7 +128,7 @@ public async Task<Auditor> TraceUnexpectedElasticsearchException(ClientCall call
121
128
public async Task < Auditor > TraceElasticsearchExceptionOnResponse ( ClientCall callTrace , Action < ElasticsearchClientException > assert )
122
129
#pragma warning restore 1998
123
130
{
124
- _cluster = _cluster ?? Cluster ( ) ;
131
+ _cluster ??= Cluster ( ) ;
125
132
_cluster . ClientThrows ( false ) ;
126
133
AssertPoolBeforeCall ? . Invoke ( _cluster . ConnectionPool ) ;
127
134
@@ -130,14 +137,15 @@ public async Task<Auditor> TraceElasticsearchExceptionOnResponse(ClientCall call
130
137
131
138
if ( Response . ApiCall . Success ) throw new Exception ( "Expected call to not be valid" ) ;
132
139
133
- var exception = Response . ApiCall . OriginalException as ElasticsearchClientException ;
134
- if ( exception == null ) throw new Exception ( "OriginalException on response is not expected ElasticsearchClientException" ) ;
140
+ if ( Response . ApiCall . OriginalException is not ElasticsearchClientException exception )
141
+ throw new Exception ( "OriginalException on response is not expected ElasticsearchClientException" ) ;
142
+
135
143
assert ( exception ) ;
136
144
137
145
AuditTrail = exception . AuditTrail ;
138
146
AssertPoolAfterCall ? . Invoke ( _cluster . ConnectionPool ) ;
139
147
140
- _clusterAsync = _clusterAsync ?? Cluster ( ) ;
148
+ _clusterAsync ??= Cluster ( ) ;
141
149
_clusterAsync . ClientThrows ( false ) ;
142
150
Func < Task > callAsync = async ( ) => { ResponseAsync = await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) . ConfigureAwait ( false ) ; } ;
143
151
await callAsync ( ) . ConfigureAwait ( false ) ;
@@ -157,30 +165,38 @@ public async Task<Auditor> TraceElasticsearchExceptionOnResponse(ClientCall call
157
165
public async Task < Auditor > TraceUnexpectedException ( ClientCall callTrace , Action < UnexpectedElasticsearchClientException > assert )
158
166
#pragma warning restore 1998
159
167
{
160
- _cluster = _cluster ?? Cluster ( ) ;
168
+ _cluster ??= Cluster ( ) ;
161
169
AssertPoolBeforeCall ? . Invoke ( _cluster . ConnectionPool ) ;
162
170
163
- Action call = ( ) => Response = _cluster . ClientCall ( callTrace ? . RequestOverrides ) ;
164
- var exception = TryCall ( call , assert ) ;
171
+
172
+ var exception = TryCall ( Call , assert ) ;
165
173
assert ( exception ) ;
166
174
167
175
AuditTrail = exception . AuditTrail ;
168
176
AssertPoolAfterCall ? . Invoke ( _cluster . ConnectionPool ) ;
169
177
170
- _clusterAsync = _clusterAsync ?? Cluster ( ) ;
171
- Func < Task > callAsync = async ( ) => ResponseAsync = await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) . ConfigureAwait ( false ) ;
172
- exception = await TryCallAsync ( callAsync , assert ) . ConfigureAwait ( false ) ;
178
+ _clusterAsync ??= Cluster ( ) ;
179
+
180
+ exception = await TryCallAsync ( CallAsync , assert ) . ConfigureAwait ( false ) ;
173
181
assert ( exception ) ;
174
182
175
183
AsyncAuditTrail = exception . AuditTrail ;
176
184
AssertPoolAfterCall ? . Invoke ( _clusterAsync . ConnectionPool ) ;
185
+
177
186
return new Auditor ( _cluster , _clusterAsync ) ;
187
+
188
+ void Call ( ) => Response = _cluster . ClientCall ( callTrace ? . RequestOverrides ) ;
189
+ async Task CallAsync ( ) => ResponseAsync = await _clusterAsync . ClientCallAsync ( callTrace ? . RequestOverrides ) . ConfigureAwait ( false ) ;
178
190
}
179
191
180
- private Auditor AssertAuditTrails ( ClientCall callTrace , int nthCall )
192
+ private Auditor AssertAuditTrails ( ClientCall callTrace , int nthCall , bool skipProductCheck )
181
193
{
182
194
var nl = Environment . NewLine ;
183
- if ( AuditTrail . Count != AsyncAuditTrail . Count )
195
+
196
+ if ( skipProductCheck )
197
+ AuditTrail . RemoveAll ( a => a . Event is AuditEvent . ProductCheckOnStartup or AuditEvent . ProductCheckSuccess or AuditEvent . ProductCheckFailure ) ;
198
+
199
+ if ( AuditTrail . Count ( Predicate ) != AsyncAuditTrail . Count ( Predicate ) )
184
200
throw new Exception ( $ "{ nthCall } has a mismatch between sync and async. { nl } async:{ AuditTrail } { nl } sync:{ AsyncAuditTrail } ") ;
185
201
186
202
AssertTrailOnResponse ( callTrace , AuditTrail , true , nthCall ) ;
@@ -192,8 +208,13 @@ private Auditor AssertAuditTrails(ClientCall callTrace, int nthCall)
192
208
callTrace ? . AssertPoolAfterCall ? . Invoke ( _cluster . ConnectionPool ) ;
193
209
callTrace ? . AssertPoolAfterCall ? . Invoke ( _clusterAsync . ConnectionPool ) ;
194
210
return new Auditor ( _cluster , _clusterAsync ) ;
195
- }
196
211
212
+ // These happen one time only so should not be counted when comparing equality of audit trails
213
+ static bool Predicate ( Net . Audit auditEvent ) =>
214
+ auditEvent . Event != AuditEvent . ProductCheckOnStartup &&
215
+ auditEvent . Event != AuditEvent . ProductCheckFailure &&
216
+ auditEvent . Event != AuditEvent . ProductCheckSuccess ;
217
+ }
197
218
198
219
public void VisualizeCalls ( int numberOfCalls )
199
220
{
@@ -218,14 +239,24 @@ private static string AuditTrailToString(List<Elasticsearch.Net.Audit> auditTrai
218
239
return actualAuditTrail ;
219
240
}
220
241
242
+
221
243
public async Task < Auditor > TraceCalls ( params ClientCall [ ] audits )
222
244
{
223
245
var auditor = this ;
224
- foreach ( var a in audits . Select ( ( a , i ) => new { a , i } ) ) auditor = await auditor . TraceCall ( a . a , a . i ) . ConfigureAwait ( false ) ;
246
+ foreach ( var a in audits . Select ( ( a , i ) => new { a , i } ) )
247
+ auditor = await auditor . TraceCall ( a . a , a . i ) . ConfigureAwait ( false ) ;
248
+ return auditor ;
249
+ }
250
+
251
+ public async Task < Auditor > TraceCalls ( bool skipProductCheck , params ClientCall [ ] audits )
252
+ {
253
+ var auditor = this ;
254
+ foreach ( var a in audits . Select ( ( a , i ) => new { a , i } ) )
255
+ auditor = await auditor . TraceCall ( skipProductCheck , a . a , a . i ) . ConfigureAwait ( false ) ;
225
256
return auditor ;
226
257
}
227
258
228
- private static void AssertTrailOnResponse ( ClientCall callTrace , List < Elasticsearch . Net . Audit > auditTrail , bool sync , int nthCall )
259
+ private static void AssertTrailOnResponse ( ClientCall callTrace , IReadOnlyCollection < Net . Audit > auditTrail , bool sync , int nthCall )
229
260
{
230
261
var typeOfTrail = ( sync ? "synchronous" : "asynchronous" ) + " audit trail" ;
231
262
var nthClientCall = ( nthCall + 1 ) . ToOrdinal ( ) ;
0 commit comments