@@ -4,7 +4,6 @@ package sarama
4
4
5
5
import (
6
6
"errors"
7
- "github.com/stretchr/testify/assert"
8
7
"log"
9
8
"math"
10
9
"os"
@@ -16,6 +15,8 @@ import (
16
15
"testing"
17
16
"time"
18
17
18
+ "github.com/stretchr/testify/assert"
19
+
19
20
"github.com/fortytw2/leaktest"
20
21
"github.com/rcrowley/go-metrics"
21
22
"github.com/stretchr/testify/require"
@@ -638,6 +639,71 @@ func TestAsyncProducerMultipleRetriesWithBackoffFunc(t *testing.T) {
638
639
}
639
640
}
640
641
642
+ func TestAsyncProducerWithExponentialBackoffDurations (t * testing.T ) {
643
+ var backoffDurations []time.Duration
644
+ var mu sync.Mutex
645
+
646
+ topic := "my_topic"
647
+ maxBackoff := 2 * time .Second
648
+ config := NewTestConfig ()
649
+
650
+ initBackoff := 100 * time .Millisecond
651
+ innerBackoffFunc := NewExponentialBackoff (initBackoff , maxBackoff )
652
+ backoffFunc := func (retries , maxRetries int ) time.Duration {
653
+ duration := innerBackoffFunc (retries , maxRetries )
654
+ mu .Lock ()
655
+ backoffDurations = append (backoffDurations , duration )
656
+ mu .Unlock ()
657
+ return duration
658
+ }
659
+
660
+ config .Producer .Flush .Messages = 5
661
+ config .Producer .Return .Successes = true
662
+ config .Producer .Retry .Max = 3
663
+ config .Producer .Retry .BackoffFunc = backoffFunc
664
+
665
+ broker := NewMockBroker (t , 1 )
666
+
667
+ metadataResponse := new (MetadataResponse )
668
+ metadataResponse .AddBroker (broker .Addr (), broker .BrokerID ())
669
+ metadataResponse .AddTopicPartition (topic , 0 , broker .BrokerID (), nil , nil , nil , ErrNoError )
670
+ broker .Returns (metadataResponse )
671
+
672
+ producer , err := NewAsyncProducer ([]string {broker .Addr ()}, config )
673
+ if err != nil {
674
+ t .Fatal (err )
675
+ }
676
+
677
+ failResponse := new (ProduceResponse )
678
+ failResponse .AddTopicPartition (topic , 0 , ErrNotLeaderForPartition )
679
+ successResponse := new (ProduceResponse )
680
+ successResponse .AddTopicPartition (topic , 0 , ErrNoError )
681
+
682
+ broker .Returns (failResponse )
683
+ broker .Returns (metadataResponse )
684
+ broker .Returns (failResponse )
685
+ broker .Returns (metadataResponse )
686
+ broker .Returns (successResponse )
687
+
688
+ for i := 0 ; i < 5 ; i ++ {
689
+ producer .Input () <- & ProducerMessage {Topic : topic , Value : StringEncoder ("test" )}
690
+ }
691
+
692
+ expectResults (t , producer , 5 , 0 )
693
+ closeProducer (t , producer )
694
+ broker .Close ()
695
+
696
+ assert .Greater (t , backoffDurations [0 ], time .Duration (0 ), "Expected first backoff duration to be greater than 0" )
697
+
698
+ for i := 1 ; i < len (backoffDurations ); i ++ {
699
+ assert .Greater (t , backoffDurations [i ], time .Duration (0 ), "Expected backoff[%d] to be greater than 0" , i )
700
+ assert .GreaterOrEqual (t , backoffDurations [i ], backoffDurations [i - 1 ],
701
+ "Expected backoff[%d] >= backoff[%d], but got %v < %v" , i , i - 1 , backoffDurations [i ], backoffDurations [i - 1 ])
702
+ assert .LessOrEqual (t , backoffDurations [i ], maxBackoff ,
703
+ "Expected backoff[%d] <= maxBackoff, but got %v > %v" , i , backoffDurations [i ], maxBackoff )
704
+ }
705
+ }
706
+
641
707
// https://github.com/IBM/sarama/issues/2129
642
708
func TestAsyncProducerMultipleRetriesWithConcurrentRequests (t * testing.T ) {
643
709
// Logger = log.New(os.Stdout, "[sarama] ", log.LstdFlags)
0 commit comments