util: add flag to control all failpoints (#176)

Signed-off-by: disksing <i@disksing.com>
This commit is contained in:
disksing 2021-06-26 21:46:26 +08:00 committed by GitHub
parent a0410b45bb
commit 28ad1f94e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 0 deletions

View File

@ -16,9 +16,11 @@ package client
import ( import (
"testing" "testing"
"github.com/tikv/client-go/v2/util"
"go.uber.org/goleak" "go.uber.org/goleak"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
util.EnableFailpoints()
goleak.VerifyTestMain(m) goleak.VerifyTestMain(m)
} }

View File

@ -16,9 +16,11 @@ package config
import ( import (
"testing" "testing"
"github.com/tikv/client-go/v2/util"
"go.uber.org/goleak" "go.uber.org/goleak"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
util.EnableFailpoints()
goleak.VerifyTestMain(m) goleak.VerifyTestMain(m)
} }

View File

@ -0,0 +1,25 @@
// Copyright 2021 TiKV Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package tikv_test
import (
"testing"
"github.com/tikv/client-go/v2/tikv"
)
func TestMain(m *testing.M) {
tikv.EnableFailpoints()
m.Run()
}

22
tikv/failpoint_export.go Normal file
View File

@ -0,0 +1,22 @@
// Copyright 2021 TiKV Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package tikv
import "github.com/tikv/client-go/v2/util"
var (
// EnableFailpoints enables use of failpoints.
// It should be called before using client to avoid data race.
EnableFailpoints = util.EnableFailpoints
)

View File

@ -33,13 +33,29 @@
package util package util
import ( import (
"errors"
"github.com/pingcap/failpoint" "github.com/pingcap/failpoint"
) )
const failpointPrefix = "tikvclient/" const failpointPrefix = "tikvclient/"
var (
failpointsEnabled bool
errDisabled = errors.New("failpoints are disabled")
)
// EnableFailpoints enables use of failpoints.
// It should be called before using client to avoid data race.
func EnableFailpoints() {
failpointsEnabled = true
}
// EvalFailpoint injects code for testing. It is used to replace `failpoint.Inject` // EvalFailpoint injects code for testing. It is used to replace `failpoint.Inject`
// to make it possible to be used in a library. // to make it possible to be used in a library.
func EvalFailpoint(name string) (interface{}, error) { func EvalFailpoint(name string) (interface{}, error) {
if !failpointsEnabled {
return nil, errDisabled
}
return failpoint.Eval(failpointPrefix + name) return failpoint.Eval(failpointPrefix + name)
} }