@@ -529,7 +529,7 @@ func TestTransportPerformRetries(t *testing.T) {
529529 t .Fatalf ("Unexpected error: %s" , err )
530530 }
531531
532- if i != numReqs {
532+ if i != numReqs + 1 {
533533 t .Errorf ("Unexpected number of requests, want=%d, got=%d" , numReqs , i )
534534 }
535535
@@ -575,7 +575,8 @@ func TestTransportPerformRetries(t *testing.T) {
575575 t .Errorf ("Unexpected response: %+v" , res )
576576 }
577577
578- if i != numReqs {
578+ // Should be initial HTTP request + 3 retries
579+ if i != numReqs + 1 {
579580 t .Errorf ("Unexpected number of requests, want=%d, got=%d" , numReqs , i )
580581 }
581582 })
@@ -604,8 +605,8 @@ func TestTransportPerformRetries(t *testing.T) {
604605 }
605606 _ = res
606607
607- if n := len (bodies ); n != 3 {
608- t .Fatalf ("expected 3 requests, got %d" , n )
608+ if n := len (bodies ); n != 4 {
609+ t .Fatalf ("expected 4 requests, got %d" , n )
609610 }
610611 for i , body := range bodies {
611612 if body != "FOOBAR" {
@@ -674,14 +675,15 @@ func TestTransportPerformRetries(t *testing.T) {
674675 t .Run ("Delay the retry with a backoff function" , func (t * testing.T ) {
675676 var (
676677 i int
677- numReqs = 3
678+ numReqs = 4
678679 start = time .Now ()
679- expectedDuration = time .Duration (numReqs * 100 ) * time .Millisecond
680+ expectedDuration = time .Duration (( numReqs - 1 ) * 100 ) * time .Millisecond
680681 )
681682
682683 u , _ := url .Parse ("http://foo.bar" )
683684 tp , _ := New (Config {
684- URLs : []* url.URL {u , u , u },
685+ MaxRetries : numReqs ,
686+ URLs : []* url.URL {u , u , u },
685687 Transport : & mockTransp {
686688 RoundTripFunc : func (req * http.Request ) (* http.Response , error ) {
687689 i ++
@@ -796,3 +798,73 @@ func TestMetaHeader(t *testing.T) {
796798 }
797799 })
798800}
801+
802+ func TestMaxRetries (t * testing.T ) {
803+ tests := []struct {
804+ name string
805+ maxRetries int
806+ disableRetry bool
807+ expectedCallCount int
808+ }{
809+ {
810+ name : "MaxRetries Active set to default" ,
811+ disableRetry : false ,
812+ expectedCallCount : 4 ,
813+ },
814+ {
815+ name : "MaxRetries Active set to 1" ,
816+ maxRetries : 1 ,
817+ disableRetry : false ,
818+ expectedCallCount : 2 ,
819+ },
820+ {
821+ name : "Max Retries Active set to 2" ,
822+ maxRetries : 2 ,
823+ disableRetry : false ,
824+ expectedCallCount : 3 ,
825+ },
826+ {
827+ name : "Max Retries Active set to 3" ,
828+ maxRetries : 3 ,
829+ disableRetry : false ,
830+ expectedCallCount : 4 ,
831+ },
832+ {
833+ name : "MaxRetries Inactive set to 0" ,
834+ maxRetries : 0 ,
835+ disableRetry : true ,
836+ expectedCallCount : 1 ,
837+ },
838+ {
839+ name : "MaxRetries Inactive set to 3" ,
840+ maxRetries : 3 ,
841+ disableRetry : true ,
842+ expectedCallCount : 1 ,
843+ },
844+ }
845+ for _ , test := range tests {
846+ t .Run (test .name , func (t * testing.T ) {
847+ var callCount int
848+ c , _ := New (Config {
849+ URLs : []* url.URL {{}},
850+ Transport : & mockTransp {
851+ RoundTripFunc : func (req * http.Request ) (* http.Response , error ) {
852+ callCount ++
853+ return & http.Response {
854+ StatusCode : http .StatusBadGateway ,
855+ Status : "MOCK" ,
856+ }, nil
857+ },
858+ },
859+ MaxRetries : test .maxRetries ,
860+ DisableRetry : test .disableRetry ,
861+ })
862+
863+ c .Perform (& http.Request {URL : & url.URL {}, Header : make (http.Header )}) // errcheck ignore
864+
865+ if test .expectedCallCount != callCount {
866+ t .Errorf ("Bad retry call count, got : %d, want : %d" , callCount , test .expectedCallCount )
867+ }
868+ })
869+ }
870+ }
0 commit comments