Skip to content

Commit

Permalink
br: fix unstable unit test (pingcap#31317)
Browse files Browse the repository at this point in the history
  • Loading branch information
3pointer authored Jan 6, 2022
1 parent e053cbb commit b00c2a9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
26 changes: 6 additions & 20 deletions br/pkg/restore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ import (
"google.golang.org/grpc/keepalive"
)

var mc *mock.Cluster

var defaultKeepaliveCfg = keepalive.ClientParameters{
Time: 3 * time.Second,
Timeout: 10 * time.Second,
}

func TestCreateTables(t *testing.T) {
m, err := mock.NewCluster()
require.NoError(t, err)
require.NoError(t, m.Start())
defer m.Stop()

m := mc
client, err := restore.NewRestoreClient(gluetidb.New(), m.PDClient, m.Storage, nil, defaultKeepaliveCfg)
require.NoError(t, err)

Expand Down Expand Up @@ -89,11 +87,7 @@ func TestCreateTables(t *testing.T) {
}

func TestIsOnline(t *testing.T) {
m, err := mock.NewCluster()
require.NoError(t, err)
require.NoError(t, m.Start())
defer m.Stop()

m := mc
client, err := restore.NewRestoreClient(gluetidb.New(), m.PDClient, m.Storage, nil, defaultKeepaliveCfg)
require.NoError(t, err)

Expand All @@ -103,11 +97,7 @@ func TestIsOnline(t *testing.T) {
}

func TestPreCheckTableClusterIndex(t *testing.T) {
m, err := mock.NewCluster()
require.NoError(t, err)
require.NoError(t, m.Start())
defer m.Stop()

m := mc
client, err := restore.NewRestoreClient(gluetidb.New(), m.PDClient, m.Storage, nil, defaultKeepaliveCfg)
require.NoError(t, err)

Expand Down Expand Up @@ -178,11 +168,7 @@ func (fpdc fakePDClient) GetAllStores(context.Context, ...pd.GetStoreOption) ([]
}

func TestPreCheckTableTiFlashReplicas(t *testing.T) {
m, err := mock.NewCluster()
require.NoError(t, err)
require.NoError(t, m.Start())
defer m.Stop()

m := mc
mockStores := []*metapb.Store{
{
Id: 1,
Expand Down
25 changes: 23 additions & 2 deletions br/pkg/restore/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package restore
package restore_test

import (
"fmt"
"os"
"testing"

"github.com/pingcap/tidb/br/pkg/mock"
"github.com/pingcap/tidb/util/testbridge"
"go.uber.org/goleak"
)
Expand All @@ -28,5 +31,23 @@ func TestMain(m *testing.M) {
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}
goleak.VerifyTestMain(m, opts...)

var err error
mc, err = mock.NewCluster()
if err != nil {
panic(err)
}
err = mc.Start()
if err != nil {
panic(err)
}
exitCode := m.Run()
mc.Stop()
if exitCode == 0 {
if err := goleak.Find(opts...); err != nil {
fmt.Fprintf(os.Stderr, "goleak: Errors on successful test run: %v\n", err)
exitCode = 1
}
}
os.Exit(exitCode)
}

0 comments on commit b00c2a9

Please sign in to comment.