Skip to content

Commit

Permalink
br/pkg/lightning/log: migrate test-infra to testify (#31289)
Browse files Browse the repository at this point in the history
close #28238
  • Loading branch information
tisonkun authored Jan 7, 2022
1 parent 152a00d commit 7a5b715
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 87 deletions.
44 changes: 18 additions & 26 deletions br/pkg/lightning/log/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,60 @@ package log_test
import (
"regexp"
"strings"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var _ = Suite(&testFilterSuite{})

type testFilterSuite struct{}

func (s *testFilterSuite) TestFilter(c *C) {
func TestFilter(t *testing.T) {
logger, buffer := log.MakeTestLogger()
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(
buffer.Stripped(), Equals,
`{"$lvl":"WARN","$msg":"the message","number":123456,"array":[7,8,9]}`,
)

require.Equal(t, `{"$lvl":"WARN","$msg":"the message","number":123456,"array":[7,8,9]}`, buffer.Stripped())

logger, buffer = log.MakeTestLogger(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return log.NewFilterCore(c, "github.com/pingcap/br/")
}), zap.AddCaller())
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(buffer.Stripped(), HasLen, 0)
require.Len(t, buffer.Stripped(), 0)

logger, buffer = log.MakeTestLogger(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return log.NewFilterCore(c, "github.com/pingcap/tidb/br/").With([]zap.Field{zap.String("a", "b")})
}), zap.AddCaller())
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(
buffer.Stripped(), Equals,
`{"$lvl":"WARN","$msg":"the message","a":"b","number":123456,"array":[7,8,9]}`,
)
require.Equal(t, `{"$lvl":"WARN","$msg":"the message","a":"b","number":123456,"array":[7,8,9]}`, buffer.Stripped())

logger, buffer = log.MakeTestLogger(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return log.NewFilterCore(c, "github.com/pingcap/br/").With([]zap.Field{zap.String("a", "b")})
}), zap.AddCaller())
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(buffer.Stripped(), HasLen, 0)
require.Len(t, buffer.Stripped(), 0)

// Fields won't trigger filter.
logger, buffer = log.MakeTestLogger(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return log.NewFilterCore(c, "github.com/pingcap/check/").With([]zap.Field{zap.String("a", "b")})
}), zap.AddCaller())
logger.Warn("the message", zap.String("stack", "github.com/pingcap/tidb/br/"))
c.Assert(buffer.Stripped(), HasLen, 0)
require.Len(t, buffer.Stripped(), 0)
}

// PASS: filter_test.go:82: testFilterSuite.BenchmarkFilterRegexMatchString 1000000 1163 ns/op
// PASS: filter_test.go:64: testFilterSuite.BenchmarkFilterStringsContains 10000000 159 ns/op
// BenchmarkFilterStringsContains-16 16693887 66.68 ns/op
// BenchmarkFilterRegexMatchString-16 2350828 510.6 ns/op
//
// Run `go test github.com/pingcap/tidb/br/pkg/lightning/log -check.b -test.v` to get benchmark result.
func (s *testFilterSuite) BenchmarkFilterStringsContains(c *C) {
c.ResetTimer()
// Run `go test -run='^$' -bench=. -v github.com/pingcap/tidb/br/pkg/lightning/log` to get benchmark result.
func BenchmarkFilterStringsContains(b *testing.B) {
b.ResetTimer()

inputs := []string{
"github.com/pingcap/tidb/some/package/path",
"github.com/tikv/pd/some/package/path",
"github.com/pingcap/tidb/br/some/package/path",
}
filters := []string{"github.com/pingcap/tidb/", "github.com/tikv/pd/"}
for i := 0; i < c.N; i++ {
for i := 0; i < b.N; i++ {
for i := range inputs {
for j := range filters {
_ = strings.Contains(inputs[i], filters[j])
Expand All @@ -75,16 +67,16 @@ func (s *testFilterSuite) BenchmarkFilterStringsContains(c *C) {
}
}

func (s *testFilterSuite) BenchmarkFilterRegexMatchString(c *C) {
c.ResetTimer()
func BenchmarkFilterRegexMatchString(b *testing.B) {
b.ResetTimer()

inputs := []string{
"github.com/pingcap/tidb/some/package/path",
"github.com/tikv/pd/some/package/path",
"github.com/pingcap/tidb/br/some/package/path",
}
filters := regexp.MustCompile(`github.com/(pingcap/tidb|tikv/pd)/`)
for i := 0; i < c.N; i++ {
for i := 0; i < b.N; i++ {
for i := range inputs {
_ = filters.MatchString(inputs[i])
}
Expand Down
43 changes: 0 additions & 43 deletions br/pkg/lightning/log/log_serial_test.go

This file was deleted.

60 changes: 42 additions & 18 deletions br/pkg/lightning/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,61 @@
package log_test

import (
"io"
"os"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

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

type logSuite struct{}

var _ = Suite(&logSuite{})

func (s *logSuite) TestConfigAdjust(c *C) {
func TestConfigAdjust(t *testing.T) {
cfg := &log.Config{}
cfg.Adjust()
c.Assert(cfg.Level, Equals, "info")
require.Equal(t, "info", cfg.Level)

cfg.File = "."
err := log.InitLogger(cfg, "info")
c.Assert(err, ErrorMatches, "can't use directory as log file name")
log.L().Named("xx")
require.EqualError(t, err, "can't use directory as log file name")
}

func (s *logSuite) TestTestLogger(c *C) {
func TestTestLogger(t *testing.T) {
logger, buffer := log.MakeTestLogger()
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(
buffer.Stripped(), Equals,
`{"$lvl":"WARN","$msg":"the message","number":123456,"array":[7,8,9]}`,
)
require.Equal(t, `{"$lvl":"WARN","$msg":"the message","number":123456,"array":[7,8,9]}`, buffer.Stripped())
}

func TestInitStdoutLogger(t *testing.T) {
r, w, err := os.Pipe()
require.NoError(t, err)
oldStdout := os.Stdout
os.Stdout = w

msg := "logger is initialized to stdout"
outputC := make(chan string, 1)
go func() {
buf := make([]byte, 4096)
n := 0
for {
nn, err := r.Read(buf[n:])
if nn == 0 || err == io.EOF {
break
}
require.NoError(t, err)
n += nn
}
outputC <- string(buf[:n])
}()

logCfg := &log.Config{File: "-"}
err = log.InitLogger(logCfg, "info")
require.NoError(t, err)
log.L().Info(msg)

os.Stdout = oldStdout
require.NoError(t, w.Close())
output := <-outputC
require.NoError(t, r.Close())
require.Contains(t, output, msg)
}

0 comments on commit 7a5b715

Please sign in to comment.