Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Leavrth <jianjun.liao@outlook.com>
  • Loading branch information
Leavrth committed Apr 24, 2023
1 parent bf6af39 commit 093b38e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions br/pkg/storage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ go_library(
"@com_github_google_uuid//:uuid",
"@com_github_klauspost_compress//zstd",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_pingcap_kvproto//pkg/brpb",
"@com_github_pingcap_log//:log",
"@com_github_spf13_pflag//:pflag",
Expand Down Expand Up @@ -80,6 +81,7 @@ go_test(
"@com_github_fsouza_fake_gcs_server//fakestorage",
"@com_github_golang_mock//gomock",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_pingcap_kvproto//pkg/brpb",
"@com_github_stretchr_testify//require",
],
Expand Down
8 changes: 8 additions & 0 deletions br/pkg/storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
backuppb "github.com/pingcap/kvproto/pkg/brpb"
"github.com/pingcap/log"
berrors "github.com/pingcap/tidb/br/pkg/errors"
Expand Down Expand Up @@ -961,6 +962,13 @@ func isConnectionResetError(err error) bool {
}

func (rl retryerWithLog) ShouldRetry(r *request.Request) bool {
// for unit test
failpoint.Inject("replace-error-to-connection-reset-by-peer", func(_ failpoint.Value) {
log.Info("original error", zap.Error(r.Error))
if r.Error != nil {
r.Error = errors.New("read tcp *.*.*.*:*->*.*.*.*:*: read: connection reset by peer")
}
})
if isConnectionResetError(r.Error) {
return true
}
Expand Down
49 changes: 49 additions & 0 deletions br/pkg/storage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/golang/mock/gomock"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
backuppb "github.com/pingcap/kvproto/pkg/brpb"
"github.com/pingcap/tidb/br/pkg/mock"
. "github.com/pingcap/tidb/br/pkg/storage"
Expand Down Expand Up @@ -1292,3 +1294,50 @@ func TestS3StorageBucketRegion(t *testing.T) {
}(ca.name, ca.expectRegion, ca.s3)
}
}

func TestRetryError(t *testing.T) {
var count int32 = 0
var errString string = "read tcp *.*.*.*:*->*.*.*.*:*: read: connection reset by peer"
var lock sync.Mutex
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "PUT" {
var curCnt int32
t.Log(r.URL)
lock.Lock()
count += 1
curCnt = count
lock.Unlock()
if curCnt < 2 {
// write an cannot-retry error, but we modify the error to specific error, so client would retry.
w.WriteHeader(403)
return
}
}

w.WriteHeader(200)

}))

defer server.Close()
t.Log(server.URL)

require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/br/pkg/storage/replace-error-to-connection-reset-by-peer", "return(true)"))
defer func() {
failpoint.Disable("github.com/pingcap/tidb/br/pkg/storage/replace-error-to-connection-reset-by-peer")
}()

ctx := context.Background()
s, err := NewS3Storage(ctx, &backuppb.S3{
Endpoint: server.URL,
Bucket: "test",
Prefix: "retry",
AccessKey: "none",
SecretAccessKey: "none",
Provider: "skip check region",
ForcePathStyle: true,
}, &ExternalStorageOptions{})
require.NoError(t, err)
err = s.WriteFile(ctx, "reset", []byte(errString))
require.NoError(t, err)
require.Equal(t, count, int32(2))
}

0 comments on commit 093b38e

Please sign in to comment.