diff --git a/client/main_test.go b/client/main_test.go index e0aa3a97..d65b5887 100644 --- a/client/main_test.go +++ b/client/main_test.go @@ -16,9 +16,11 @@ package client import ( "testing" + "github.com/tikv/client-go/v2/util" "go.uber.org/goleak" ) func TestMain(m *testing.M) { + util.EnableFailpoints() goleak.VerifyTestMain(m) } diff --git a/config/main_test.go b/config/main_test.go index 9a46a346..b0172d89 100644 --- a/config/main_test.go +++ b/config/main_test.go @@ -16,9 +16,11 @@ package config import ( "testing" + "github.com/tikv/client-go/v2/util" "go.uber.org/goleak" ) func TestMain(m *testing.M) { + util.EnableFailpoints() goleak.VerifyTestMain(m) } diff --git a/integration_tests/main_test.go b/integration_tests/main_test.go new file mode 100644 index 00000000..cff64e96 --- /dev/null +++ b/integration_tests/main_test.go @@ -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() +} diff --git a/tikv/failpoint_export.go b/tikv/failpoint_export.go new file mode 100644 index 00000000..9ef5f56c --- /dev/null +++ b/tikv/failpoint_export.go @@ -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 +) diff --git a/util/failpoint.go b/util/failpoint.go index 3e3a7e54..1ad0b914 100644 --- a/util/failpoint.go +++ b/util/failpoint.go @@ -33,13 +33,29 @@ package util import ( + "errors" + "github.com/pingcap/failpoint" ) 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` // to make it possible to be used in a library. func EvalFailpoint(name string) (interface{}, error) { + if !failpointsEnabled { + return nil, errDisabled + } return failpoint.Eval(failpointPrefix + name) }