@@ -535,6 +535,91 @@ func TestBatchSenderWithTimeout(t *testing.T) {
535
535
assert .EqualValues (t , 12 , sink .itemsCount .Load ())
536
536
}
537
537
538
+ func TestBatchSenderTimerResetNoConflict (t * testing.T ) {
539
+ delayBatchMergeFunc := func (_ context.Context , r1 Request , r2 Request ) (Request , error ) {
540
+ time .Sleep (30 * time .Millisecond )
541
+ if r1 == nil {
542
+ return r2 , nil
543
+ }
544
+ fr1 := r1 .(* fakeRequest )
545
+ fr2 := r2 .(* fakeRequest )
546
+ if fr2 .mergeErr != nil {
547
+ return nil , fr2 .mergeErr
548
+ }
549
+ return & fakeRequest {
550
+ items : fr1 .items + fr2 .items ,
551
+ sink : fr1 .sink ,
552
+ exportErr : fr2 .exportErr ,
553
+ delay : fr1 .delay + fr2 .delay ,
554
+ }, nil
555
+ }
556
+ bCfg := exporterbatcher .NewDefaultConfig ()
557
+ bCfg .MinSizeItems = 8
558
+ bCfg .FlushTimeout = 50 * time .Millisecond
559
+ be , err := newBaseExporter (defaultSettings , defaultDataType , newNoopObsrepSender ,
560
+ WithBatcher (bCfg , WithRequestBatchFuncs (delayBatchMergeFunc , fakeBatchMergeSplitFunc )))
561
+ require .NoError (t , err )
562
+ require .NoError (t , be .Start (context .Background (), componenttest .NewNopHost ()))
563
+ sink := newFakeRequestSink ()
564
+
565
+ // Send 2 concurrent requests that should be merged in one batch in the same interval as the flush timer
566
+ go func () {
567
+ require .NoError (t , be .send (context .Background (), & fakeRequest {items : 4 , sink : sink }))
568
+ }()
569
+ time .Sleep (30 * time .Millisecond )
570
+ go func () {
571
+ require .NoError (t , be .send (context .Background (), & fakeRequest {items : 4 , sink : sink }))
572
+ }()
573
+
574
+ // The batch should be sent either with the flush interval or by reaching the minimum items size with no conflict
575
+ assert .EventuallyWithT (t , func (c * assert.CollectT ) {
576
+ assert .LessOrEqual (c , uint64 (1 ), sink .requestsCount .Load ())
577
+ assert .EqualValues (c , 8 , sink .itemsCount .Load ())
578
+ }, 200 * time .Millisecond , 10 * time .Millisecond )
579
+
580
+ require .NoError (t , be .Shutdown (context .Background ()))
581
+ }
582
+
583
+ func TestBatchSenderTimerFlush (t * testing.T ) {
584
+ bCfg := exporterbatcher .NewDefaultConfig ()
585
+ bCfg .MinSizeItems = 8
586
+ bCfg .FlushTimeout = 100 * time .Millisecond
587
+ be , err := newBaseExporter (defaultSettings , defaultDataType , newNoopObsrepSender ,
588
+ WithBatcher (bCfg , WithRequestBatchFuncs (fakeBatchMergeFunc , fakeBatchMergeSplitFunc )))
589
+ require .NoError (t , err )
590
+ require .NoError (t , be .Start (context .Background (), componenttest .NewNopHost ()))
591
+ sink := newFakeRequestSink ()
592
+ time .Sleep (50 * time .Millisecond )
593
+
594
+ // Send 2 concurrent requests that should be merged in one batch and sent immediately
595
+ go func () {
596
+ require .NoError (t , be .send (context .Background (), & fakeRequest {items : 4 , sink : sink }))
597
+ }()
598
+ go func () {
599
+ require .NoError (t , be .send (context .Background (), & fakeRequest {items : 4 , sink : sink }))
600
+ }()
601
+ assert .EventuallyWithT (t , func (c * assert.CollectT ) {
602
+ assert .LessOrEqual (c , uint64 (1 ), sink .requestsCount .Load ())
603
+ assert .EqualValues (c , 8 , sink .itemsCount .Load ())
604
+ }, 30 * time .Millisecond , 5 * time .Millisecond )
605
+
606
+ // Send another request that should be flushed after 100ms instead of 50ms since last flush
607
+ go func () {
608
+ require .NoError (t , be .send (context .Background (), & fakeRequest {items : 4 , sink : sink }))
609
+ }()
610
+
611
+ // Confirm that it is not flushed in 50ms
612
+ time .Sleep (60 * time .Millisecond )
613
+ assert .LessOrEqual (t , uint64 (1 ), sink .requestsCount .Load ())
614
+ assert .EqualValues (t , 8 , sink .itemsCount .Load ())
615
+
616
+ // Confirm that it is flushed after 100ms (using 60+50=110 here to be safe)
617
+ time .Sleep (50 * time .Millisecond )
618
+ assert .LessOrEqual (t , uint64 (2 ), sink .requestsCount .Load ())
619
+ assert .EqualValues (t , 12 , sink .itemsCount .Load ())
620
+ require .NoError (t , be .Shutdown (context .Background ()))
621
+ }
622
+
538
623
func queueBatchExporter (t * testing.T , batchOption Option ) * baseExporter {
539
624
be , err := newBaseExporter (defaultSettings , defaultDataType , newNoopObsrepSender , batchOption ,
540
625
WithRequestQueue (exporterqueue .NewDefaultConfig (), exporterqueue .NewMemoryQueueFactory [Request ]()))
0 commit comments