@@ -236,9 +236,9 @@ public void Init_MultipleCalls_ReplacesHubWithLatest()
236236 }
237237
238238 [ Theory ]
239- [ InlineData ( true ) ]
240- [ InlineData ( false ) ]
241- [ InlineData ( null ) ]
239+ [ InlineData ( true ) ] // InitCacheFlushTimeout is more than enough time to process all messages
240+ [ InlineData ( false ) ] // InitCacheFlushTimeout is less time than needed to process all messages
241+ [ InlineData ( null ) ] // InitCacheFlushTimeout is not set
242242 public async Task Init_WithCache_BlocksUntilExistingCacheIsFlushed ( bool ? testDelayWorking )
243243 {
244244 // Arrange
@@ -253,7 +253,7 @@ public async Task Init_WithCache_BlocksUntilExistingCacheIsFlushed(bool? testDel
253253 Dsn = ValidDsnWithoutSecret ,
254254 CacheDirectoryPath = cachePath
255255 } , startWorker : false ) ;
256- const int numEnvelopes = 3 ;
256+ const int numEnvelopes = 5 ; // Not too many, or this will be slow. Not too few or this will be flaky.
257257 for ( var i = 0 ; i < numEnvelopes ; i ++ )
258258 {
259259 using var envelope = Envelope . FromEvent ( new SentryEvent ( ) ) ;
@@ -269,9 +269,9 @@ public async Task Init_WithCache_BlocksUntilExistingCacheIsFlushed(bool? testDel
269269 // Set the timeout for the desired result
270270 var initFlushTimeout = testDelayWorking switch
271271 {
272- true => TimeSpan . FromTicks ( processingDelayPerEnvelope . Ticks * ( numEnvelopes + 1 ) ) ,
273- false => TimeSpan . FromTicks ( ( long ) ( processingDelayPerEnvelope . Ticks * 1.9 ) ) , // not quite 2, since we want 1 envelope
274- null => TimeSpan . Zero
272+ true => TimeSpan . FromTicks ( processingDelayPerEnvelope . Ticks * ( numEnvelopes + 1 ) ) , // more than enough
273+ false => TimeSpan . FromTicks ( processingDelayPerEnvelope . Ticks * 3 ) , // enough for at least one, but not all
274+ null => TimeSpan . Zero // none at all
275275 } ;
276276
277277 // Act
@@ -294,18 +294,24 @@ public async Task Init_WithCache_BlocksUntilExistingCacheIsFlushed(bool? testDel
294294
295295 // Assert
296296 var actualCount = transport . GetSentEnvelopes ( ) . Count ;
297- var expectedCount = testDelayWorking switch
298- {
299- true => numEnvelopes , // We waited long enough to have them all
300- false => 1 , // We only waited long enough to have one
301- null => 0 // We shouldn't have any, as we didn't ask to flush the cache on init
302- } ;
303-
304- Assert . Equal ( expectedCount , actualCount ) ;
305297
306- if ( testDelayWorking is true )
298+ switch ( testDelayWorking )
307299 {
308- Assert . True ( stopwatch . Elapsed < initFlushTimeout , "Should not have waited for the entire timeout!" ) ;
300+ case true :
301+ // We waited long enough to have them all
302+ Assert . Equal ( numEnvelopes , actualCount ) ;
303+
304+ // But we should not have waited longer than we needed to
305+ Assert . True ( stopwatch . Elapsed < initFlushTimeout , "Should not have waited for the entire timeout!" ) ;
306+ break ;
307+ case false :
308+ // We only waited long enough to have at least one, but not all of them
309+ Assert . True ( actualCount is > 0 and < numEnvelopes ) ;
310+ break ;
311+ case null :
312+ // We shouldn't have any, as we didn't ask to flush the cache on init
313+ Assert . Equal ( 0 , actualCount ) ;
314+ break ;
309315 }
310316 }
311317 finally
0 commit comments