10
10
using Segment . Serialization ;
11
11
using Tests . Utils ;
12
12
using Xunit ;
13
+ using System . Linq ;
13
14
14
15
namespace Tests . Utilities
15
16
{
@@ -225,6 +226,86 @@ public void TestConfigWithEventPipelineProviders(IEventPipelineProvider provider
225
226
analytics . Track ( "test" ) ;
226
227
}
227
228
229
+ [ Fact ]
230
+ public void TestSyncEventPipelineProviderWaits ( )
231
+ {
232
+ const int iterations = 100 ;
233
+ const int newAnalyticsEvery = 10 ;
234
+ const int eventCount = 10 ;
235
+
236
+ int totalTracks = 0 ;
237
+ int totalUploads = 0 ;
238
+
239
+ _mockHttpClient
240
+ . Setup ( client => client . Upload ( It . IsAny < byte [ ] > ( ) ) )
241
+ . Callback < byte [ ] > ( bytes =>
242
+ {
243
+ string content = System . Text . Encoding . UTF8 . GetString ( bytes ) ;
244
+ int count = content . Split ( new string [ ] { "test" } , StringSplitOptions . None ) . Length - 1 ;
245
+ totalUploads += count ;
246
+ } )
247
+ . ReturnsAsync ( true ) ;
248
+
249
+ var config = new Configuration (
250
+ writeKey : "123" ,
251
+ useSynchronizeDispatcher : true ,
252
+ flushInterval : 100000 ,
253
+ flushAt : eventCount * 2 ,
254
+ httpClientProvider : new MockHttpClientProvider ( _mockHttpClient ) ,
255
+ storageProvider : new InMemoryStorageProvider ( ) ,
256
+ eventPipelineProvider : new SyncEventPipelineProvider ( )
257
+ ) ;
258
+
259
+ var analytics = new Analytics ( config ) ;
260
+ for ( int j = 0 ; j < iterations ; j ++ )
261
+ {
262
+ if ( j % newAnalyticsEvery == 0 )
263
+ {
264
+ analytics = new Analytics ( config ) ;
265
+ }
266
+ _mockHttpClient . Invocations . Clear ( ) ;
267
+ for ( int i = 0 ; i < eventCount ; i ++ )
268
+ {
269
+ analytics . Track ( $ "test { i } ") ;
270
+ totalTracks ++ ;
271
+ }
272
+ analytics . Flush ( ) ;
273
+
274
+ #pragma warning disable CS4014 // Silly compiler, this isn't an invocation so it doesn't need to be awaited
275
+ _mockHttpClient . Verify ( client => client . Upload ( It . IsAny < byte [ ] > ( ) ) , Times . AtLeastOnce , $ "Iteration { j } of { eventCount } ") ;
276
+ #pragma warning restore CS4014
277
+ IInvocation lastUploadInvocation = _mockHttpClient . Invocations . Last ( invocation => invocation . Method . Name == "Upload" ) ;
278
+ int testsUploaded = System . Text . Encoding . UTF8
279
+ . GetString ( ( byte [ ] ) lastUploadInvocation . Arguments [ 0 ] )
280
+ . Split ( new string [ ] { "test" } , StringSplitOptions . None ) . Length - 1 ;
281
+ Assert . Equal ( eventCount , testsUploaded ) ;
282
+ }
283
+ Assert . Equal ( totalTracks , totalUploads ) ;
284
+ }
285
+
286
+ [ Fact ]
287
+ public void TestRepeatedFlushesDontHang ( )
288
+ {
289
+ var config = new Configuration (
290
+ writeKey : "123" ,
291
+ useSynchronizeDispatcher : true ,
292
+ flushInterval : 0 ,
293
+ flushAt : 1 ,
294
+ httpClientProvider : new MockHttpClientProvider ( _mockHttpClient ) ,
295
+ storageProvider : new MockStorageProvider ( _storage ) ,
296
+ eventPipelineProvider : new SyncEventPipelineProvider ( 5000 )
297
+ ) ;
298
+ var analytics = new Analytics ( config ) ;
299
+ analytics . Track ( "test" ) ;
300
+ DateTime startTime = DateTime . Now ;
301
+ analytics . Flush ( ) ;
302
+ analytics . Flush ( ) ;
303
+ analytics . Flush ( ) ;
304
+ analytics . Flush ( ) ;
305
+ analytics . Flush ( ) ;
306
+ Assert . True ( DateTime . Now - startTime < TimeSpan . FromMilliseconds ( 100 ) ) ;
307
+ }
308
+
228
309
[ Fact ]
229
310
public void TestConfigWithCustomEventPipelineProvider ( )
230
311
{
@@ -248,13 +329,9 @@ public void TestConfigWithCustomEventPipelineProvider()
248
329
249
330
public class CustomEventPipelineProvider : IEventPipelineProvider
250
331
{
251
- public CustomEventPipelineProvider ( )
252
- {
253
- }
254
-
332
+ public CustomEventPipelineProvider ( ) { }
255
333
public IEventPipeline Create ( Analytics analytics , string key )
256
334
{
257
- // Custom implementation
258
335
return new CustomEventPipeline ( analytics , key ) ;
259
336
}
260
337
0 commit comments