@@ -638,6 +638,66 @@ func TestAsyncProducerMultipleRetriesWithBackoffFunc(t *testing.T) {
638
638
}
639
639
}
640
640
641
+ func TestAsyncProducerWithExponentialBackoffDurations (t * testing.T ) {
642
+ var backoffDurations []time.Duration
643
+ var mu sync.Mutex
644
+
645
+ topic := "my_topic"
646
+ maxBackoff := 2 * time .Second
647
+ config := NewTestConfig ()
648
+
649
+ backoffFunc := func (retries , maxRetries int ) time.Duration {
650
+ duration := NewExponentialBackoff (config .Producer .Retry .Backoff , maxBackoff )(retries , maxRetries )
651
+ mu .Lock ()
652
+ backoffDurations = append (backoffDurations , duration )
653
+ mu .Unlock ()
654
+ return duration
655
+ }
656
+
657
+ config .Producer .Flush .MaxMessages = 1
658
+ config .Producer .Return .Successes = true
659
+ config .Producer .Retry .Max = 4
660
+ config .Producer .Retry .BackoffFunc = backoffFunc
661
+
662
+ broker := NewMockBroker (t , 1 )
663
+
664
+ metadataResponse := new (MetadataResponse )
665
+ metadataResponse .AddBroker (broker .Addr (), broker .BrokerID ())
666
+ metadataResponse .AddTopicPartition (topic , 0 , broker .BrokerID (), nil , nil , nil , ErrNoError )
667
+ broker .Returns (metadataResponse )
668
+
669
+ producer , err := NewAsyncProducer ([]string {broker .Addr ()}, config )
670
+ if err != nil {
671
+ t .Fatal (err )
672
+ }
673
+
674
+ failResponse := new (ProduceResponse )
675
+ failResponse .AddTopicPartition (topic , 0 , ErrNotLeaderForPartition )
676
+ successResponse := new (ProduceResponse )
677
+ successResponse .AddTopicPartition (topic , 0 , ErrNoError )
678
+
679
+ broker .Returns (failResponse )
680
+ broker .Returns (metadataResponse )
681
+ broker .Returns (failResponse )
682
+ broker .Returns (metadataResponse )
683
+ broker .Returns (successResponse )
684
+
685
+ producer .Input () <- & ProducerMessage {Topic : topic , Value : StringEncoder ("test" )}
686
+
687
+ expectResults (t , producer , 1 , 0 )
688
+ closeProducer (t , producer )
689
+ broker .Close ()
690
+
691
+ for i := 1 ; i < len (backoffDurations ); i ++ {
692
+ if backoffDurations [i ] < backoffDurations [i - 1 ] {
693
+ t .Errorf ("expected backoff[%d] >= backoff[%d], got %v < %v" , i , i - 1 , backoffDurations [i ], backoffDurations [i - 1 ])
694
+ }
695
+ if backoffDurations [i ] > maxBackoff {
696
+ t .Errorf ("backoff exceeded max: %v" , backoffDurations [i ])
697
+ }
698
+ }
699
+ }
700
+
641
701
// https://github.com/IBM/sarama/issues/2129
642
702
func TestAsyncProducerMultipleRetriesWithConcurrentRequests (t * testing.T ) {
643
703
// Logger = log.New(os.Stdout, "[sarama] ", log.LstdFlags)
0 commit comments