Skip to content

Commit

Permalink
owner: migrate test-infra to testify (#26770)
Browse files Browse the repository at this point in the history
  • Loading branch information
hu00yan committed Aug 9, 2021
1 parent 7f5c0fa commit 558ffd9
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 158 deletions.
80 changes: 30 additions & 50 deletions owner/fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// 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.
// +build !windows

package owner

Expand All @@ -20,114 +19,95 @@ import (
"math"
"net"
"os"
"runtime"
"sync"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/testleak"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.etcd.io/etcd/clientv3"
"google.golang.org/grpc"
)

// Ignore this test on the windows platform, because calling unix socket with address in
// host:port format fails on windows.
func TestT(t *testing.T) {
CustomVerboseFlag = true
logLevel := os.Getenv("log_level")
err := logutil.InitLogger(logutil.NewLogConfig(logLevel, "", "", logutil.EmptyFileLogConfig, false))
if err != nil {
t.Fatal(err)
}
TestingT(t)
}

var _ = Suite(&testSuite{})

type testSuite struct {
}

func (s *testSuite) SetUpSuite(c *C) {
}

func (s *testSuite) TearDownSuite(c *C) {
}

var (
dialTimeout = 5 * time.Second
dialTimeout = 3 * time.Second
retryCnt = math.MaxInt32
)

func (s *testSuite) TestFailNewSession(c *C) {
os.Remove("new_session:0")
func TestFailNewSession(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("integration.NewClusterV3 will create file contains a colon which is not allowed on Windows")
}

_ = os.Remove("new_session:0")
ln, err := net.Listen("unix", "new_session:0")
c.Assert(err, IsNil)
require.NoError(t, err)

addr := ln.Addr()
endpoints := []string{fmt.Sprintf("%s://%s", addr.Network(), addr.String())}
c.Assert(err, IsNil)
require.NoError(t, err)

srv := grpc.NewServer(grpc.ConnectionTimeout(time.Minute))
var stop sync.WaitGroup
stop.Add(1)

go func() {
if err = srv.Serve(ln); err != nil {
c.Errorf("can't serve gRPC requests %v", err)
}
stop.Done()
defer stop.Done()
err = srv.Serve(ln)
assert.NoError(t, err)
}()

leakFunc := testleak.AfterTest(c)
defer func() {
srv.Stop()
stop.Wait()
leakFunc()
}()

func() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
})
c.Assert(err, IsNil)
require.NoError(t, err)
defer func() {
if cli != nil {
cli.Close()
_ = cli.Close()
}
c.Assert(failpoint.Disable("github.com/pingcap/tidb/owner/closeClient"), IsNil)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/owner/closeClient"))
}()
c.Assert(failpoint.Enable("github.com/pingcap/tidb/owner/closeClient", `return(true)`), IsNil)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/owner/closeClient", `return(true)`))

// TODO: It takes more than 2s here in etcd client, the CI takes 5s to run this test.
// The config is hard coded, not way to control it outside.
// Call stack:
// https://github.com/etcd-io/etcd/blob/ae9734e/clientv3/concurrency/session.go#L38
// https://github.com/etcd-io/etcd/blob/ae9734ed278b7a1a7dfc82e800471ebbf9fce56f/clientv3/client.go#L253
// https://github.com/etcd-io/etcd/blob/ae9734ed278b7a1a7dfc82e800471ebbf9fce56f/clientv3/retry_interceptor.go#L63
_, err = NewSession(context.Background(), "fail_new_serssion", cli, retryCnt, ManagerSessionTTL)
_, err = NewSession(context.Background(), "fail_new_session", cli, retryCnt, ManagerSessionTTL)
isContextDone := terror.ErrorEqual(grpc.ErrClientConnClosing, err) || terror.ErrorEqual(context.Canceled, err)
c.Assert(isContextDone, IsTrue, Commentf("err %v", err))
require.Truef(t, isContextDone, "err %v", err)
}()

func() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
})
c.Assert(err, IsNil)
require.NoError(t, err)
defer func() {
if cli != nil {
cli.Close()
_ = cli.Close()
}
c.Assert(failpoint.Disable("github.com/pingcap/tidb/owner/closeGrpc"), IsNil)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/owner/closeGrpc"))
}()
c.Assert(failpoint.Enable("github.com/pingcap/tidb/owner/closeGrpc", `return(true)`), IsNil)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/owner/closeGrpc", `return(true)`))

// TODO: It takes more than 2s here in etcd client, the CI takes 5s to run this test.
// The config is hard coded, not way to control it outside.
_, err = NewSession(context.Background(), "fail_new_serssion", cli, retryCnt, ManagerSessionTTL)
_, err = NewSession(context.Background(), "fail_new_session", cli, retryCnt, ManagerSessionTTL)
isContextDone := terror.ErrorEqual(grpc.ErrClientConnClosing, err) || terror.ErrorEqual(context.Canceled, err)
c.Assert(isContextDone, IsTrue, Commentf("err %v", err))
require.Truef(t, isContextDone, "err %v", err)
}()
}
29 changes: 29 additions & 0 deletions owner/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//Copyright 2021 PingCAP, Inc.
//
// 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 owner

import (
"testing"

"github.com/pingcap/tidb/util/testbridge"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
opts := []goleak.Option{
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
}
goleak.VerifyTestMain(m, opts...)
}
Loading

0 comments on commit 558ffd9

Please sign in to comment.