Skip to content

Commit

Permalink
*: move the functions that only used in tests (pingcap#31929)
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta authored Jan 24, 2022
1 parent 2d6386d commit 203ab22
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 73 deletions.
File renamed without changes.
7 changes: 3 additions & 4 deletions dumpling/export/sql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
var colTypeRowReceiverMap = map[string]func() RowReceiverStringer{}

var (
nullValue = "NULL"
quotationMark = []byte{'\''}
twoQuotationMarks = []byte{'\'', '\''}
doubleQuotationMark = []byte{'"'}
nullValue = "NULL"
quotationMark = []byte{'\''}
twoQuotationMarks = []byte{'\'', '\''}
)

// There are two kinds of scenes to use this dataType
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions dumpling/export/writer_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestWriteInsertInCsv(t *testing.T) {
bf := storage.NewBufferWriter()

// test nullValue
opt := &csvOption{separator: []byte(","), delimiter: doubleQuotationMark, nullValue: "\\N"}
opt := &csvOption{separator: []byte(","), delimiter: []byte{'"'}, nullValue: "\\N"}
conf := configForWriteCSV(cfg, true, opt)
n, err := WriteInsertInCsv(tcontext.Background(), conf, tableIR, tableIR, bf)
require.Equal(t, uint64(4), n)
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestWriteInsertInCsvReturnsError(t *testing.T) {
bf := storage.NewBufferWriter()

// test nullValue
opt := &csvOption{separator: []byte(","), delimiter: doubleQuotationMark, nullValue: "\\N"}
opt := &csvOption{separator: []byte(","), delimiter: []byte{'"'}, nullValue: "\\N"}
conf := configForWriteCSV(cfg, true, opt)
n, err := WriteInsertInCsv(tcontext.Background(), conf, tableIR, tableIR, bf)
require.Equal(t, uint64(3), n)
Expand Down
1 change: 1 addition & 0 deletions executor/hash_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (c *hashRowContainer) matchJoinKey(buildRow, probeRow chunk.Row, probeHCtx
}

// alreadySpilledSafeForTest indicates that records have spilled out into disk. It's thread-safe.
// nolint: unused
func (c *hashRowContainer) alreadySpilledSafeForTest() bool {
return c.rowContainer.AlreadySpilledSafeForTest()
}
Expand Down
5 changes: 5 additions & 0 deletions plugin/conn_ip_example/conn_ip_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ var connection int32

// Validate implements TiDB plugin's Validate SPI.
// It is called before OnInit
// nolint: unused, deadcode
func Validate(ctx context.Context, m *plugin.Manifest) error {
fmt.Println("## conn_ip_example Validate called ##")
fmt.Printf("---- context: %s\n", ctx)
return nil
}

// OnInit implements TiDB plugin's OnInit SPI.
// nolint: unused, deadcode
func OnInit(ctx context.Context, manifest *plugin.Manifest) error {
fmt.Println("## conn_ip_example OnInit called ##")
fmt.Printf("---- context: %s\n", ctx)
Expand Down Expand Up @@ -78,6 +80,7 @@ func OnInit(ctx context.Context, manifest *plugin.Manifest) error {
}

// OnShutdown implements TiDB plugin's OnShutdown SPI.
// nolint: unused, deadcode
func OnShutdown(ctx context.Context, manifest *plugin.Manifest) error {
fmt.Println("## conn_ip_example OnShutdown called ##")
fmt.Printf("---- context: %s\n", ctx)
Expand All @@ -87,6 +90,7 @@ func OnShutdown(ctx context.Context, manifest *plugin.Manifest) error {
}

// OnGeneralEvent implements TiDB Audit plugin's OnGeneralEvent SPI.
// nolint: unused, deadcode
func OnGeneralEvent(ctx context.Context, sctx *variable.SessionVars, event plugin.GeneralEvent, cmd string) {
fmt.Println("## conn_ip_example OnGeneralEvent called ##")
if sctx != nil {
Expand All @@ -112,6 +116,7 @@ func OnGeneralEvent(ctx context.Context, sctx *variable.SessionVars, event plugi
}

// OnConnectionEvent implements TiDB Audit plugin's OnConnectionEvent SPI.
// nolint: unused, deadcode
func OnConnectionEvent(ctx context.Context, event plugin.ConnectionEvent, info *variable.ConnectionInfo) error {
var reason string
if r := ctx.Value(plugin.RejectReasonCtxValue{}); r != nil {
Expand Down
1 change: 1 addition & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type plugins struct {
}

// clone deep copies plugins info.
// nolint: unused
func (p *plugins) clone() *plugins {
np := &plugins{
plugins: make(map[Kind][]Plugin, len(p.plugins)),
Expand Down
14 changes: 0 additions & 14 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1648,20 +1648,6 @@ func (s *SessionVars) GetReadableTxnMode() string {
return txnMode
}

func (s *SessionVars) setTxnMode(val string) error {
switch strings.ToUpper(val) {
case ast.Pessimistic:
s.TxnMode = ast.Pessimistic
case ast.Optimistic:
s.TxnMode = ast.Optimistic
case "":
s.TxnMode = ""
default:
return ErrWrongValueForVar.FastGenByArgs(TiDBTxnMode, val)
}
return nil
}

// SetPrevStmtDigest sets the digest of the previous statement.
func (s *SessionVars) SetPrevStmtDigest(prevStmtDigest string) {
s.prevStmtDigest = prevStmtDigest
Expand Down
14 changes: 0 additions & 14 deletions sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,6 @@ func TestSysVar(t *testing.T) {
require.Equal(t, runtime.GOARCH, f.Value)
}

func TestTxnMode(t *testing.T) {
seVar := NewSessionVars()
require.NotNil(t, seVar)
require.Equal(t, "", seVar.TxnMode)
err := seVar.setTxnMode("pessimistic")
require.NoError(t, err)
err = seVar.setTxnMode("optimistic")
require.NoError(t, err)
err = seVar.setTxnMode("")
require.NoError(t, err)
err = seVar.setTxnMode("something else")
require.Error(t, err)
}

func TestError(t *testing.T) {
kvErrs := []*terror.Error{
ErrUnsupportedValueForVar,
Expand Down
11 changes: 0 additions & 11 deletions statistics/fmsketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,6 @@ func (s *FMSketch) InsertRowValue(sc *stmtctx.StatementContext, values []types.D
return nil
}

func buildFMSketch(sc *stmtctx.StatementContext, values []types.Datum, maxSize int) (*FMSketch, int64, error) {
s := NewFMSketch(maxSize)
for _, value := range values {
err := s.InsertValue(sc, value)
if err != nil {
return nil, 0, errors.Trace(err)
}
}
return s, s.NDV(), nil
}

// MergeFMSketch merges two FM Sketch.
func (s *FMSketch) MergeFMSketch(rs *FMSketch) {
if s == nil || rs == nil {
Expand Down
12 changes: 12 additions & 0 deletions statistics/fmsketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
Expand All @@ -33,6 +34,17 @@ func extractSampleItemsDatums(items []*SampleItem) []types.Datum {
return datums
}

func buildFMSketch(sc *stmtctx.StatementContext, values []types.Datum, maxSize int) (*FMSketch, int64, error) {
s := NewFMSketch(maxSize)
for _, value := range values {
err := s.InsertValue(sc, value)
if err != nil {
return nil, 0, errors.Trace(err)
}
}
return s, s.NDV(), nil
}

func SubTestSketch() func(*testing.T) {
return func(t *testing.T) {
s := createTestStatisticsSamples(t)
Expand Down
2 changes: 2 additions & 0 deletions util/benchdaily/bench_daily.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func Run(tests ...func(b *testing.B)) {
writeBenchResultToFile(res, *outfile)
}

// readBenchResultFromFile is used by the daily bench test.
// nolint: unused, deadcode
func readBenchResultFromFile(file string) []BenchResult {
f, err := os.Open(file)
if err != nil {
Expand Down
28 changes: 0 additions & 28 deletions util/chunk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,34 +99,6 @@ func (l *ListInDisk) GetDiskTracker() *disk.Tracker {
return l.diskTracker
}

// flush empties the write buffer, please call flush before read!
func (l *ListInDisk) flush() (err error) {
// buffered is not zero only after Add and before GetRow, after the first flush, buffered will always be zero,
// hence we use a RWLock to allow quicker quit.
l.bufFlushMutex.RLock()
checksumWriter := l.w
l.bufFlushMutex.RUnlock()
if checksumWriter == nil {
return nil
}
l.bufFlushMutex.Lock()
defer l.bufFlushMutex.Unlock()
if l.w != nil {
err = l.w.Close()
if err != nil {
return
}
l.w = nil
// the l.disk is the underlying object of the l.w, it will be closed
// after calling l.w.Close, we need to reopen it before reading rows.
l.disk, err = os.Open(l.disk.Name())
if err != nil {
return errors2.Trace(err)
}
}
return
}

// Add adds a chunk to the ListInDisk. Caller must make sure the input chk
// is not empty and not used any more and has the same field types.
// Warning: do not mix Add and GetRow (always use GetRow after you have added all the chunks), and do not use Add concurrently.
Expand Down
28 changes: 28 additions & 0 deletions util/chunk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"

"github.com/cznic/mathutil"
errors2 "github.com/pingcap/errors"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -172,6 +173,33 @@ func (l *listInDiskWriteDisk) GetRow(ptr RowPtr) (row Row, err error) {
return row, err
}

func (l *listInDiskWriteDisk) flush() (err error) {
// buffered is not zero only after Add and before GetRow, after the first flush, buffered will always be zero,
// hence we use a RWLock to allow quicker quit.
l.bufFlushMutex.RLock()
checksumWriter := l.w
l.bufFlushMutex.RUnlock()
if checksumWriter == nil {
return nil
}
l.bufFlushMutex.Lock()
defer l.bufFlushMutex.Unlock()
if l.w != nil {
err = l.w.Close()
if err != nil {
return
}
l.w = nil
// the l.disk is the underlying object of the l.w, it will be closed
// after calling l.w.Close, we need to reopen it before reading rows.
l.disk, err = os.Open(l.disk.Name())
if err != nil {
return errors2.Trace(err)
}
}
return
}

func checkRow(t *testing.T, row1, row2 Row) {
require.Equal(t, row2.GetString(0), row1.GetString(0))
require.Equal(t, row2.GetInt64(1), row1.GetInt64(1))
Expand Down
2 changes: 2 additions & 0 deletions util/selection/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func introselect(data Interface, left, right, k int, depth int) int {
}
}

// quickselect is used in test for comparison.
// nolint: unused
func quickselect(data Interface, left, right, k int) int {
if left == right {
return left
Expand Down
1 change: 1 addition & 0 deletions util/stmtsummary/statement_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ func (ssMap *stmtSummaryByDigestMap) SetMaxStmtCount(value uint) error {
}

// Used by tests
// nolint: unused
func (ssMap *stmtSummaryByDigestMap) maxStmtCount() int {
return int(ssMap.optMaxStmtCount.Load())
}
Expand Down

0 comments on commit 203ab22

Please sign in to comment.