@@ -22,6 +22,7 @@ package estransport
2222import (
2323 "bytes"
2424 "compress/gzip"
25+ "context"
2526 "fmt"
2627 "io"
2728 "io/ioutil"
@@ -785,6 +786,37 @@ func TestTransportPerformRetries(t *testing.T) {
785786 t .Errorf ("Unexpected duration, want=>%s, got=%s" , expectedDuration , end )
786787 }
787788 })
789+
790+ t .Run ("Delay the retry with retry on timeout and context deadline" , func (t * testing.T ) {
791+ var i int
792+ u , _ := url .Parse ("http://foo.bar" )
793+ tp , _ := New (Config {
794+ EnableRetryOnTimeout : true ,
795+ MaxRetries : 100 ,
796+ RetryBackoff : func (i int ) time.Duration { return time .Hour },
797+ URLs : []* url.URL {u },
798+ Transport : & mockTransp {
799+ RoundTripFunc : func (req * http.Request ) (* http.Response , error ) {
800+ i ++
801+ <- req .Context ().Done ()
802+ return nil , req .Context ().Err ()
803+ },
804+ },
805+ })
806+
807+ req , _ := http .NewRequest ("GET" , "/abc" , nil )
808+ ctx , cancel := context .WithTimeout (req .Context (), 50 * time .Millisecond )
809+ defer cancel ()
810+ req = req .WithContext (ctx )
811+
812+ _ , err := tp .Perform (req )
813+ if err != context .DeadlineExceeded {
814+ t .Fatalf ("expected context.DeadlineExceeded, got %s" , err )
815+ }
816+ if i != 1 {
817+ t .Fatalf ("unexpected number of requests: expected 1, got got %d" , i )
818+ }
819+ })
788820}
789821
790822func TestURLs (t * testing.T ) {
@@ -937,19 +969,19 @@ func TestCompatibilityHeader(t *testing.T) {
937969 {
938970 name : "Compatibility header disabled" ,
939971 compatibilityHeader : false ,
940- bodyPresent : false ,
972+ bodyPresent : false ,
941973 expectsHeader : []string {"application/json" },
942974 },
943975 {
944976 name : "Compatibility header enabled" ,
945977 compatibilityHeader : true ,
946- bodyPresent : false ,
978+ bodyPresent : false ,
947979 expectsHeader : []string {"application/vnd.elasticsearch+json;compatible-with=7" },
948980 },
949981 {
950- name : "Compatibility header enabled with body" ,
982+ name : "Compatibility header enabled with body" ,
951983 compatibilityHeader : true ,
952- bodyPresent : true ,
984+ bodyPresent : true ,
953985 expectsHeader : []string {"application/vnd.elasticsearch+json;compatible-with=7" },
954986 },
955987 }
0 commit comments