@@ -52,6 +52,10 @@ public abstract class IntegrationFixture : IAsyncLifetime
5252 private static readonly bool s_isVerbose = false ;
5353 private static int _connectionIdx = 0 ;
5454
55+ private Exception _connectionCallbackException ;
56+ private Exception _connectionRecoveryException ;
57+ private Exception _channelCallbackException ;
58+
5559 protected readonly RabbitMQCtl _rabbitMQCtl ;
5660
5761 protected ConnectionFactory _connFactory ;
@@ -77,7 +81,12 @@ public abstract class IntegrationFixture : IAsyncLifetime
7781
7882 static IntegrationFixture ( )
7983 {
84+
85+ #if NET6_0_OR_GREATER
86+ S_Random = Random . Shared ;
87+ #else
8088 S_Random = new Random ( ) ;
89+ #endif
8190 s_isRunningInCI = InitIsRunningInCI ( ) ;
8291 s_isVerbose = InitIsVerbose ( ) ;
8392
@@ -146,8 +155,10 @@ public virtual async Task InitializeAsync()
146155
147156 if ( IsVerbose )
148157 {
149- AddCallbackHandlers ( ) ;
158+ AddCallbackShutdownHandlers ( ) ;
150159 }
160+
161+ AddCallbackExceptionHandlers ( ) ;
151162 }
152163
153164 if ( _connFactory . AutomaticRecoveryEnabled )
@@ -181,42 +192,122 @@ public virtual async Task DisposeAsync()
181192 _channel = null ;
182193 _conn = null ;
183194 }
195+
196+ DisposeAssertions ( ) ;
197+ }
198+
199+ protected virtual void DisposeAssertions ( )
200+ {
201+ if ( _connectionRecoveryException != null )
202+ {
203+ Assert . Fail ( $ "unexpected connection recovery exception: { _connectionRecoveryException } ") ;
204+ }
205+
206+ if ( _connectionCallbackException != null )
207+ {
208+ Assert . Fail ( $ "unexpected connection callback exception: { _connectionCallbackException } ") ;
209+ }
210+
211+ if ( _channelCallbackException != null )
212+ {
213+ Assert . Fail ( $ "unexpected channel callback exception: { _channelCallbackException } ") ;
214+ }
184215 }
185216
186- protected virtual void AddCallbackHandlers ( )
217+ protected void AddCallbackExceptionHandlers ( )
187218 {
188219 if ( _conn != null )
189220 {
190- _conn . CallbackException += ( o , ea ) =>
221+ _conn . ConnectionRecoveryError += ( s , ea ) =>
191222 {
192- _output . WriteLine ( "{0} connection callback exception: {1}" ,
193- _testDisplayName , ea . Exception ) ;
223+ _connectionRecoveryException = ea . Exception ;
224+
225+ if ( IsVerbose )
226+ {
227+ try
228+ {
229+ _output . WriteLine ( $ "{ 0 } connection recovery exception: { 1 } ",
230+ _testDisplayName , _connectionRecoveryException ) ;
231+ }
232+ catch ( InvalidOperationException )
233+ {
234+ }
235+ }
194236 } ;
195237
196- _conn . ConnectionShutdown += ( o , ea ) =>
238+ _conn . CallbackException += ( o , ea ) =>
197239 {
198- HandleConnectionShutdown ( _conn , ea , ( args ) =>
240+ _connectionCallbackException = ea . Exception ;
241+
242+ if ( IsVerbose )
199243 {
200- _output . WriteLine ( "{0} connection shutdown, args: {1}" ,
201- _testDisplayName , args ) ;
202- } ) ;
244+ try
245+ {
246+ _output . WriteLine ( "{0} connection callback exception: {1}" ,
247+ _testDisplayName , _connectionCallbackException ) ;
248+ }
249+ catch ( InvalidOperationException )
250+ {
251+ }
252+ }
203253 } ;
204254 }
205255
206256 if ( _channel != null )
207257 {
208258 _channel . CallbackException += ( o , ea ) =>
209259 {
210- _output . WriteLine ( "{0} channel callback exception: {1}" ,
211- _testDisplayName , ea . Exception ) ;
260+ _channelCallbackException = ea . Exception ;
261+
262+ if ( IsVerbose )
263+ {
264+ try
265+ {
266+ _output . WriteLine ( "{0} channel callback exception: {1}" ,
267+ _testDisplayName , _channelCallbackException ) ;
268+ }
269+ catch ( InvalidOperationException )
270+ {
271+ }
272+ }
273+ } ;
274+ }
275+ }
276+
277+ protected void AddCallbackShutdownHandlers ( )
278+ {
279+ if ( _conn != null )
280+ {
281+ _conn . ConnectionShutdown += ( o , ea ) =>
282+ {
283+ HandleConnectionShutdown ( _conn , ea , ( args ) =>
284+ {
285+ try
286+ {
287+ _output . WriteLine ( "{0} connection shutdown, args: {1}" ,
288+ _testDisplayName , args ) ;
289+ }
290+ catch ( InvalidOperationException )
291+ {
292+ }
293+ } ) ;
212294 } ;
295+ }
213296
297+ if ( _channel != null )
298+ {
214299 _channel . ChannelShutdown += ( o , ea ) =>
215300 {
216301 HandleChannelShutdown ( _channel , ea , ( args ) =>
217302 {
218- _output . WriteLine ( "{0} channel shutdown, args: {1}" ,
219- _testDisplayName , args ) ;
303+ try
304+ {
305+ _output . WriteLine ( "{0} channel shutdown, args: {1}" ,
306+ _testDisplayName , args ) ;
307+ }
308+ catch ( InvalidOperationException )
309+ {
310+ }
220311 } ) ;
221312 } ;
222313 }
@@ -405,6 +496,11 @@ protected static Task AssertRanToCompletion(IEnumerable<Task> tasks)
405496 return DoAssertRanToCompletion ( tasks ) ;
406497 }
407498
499+ internal static void AssertRecordedQueues ( AutorecoveringConnection c , int n )
500+ {
501+ Assert . Equal ( n , c . RecordedQueuesCount ) ;
502+ }
503+
408504 protected static Task WaitAsync ( TaskCompletionSource < bool > tcs , string desc )
409505 {
410506 return WaitAsync ( tcs , WaitSpan , desc ) ;
@@ -524,11 +620,7 @@ protected static string GetUniqueString(ushort length)
524620 protected static byte [ ] GetRandomBody ( ushort size = 1024 )
525621 {
526622 var body = new byte [ size ] ;
527- #if NET6_0_OR_GREATER
528- Random . Shared . NextBytes ( body ) ;
529- #else
530623 S_Random . NextBytes ( body ) ;
531- #endif
532624 return body ;
533625 }
534626
@@ -543,7 +635,7 @@ protected static TaskCompletionSource<bool> PrepareForRecovery(IConnection conn)
543635 var tcs = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
544636
545637 AutorecoveringConnection aconn = conn as AutorecoveringConnection ;
546- aconn . RecoverySucceeded += ( source , ea ) => tcs . SetResult ( true ) ;
638+ aconn . RecoverySucceeded += ( source , ea ) => tcs . TrySetResult ( true ) ;
547639
548640 return tcs ;
549641 }
0 commit comments