Skip to content

Commit

Permalink
executor: migrate test-infra to testify for slow_query_test.go (pingc…
Browse files Browse the repository at this point in the history
  • Loading branch information
mashenjun authored Jan 25, 2022
1 parent 6b85084 commit 8fb8319
Showing 1 changed file with 77 additions and 79 deletions.
156 changes: 77 additions & 79 deletions executor/slow_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"bufio"
"bytes"
"context"
"fmt"
"os"
"strings"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser/model"
Expand All @@ -35,7 +35,7 @@ import (
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func parseLog(retriever *slowQueryRetriever, sctx sessionctx.Context, reader *bufio.Reader, logNum int) ([][]types.Datum, error) {
Expand Down Expand Up @@ -77,7 +77,7 @@ func parseSlowLog(sctx sessionctx.Context, reader *bufio.Reader, logNum int) ([]
return rows, err
}

func (s *testExecSerialSuite) TestParseSlowLogPanic(c *C) {
func TestParseSlowLogPanic(t *testing.T) {
slowLogStr :=
`# Time: 2019-04-28T15:24:04.309074+08:00
# Txn_start_ts: 405888132465033227
Expand All @@ -98,21 +98,21 @@ func (s *testExecSerialSuite) TestParseSlowLogPanic(c *C) {
# Prev_stmt: update t set i = 1;
use test;
select * from t;`
c.Assert(failpoint.Enable("github.com/pingcap/tidb/executor/errorMockParseSlowLogPanic", `return(true)`), IsNil)
require.Nil(t, failpoint.Enable("github.com/pingcap/tidb/executor/errorMockParseSlowLogPanic", `return(true)`))
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/executor/errorMockParseSlowLogPanic"), IsNil)
require.Nil(t, failpoint.Disable("github.com/pingcap/tidb/executor/errorMockParseSlowLogPanic"))
}()
reader := bufio.NewReader(bytes.NewBufferString(slowLogStr))
loc, err := time.LoadLocation("Asia/Shanghai")
c.Assert(err, IsNil)
require.Nil(t, err)
sctx := mock.NewContext()
sctx.GetSessionVars().TimeZone = loc
_, err = parseSlowLog(sctx, reader, 64)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "panic test")
require.Error(t, err)
require.Equal(t, err.Error(), "panic test")
}

func (s *testExecSuite) TestParseSlowLogFile(c *C) {
func TestParseSlowLogFile(t *testing.T) {
slowLogStr :=
`# Time: 2019-04-28T15:24:04.309074+08:00
# Txn_start_ts: 405888132465033227
Expand Down Expand Up @@ -141,16 +141,16 @@ use test;
select * from t;`
reader := bufio.NewReader(bytes.NewBufferString(slowLogStr))
loc, err := time.LoadLocation("Asia/Shanghai")
c.Assert(err, IsNil)
require.Nil(t, err)
ctx := mock.NewContext()
ctx.GetSessionVars().TimeZone = loc
rows, err := parseSlowLog(ctx, reader, 64)
c.Assert(err, IsNil)
c.Assert(len(rows), Equals, 1)
require.Nil(t, err)
require.Len(t, rows, 1)
recordString := ""
for i, value := range rows[0] {
str, err := value.ToString()
c.Assert(err, IsNil)
require.Nil(t, err)
if i > 0 {
recordString += ","
}
Expand All @@ -163,17 +163,17 @@ select * from t;`
`Cop_backoff_regionMiss_total_times: 200 Cop_backoff_regionMiss_total_time: 0.2 Cop_backoff_regionMiss_max_time: 0.2 Cop_backoff_regionMiss_max_addr: 127.0.0.1 Cop_backoff_regionMiss_avg_time: 0.2 Cop_backoff_regionMiss_p90_time: 0.2 Cop_backoff_rpcPD_total_times: 200 Cop_backoff_rpcPD_total_time: 0.2 Cop_backoff_rpcPD_max_time: 0.2 Cop_backoff_rpcPD_max_addr: 127.0.0.1 Cop_backoff_rpcPD_avg_time: 0.2 Cop_backoff_rpcPD_p90_time: 0.2 Cop_backoff_rpcTiKV_total_times: 200 Cop_backoff_rpcTiKV_total_time: 0.2 Cop_backoff_rpcTiKV_max_time: 0.2 Cop_backoff_rpcTiKV_max_addr: 127.0.0.1 Cop_backoff_rpcTiKV_avg_time: 0.2 Cop_backoff_rpcTiKV_p90_time: 0.2,` +
`0,0,1,0,1,1,,60e9378c746d9a2be1c791047e008967cf252eb6de9167ad3aa6098fa2d523f4,` +
`update t set i = 1;,select * from t;`
c.Assert(expectRecordString, Equals, recordString)
require.Equal(t, expectRecordString, recordString)

// Issue 20928
reader = bufio.NewReader(bytes.NewBufferString(slowLogStr))
rows, err = parseSlowLog(ctx, reader, 1)
c.Assert(err, IsNil)
c.Assert(len(rows), Equals, 1)
require.Nil(t, err)
require.Len(t, rows, 1)
recordString = ""
for i, value := range rows[0] {
str, err := value.ToString()
c.Assert(err, IsNil)
require.Nil(t, err)
if i > 0 {
recordString += ","
}
Expand All @@ -186,7 +186,7 @@ select * from t;`
`Cop_backoff_regionMiss_total_times: 200 Cop_backoff_regionMiss_total_time: 0.2 Cop_backoff_regionMiss_max_time: 0.2 Cop_backoff_regionMiss_max_addr: 127.0.0.1 Cop_backoff_regionMiss_avg_time: 0.2 Cop_backoff_regionMiss_p90_time: 0.2 Cop_backoff_rpcPD_total_times: 200 Cop_backoff_rpcPD_total_time: 0.2 Cop_backoff_rpcPD_max_time: 0.2 Cop_backoff_rpcPD_max_addr: 127.0.0.1 Cop_backoff_rpcPD_avg_time: 0.2 Cop_backoff_rpcPD_p90_time: 0.2 Cop_backoff_rpcTiKV_total_times: 200 Cop_backoff_rpcTiKV_total_time: 0.2 Cop_backoff_rpcTiKV_max_time: 0.2 Cop_backoff_rpcTiKV_max_addr: 127.0.0.1 Cop_backoff_rpcTiKV_avg_time: 0.2 Cop_backoff_rpcTiKV_p90_time: 0.2,` +
`0,0,1,0,1,1,,60e9378c746d9a2be1c791047e008967cf252eb6de9167ad3aa6098fa2d523f4,` +
`update t set i = 1;,select * from t;`
c.Assert(expectRecordString, Equals, recordString)
require.Equal(t, expectRecordString, recordString)

// fix sql contain '# ' bug
slowLog := bytes.NewBufferString(
Expand All @@ -204,7 +204,7 @@ select * from t;
`)
reader = bufio.NewReader(slowLog)
_, err = parseSlowLog(ctx, reader, 64)
c.Assert(err, IsNil)
require.Nil(t, err)

// test for time format compatibility.
slowLog = bytes.NewBufferString(
Expand All @@ -215,14 +215,14 @@ select * from t;
`)
reader = bufio.NewReader(slowLog)
rows, err = parseSlowLog(ctx, reader, 64)
c.Assert(err, IsNil)
c.Assert(len(rows) == 2, IsTrue)
require.Nil(t, err)
require.Len(t, rows, 2)
t0Str, err := rows[0][0].ToString()
c.Assert(err, IsNil)
c.Assert(t0Str, Equals, "2019-04-28 15:24:04.309074")
require.Nil(t, err)
require.Equal(t, t0Str, "2019-04-28 15:24:04.309074")
t1Str, err := rows[1][0].ToString()
c.Assert(err, IsNil)
c.Assert(t1Str, Equals, "2019-04-24 19:41:21.716221")
require.Nil(t, err)
require.Equal(t, t1Str, "2019-04-24 19:41:21.716221")

// Add parse error check.
slowLog = bytes.NewBufferString(
Expand All @@ -232,16 +232,16 @@ select * from t;
`)
reader = bufio.NewReader(slowLog)
_, err = parseSlowLog(ctx, reader, 64)
c.Assert(err, IsNil)
require.Nil(t, err)
warnings := ctx.GetSessionVars().StmtCtx.GetWarnings()
c.Assert(warnings, HasLen, 1)
c.Assert(warnings[0].Err.Error(), Equals, "Parse slow log at line 2, failed field is Succ, failed value is abc, error is strconv.ParseBool: parsing \"abc\": invalid syntax")
require.Len(t, warnings, 1)
require.Equal(t, warnings[0].Err.Error(), "Parse slow log at line 2, failed field is Succ, failed value is abc, error is strconv.ParseBool: parsing \"abc\": invalid syntax")
}

// It changes variable.MaxOfMaxAllowedPacket, so must be stayed in SerialSuite.
func (s *testExecSerialSuite) TestParseSlowLogFileSerial(c *C) {
func TestParseSlowLogFileSerial(t *testing.T) {
loc, err := time.LoadLocation("Asia/Shanghai")
c.Assert(err, IsNil)
require.Nil(t, err)
ctx := mock.NewContext()
ctx.GetSessionVars().TimeZone = loc
// test for bufio.Scanner: token too long.
Expand All @@ -256,33 +256,33 @@ select * from t;
slowLog.WriteString(sql)
reader := bufio.NewReader(slowLog)
_, err = parseSlowLog(ctx, reader, 64)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "single line length exceeds limit: 65536")
require.Error(t, err)
require.Equal(t, err.Error(), "single line length exceeds limit: 65536")

variable.MaxOfMaxAllowedPacket = originValue
reader = bufio.NewReader(slowLog)
_, err = parseSlowLog(ctx, reader, 64)
c.Assert(err, IsNil)
require.Nil(t, err)
}

func (s *testExecSuite) TestSlowLogParseTime(c *C) {
func TestSlowLogParseTime(t *testing.T) {
t1Str := "2019-01-24T22:32:29.313255+08:00"
t2Str := "2019-01-24T22:32:29.313255"
t1, err := ParseTime(t1Str)
c.Assert(err, IsNil)
require.Nil(t, err)
loc, err := time.LoadLocation("Asia/Shanghai")
c.Assert(err, IsNil)
require.Nil(t, err)
t2, err := time.ParseInLocation("2006-01-02T15:04:05.999999999", t2Str, loc)
c.Assert(err, IsNil)
c.Assert(t1.Unix(), Equals, t2.Unix())
require.Nil(t, err)
require.Equal(t, t1.Unix(), t2.Unix())
t1Format := t1.In(loc).Format(logutil.SlowLogTimeFormat)
c.Assert(t1Format, Equals, t1Str)
require.Equal(t, t1Format, t1Str)
}

// TestFixParseSlowLogFile bugfix
// sql select * from INFORMATION_SCHEMA.SLOW_QUERY limit 1;
// ERROR 1105 (HY000): string "2019-05-12-11:23:29.61474688" doesn't has a prefix that matches format "2006-01-02-15:04:05.999999999 -0700", err: parsing time "2019-05-12-11:23:29.61474688" as "2006-01-02-15:04:05.999999999 -0700": cannot parse "" as "-0700"
func (s *testExecSuite) TestFixParseSlowLogFile(c *C) {
func TestFixParseSlowLogFile(t *testing.T) {
slowLog := bytes.NewBufferString(
`# Time: 2019-05-12-11:23:29.614327491 +0800
# Txn_start_ts: 405888132465033227
Expand All @@ -309,11 +309,11 @@ select * from t
select * from t;`)
scanner := bufio.NewReader(slowLog)
loc, err := time.LoadLocation("Asia/Shanghai")
c.Assert(err, IsNil)
require.Nil(t, err)
ctx := mock.NewContext()
ctx.GetSessionVars().TimeZone = loc
_, err = parseSlowLog(ctx, scanner, 64)
c.Assert(err, IsNil)
require.Nil(t, err)

// Test parser error.
slowLog = bytes.NewBufferString(
Expand All @@ -323,14 +323,13 @@ select * from t;
`)
scanner = bufio.NewReader(slowLog)
_, err = parseSlowLog(ctx, scanner, 64)
c.Assert(err, IsNil)
require.Nil(t, err)
warnings := ctx.GetSessionVars().StmtCtx.GetWarnings()
c.Assert(warnings, HasLen, 1)
c.Assert(warnings[0].Err.Error(), Equals, "Parse slow log at line 2, failed field is Txn_start_ts, failed value is 405888132465033227#, error is strconv.ParseUint: parsing \"405888132465033227#\": invalid syntax")

require.Len(t, warnings, 1)
require.Equal(t, warnings[0].Err.Error(), "Parse slow log at line 2, failed field is Txn_start_ts, failed value is 405888132465033227#, error is strconv.ParseUint: parsing \"405888132465033227#\": invalid syntax")
}

func (s *testExecSuite) TestSlowQueryRetriever(c *C) {
func TestSlowQueryRetriever(t *testing.T) {
logData0 := ""
logData1 := `
# Time: 2020-02-15T18:00:01.000000+08:00
Expand All @@ -356,7 +355,7 @@ select 7;`
fileName2 := "tidb-slow-2020-02-16T19-04-05.01.log"
fileName3 := "tidb-slow.log"
fileNames := []string{fileName0, fileName1, fileName2, fileName3}
prepareLogs(c, logData, fileNames)
prepareLogs(t, logData, fileNames)
defer func() {
removeFiles(fileNames)
}()
Expand Down Expand Up @@ -447,41 +446,41 @@ select 7;`
}

loc, err := time.LoadLocation("Asia/Shanghai")
c.Assert(err, IsNil)
require.Nil(t, err)
sctx := mock.NewContext()
sctx.GetSessionVars().TimeZone = loc
sctx.GetSessionVars().SlowQueryFile = fileName3
for i, cas := range cases {
extractor := &plannercore.SlowQueryExtractor{Enable: len(cas.startTime) > 0 && len(cas.endTime) > 0}
if extractor.Enable {
startTime, err := ParseTime(cas.startTime)
c.Assert(err, IsNil)
require.Nil(t, err)
endTime, err := ParseTime(cas.endTime)
c.Assert(err, IsNil)
require.Nil(t, err)
extractor.TimeRanges = []*plannercore.TimeRange{{StartTime: startTime, EndTime: endTime}}
}
retriever, err := newSlowQueryRetriever()
c.Assert(err, IsNil)
require.Nil(t, err)
retriever.extractor = extractor
err = retriever.initialize(context.Background(), sctx)
c.Assert(err, IsNil)
comment := Commentf("case id: %v", i)
c.Assert(retriever.files, HasLen, len(cas.files), comment)
require.Nil(t, err)
comment := fmt.Sprintf("case id: %v", i)
require.Equal(t, len(retriever.files), len(cas.files), comment)
if len(retriever.files) > 0 {
reader := bufio.NewReader(retriever.files[0].file)
rows, err := parseLog(retriever, sctx, reader, 64)
c.Assert(err, IsNil)
c.Assert(len(rows), Equals, len(cas.querys), comment)
require.Nil(t, err)
require.Equal(t, len(rows), len(cas.querys), comment)
for i, row := range rows {
c.Assert(row[len(row)-1].GetString(), Equals, cas.querys[i], comment)
require.Equal(t, row[len(row)-1].GetString(), cas.querys[i], comment)
}
}

for i, file := range retriever.files {
c.Assert(file.file.Name(), Equals, cas.files[i])
c.Assert(file.file.Close(), IsNil)
require.Equal(t, file.file.Name(), cas.files[i])
require.Nil(t, file.file.Close())
}
c.Assert(retriever.close(), IsNil)
require.Nil(t, retriever.close())
}
}

Expand Down Expand Up @@ -525,12 +524,12 @@ func TestSplitbyColon(t *testing.T) {
}
for _, c := range cases {
resFields, resValues := splitByColon(c.line)
assert.Equal(t, c.fields, resFields)
assert.Equal(t, c.values, resValues)
require.Equal(t, c.fields, resFields)
require.Equal(t, c.values, resValues)
}
}

func (s *testExecSuite) TestBatchLogForReversedScan(c *C) {
func TestBatchLogForReversedScan(t *testing.T) {
logData0 := ""
logData1 := `
# Time: 2020-02-15T18:00:01.000000+08:00
Expand Down Expand Up @@ -560,7 +559,7 @@ select 9;`
fileName3 := "tidb-slow-2020-02-17T19-04-05.01.log"
fileName4 := "tidb-slow.log"
fileNames := []string{fileName0, fileName1, fileName2, fileName3, fileName4}
prepareLogs(c, logData, fileNames)
prepareLogs(t, logData, fileNames)
defer func() {
removeFiles(fileNames)
}()
Expand Down Expand Up @@ -617,49 +616,48 @@ select 9;`
}

loc, err := time.LoadLocation("Asia/Shanghai")
c.Assert(err, IsNil)
require.Nil(t, err)
sctx := mock.NewContext()
sctx.GetSessionVars().TimeZone = loc
sctx.GetSessionVars().SlowQueryFile = fileName3
for i, cas := range cases {
extractor := &plannercore.SlowQueryExtractor{Enable: len(cas.startTime) > 0 && len(cas.endTime) > 0, Desc: true}
if extractor.Enable {
startTime, err := ParseTime(cas.startTime)
c.Assert(err, IsNil)
require.Nil(t, err)
endTime, err := ParseTime(cas.endTime)
c.Assert(err, IsNil)
require.Nil(t, err)
extractor.TimeRanges = []*plannercore.TimeRange{{StartTime: startTime, EndTime: endTime}}
}
retriever, err := newSlowQueryRetriever()
c.Assert(err, IsNil)
require.Nil(t, err)
retriever.extractor = extractor
sctx.GetSessionVars().SlowQueryFile = fileName4
err = retriever.initialize(context.Background(), sctx)
c.Assert(err, IsNil)
comment := Commentf("case id: %v", i)
c.Assert(retriever.files, HasLen, len(cas.files), comment)
require.Nil(t, err)
comment := fmt.Sprintf("case id: %v", i)
if len(retriever.files) > 0 {
reader := bufio.NewReader(retriever.files[0].file)
offset := &offset{length: 0, offset: 0}
rows, err := retriever.getBatchLogForReversedScan(context.Background(), reader, offset, 3)
c.Assert(err, IsNil)
require.Nil(t, err)
for _, row := range rows {
for j, log := range row {
c.Assert(log, Equals, cas.logs[0][j], comment)
require.Equal(t, log, cas.logs[0][j], comment)
}
}
}
c.Assert(retriever.close(), IsNil)
require.Nil(t, retriever.close())
}
}

func prepareLogs(c *C, logData []string, fileNames []string) {
func prepareLogs(t *testing.T, logData []string, fileNames []string) {
writeFile := func(file string, data string) {
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
c.Assert(err, IsNil)
require.Nil(t, err)
_, err = f.Write([]byte(data))
c.Assert(f.Close(), IsNil)
c.Assert(err, IsNil)
require.Nil(t, f.Close())
require.Nil(t, err)
}

for i, log := range logData {
Expand Down

0 comments on commit 8fb8319

Please sign in to comment.