Skip to content

Commit

Permalink
errno: migrate test-infra to testify (pingcap#26082)
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun authored Jul 10, 2021
1 parent 372c529 commit 98b2c88
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 31 deletions.
55 changes: 24 additions & 31 deletions errno/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@ package errno
import (
"testing"

. "github.com/pingcap/check"
"github.com/stretchr/testify/assert"
)

func TestT(t *testing.T) {
TestingT(t)
}

var _ = Suite(&testErrno{})

type testErrno struct{}

func (s *testErrno) TestCopySafety(c *C) {
func TestCopySafety(t *testing.T) {
t.Parallel()

IncrementError(123, "user", "host")
IncrementError(321, "user2", "host2")
Expand All @@ -48,42 +41,42 @@ func (s *testErrno) TestCopySafety(c *C) {
IncrementWarning(333, "c", "d")

// global stats
c.Assert(stats.global[123].ErrorCount, Equals, 3)
c.Assert(globalCopy[123].ErrorCount, Equals, 1)
assert.Equal(t, 3, stats.global[123].ErrorCount)
assert.Equal(t, 1, globalCopy[123].ErrorCount)

// user stats
c.Assert(len(stats.users), Equals, 6)
c.Assert(len(userCopy), Equals, 3)
c.Assert(stats.users["user"][123].ErrorCount, Equals, 2)
c.Assert(stats.users["user"][123].WarningCount, Equals, 2)
c.Assert(userCopy["user"][123].ErrorCount, Equals, 1)
c.Assert(userCopy["user"][123].WarningCount, Equals, 1)
assert.Len(t, stats.users, 6)
assert.Len(t, userCopy, 3)
assert.Equal(t, 2, stats.users["user"][123].ErrorCount)
assert.Equal(t, 2, stats.users["user"][123].WarningCount)
assert.Equal(t, 1, userCopy["user"][123].ErrorCount)
assert.Equal(t, 1, userCopy["user"][123].WarningCount)

// ensure there is no user3 in userCopy
_, ok := userCopy["user3"]
c.Assert(ok, IsFalse)
assert.False(t, ok)
_, ok = stats.users["user3"]
c.Assert(ok, IsTrue)
assert.True(t, ok)
_, ok = userCopy["a"]
c.Assert(ok, IsFalse)
assert.False(t, ok)
_, ok = stats.users["a"]
c.Assert(ok, IsTrue)
assert.True(t, ok)

// host stats
c.Assert(len(stats.hosts), Equals, 5)
c.Assert(len(hostCopy), Equals, 3)
assert.Len(t, stats.hosts, 5)
assert.Len(t, hostCopy, 3)

IncrementError(123, "user3", "newhost")
c.Assert(len(stats.hosts), Equals, 6)
c.Assert(len(hostCopy), Equals, 3)
assert.Len(t, stats.hosts, 6)
assert.Len(t, hostCopy, 3)

// ensure there is no newhost in hostCopy
_, ok = hostCopy["newhost"]
c.Assert(ok, IsFalse)
assert.False(t, ok)
_, ok = stats.hosts["newhost"]
c.Assert(ok, IsTrue)
assert.True(t, ok)
_, ok = hostCopy["b"]
c.Assert(ok, IsFalse)
assert.False(t, ok)
_, ok = stats.hosts["b"]
c.Assert(ok, IsTrue)

assert.True(t, ok)
}
26 changes: 26 additions & 0 deletions errno/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 errno

import (
"os"
"testing"

"github.com/pingcap/tidb/util/testbridge"
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
os.Exit(m.Run())
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect
github.com/shirou/gopsutil v3.21.2+incompatible
github.com/soheilhy/cmux v0.1.4
github.com/stretchr/testify v1.7.0
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2
github.com/tikv/client-go/v2 v2.0.0-alpha.0.20210709052506-aadf3cf62721
github.com/tikv/pd v1.1.0-beta.0.20210323121136-78679e5e209d
Expand Down
34 changes: 34 additions & 0 deletions util/testbridge/bridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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.

// +build !codes

package testbridge

import (
"flag"
)

// WorkaroundGoCheckFlags registers flags of go-check for pkg does not import go-check
// to workaround the go-check flags passed in Makefile.
//
// TODO: Remove this function when the migration from go-check to testify[1] is done.
// [1] https://github.com/pingcap/tidb/issues/26022
func WorkaroundGoCheckFlags() {
if flag.Lookup("check.timeout") == nil {
_ = flag.Duration("check.timeout", 0, "WorkaroundGoCheckFlags: check.timeout")
}
if flag.Lookup("check.p") == nil {
_ = flag.Bool("check.p", false, "WorkaroundGoCheckFlags: check.p")
}
}

0 comments on commit 98b2c88

Please sign in to comment.