@@ -593,3 +593,57 @@ func TestRetryIntervalAccumulation(t *testing.T) {
593
593
// Debug line to capture output of successful test
594
594
// assert.True(t, false)
595
595
}
596
+
597
+ func TestFlush (t * testing.T ) {
598
+ log .Log .SetLogLevel (log .DebugLevel )
599
+ hs := test .NewTestService (t , "http://localhost:8086" )
600
+ //
601
+ opts := write .DefaultOptions ().SetRetryInterval (1 )
602
+ ctx := context .Background ()
603
+ srv := NewService ("my-org" , "my-bucket" , hs , opts )
604
+
605
+ hs .SetReplyError (& http.Error {
606
+ Err : errors .New ("connection refused" ),
607
+ })
608
+
609
+ lines := test .GenRecords (5 )
610
+ // Test flush will fail all batches
611
+ for _ , line := range lines {
612
+ b := NewBatch (line , 20 )
613
+ _ = srv .HandleWrite (ctx , b )
614
+ }
615
+ assert .Equal (t , 5 , srv .retryQueue .list .Len ())
616
+ srv .Flush ()
617
+ assert .Len (t , hs .Lines (), 0 )
618
+
619
+ // Test flush will find all batches expired
620
+ for _ , line := range lines {
621
+ b := NewBatch (line , 5 )
622
+ _ = srv .HandleWrite (ctx , b )
623
+ }
624
+
625
+ assert .Equal (t , 5 , srv .retryQueue .list .Len ())
626
+ <- time .After (5 * time .Millisecond )
627
+
628
+ hs .SetReplyError (nil )
629
+ // all batches should expire
630
+ srv .Flush ()
631
+ assert .Len (t , hs .Lines (), 0 )
632
+ assert .Equal (t , 0 , srv .retryQueue .list .Len ())
633
+
634
+ // Test flush will succeed
635
+ hs .SetReplyError (& http.Error {
636
+ Err : errors .New ("connection refused" ),
637
+ })
638
+ for _ , line := range lines {
639
+ b := NewBatch (line , 5 )
640
+ _ = srv .HandleWrite (ctx , b )
641
+ }
642
+
643
+ assert .Equal (t , 5 , srv .retryQueue .list .Len ())
644
+ hs .SetReplyError (nil )
645
+ // all batches should expire
646
+ srv .Flush ()
647
+ assert .Len (t , hs .Lines (), 5 )
648
+ assert .Equal (t , 0 , srv .retryQueue .list .Len ())
649
+ }
0 commit comments