@@ -10,9 +10,11 @@ import (
1010 "bytes"
1111 "context"
1212 "errors"
13+ "fmt"
1314 "io"
1415 "math"
1516 "net/http"
17+ "reflect"
1618 "strings"
1719 "testing"
1820 "time"
@@ -962,3 +964,49 @@ func TestCalcDelay(t *testing.T) {
962964 }
963965 })
964966}
967+
968+ type retryLoggingChecker struct {
969+ count int32
970+ }
971+
972+ func (r * retryLoggingChecker ) Do (req * policy.Request ) (* http.Response , error ) {
973+ if r .count > 0 {
974+ var opValues logPolicyOpValues
975+ req .OperationValue (& opValues )
976+ if reflect .ValueOf (opValues ).IsZero () {
977+ // the logging policy is after us. so it should have populated logPolicyOpValues
978+ return nil , errors .New ("unexpected zero-value for logPolicyOpValues" )
979+ }
980+
981+ // verify that the logging policy is updating the try in opValues
982+ if r .count != opValues .try {
983+ return nil , fmt .Errorf ("expected count %d, got %d" , r .count , opValues .try )
984+ }
985+ }
986+ r .count ++
987+ return req .Next ()
988+ }
989+
990+ func TestRetryPolicyWithLoggingChecker (t * testing.T ) {
991+ srv , close := mock .NewServer ()
992+ defer close ()
993+ srv .AppendResponse (mock .WithBodyReadError ())
994+ srv .AppendResponse (mock .WithBodyReadError ())
995+ srv .AppendResponse ()
996+ pl := newTestPipeline (& policy.ClientOptions {
997+ Transport : srv , Retry : * testRetryOptions (),
998+ PerRetryPolicies : []policy.Policy {& retryLoggingChecker {}},
999+ })
1000+ req , err := NewRequest (context .Background (), http .MethodGet , srv .URL ())
1001+ require .NoError (t , err )
1002+
1003+ body := newRewindTrackingBody ("stuff" )
1004+ require .NoError (t , req .SetBody (body , "text/plain" ))
1005+
1006+ resp , err := pl .Do (req )
1007+ require .NoError (t , err )
1008+ require .EqualValues (t , http .StatusOK , resp .StatusCode )
1009+ require .EqualValues (t , 3 , srv .Requests ())
1010+ require .EqualValues (t , 2 , body .rcount )
1011+ require .True (t , body .closed )
1012+ }
0 commit comments