Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
Signed-off-by: joccau <zak.zhao@pingcap.com>
  • Loading branch information
joccau committed Dec 8, 2022
1 parent 4bbacec commit 80e2e20
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion br/pkg/task/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ func restoreStream(
}
defer func() {
if err = restoreGc(); err != nil {
log.Info("failed to set gc enabled", zap.Error(err))
log.Error("failed to set gc enabled", zap.Error(err))
}
}()

Expand Down
6 changes: 3 additions & 3 deletions br/pkg/utils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func IsLogBackupEnabled(ctx sqlexec.RestrictedSQLExecutor) (bool, error) {
}

func GetGcRatio(ctx sqlexec.RestrictedSQLExecutor) (string, error) {
valStr := "show config where name = 'log-backup.enable' and type = 'tikv'"
valStr := "show config where name = 'gc.ratio-threshold' and type = 'tikv'"
rows, fields, errSQL := ctx.ExecRestrictedSQL(
kv.WithInternalSourceType(context.Background(), kv.InternalTxnBR),
nil,
Expand All @@ -126,9 +126,9 @@ func SetGcRatio(ctx sqlexec.RestrictedSQLExecutor, ratio string) error {
ratio,
)
if err != nil {
return errors.Trace(err)
return errors.Annotatef(err, "failed to set config `gc.ratio-threshold`=%s", ratio)
}
log.Info("set config tikv gc.ratio-threshold", zap.String("ratio", ratio))
log.Warn("set config tikv gc.ratio-threshold", zap.String("ratio", ratio))
return nil
}

Expand Down
29 changes: 24 additions & 5 deletions br/pkg/utils/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package utils_test

import (
"context"
"strings"
"testing"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -35,7 +36,19 @@ func (m *mockRestrictedSQLExecutor) ExecRestrictedSQL(ctx context.Context, opts
if m.errHappen {
return nil, nil, errors.New("injected error")
}
return m.rows, m.fields, nil

if strings.Contains(sql, "show config") {
return m.rows, m.fields, nil
} else if strings.Contains(sql, "set config") && strings.Contains(sql, "gc.ratio-threshold") {
value := args[0].(string)

for _, r := range m.rows {
d := types.Datum{}
d.SetString(value, "")
chunk.MutRow(r).SetDatum(3, d)
}
}
return nil, nil, nil
}

func TestIsLogBackupEnabled(t *testing.T) {
Expand Down Expand Up @@ -130,7 +143,7 @@ func TestGc(t *testing.T) {
types.NewFieldType(mysql.TypeString),
types.NewFieldType(mysql.TypeString),
types.NewFieldType(mysql.TypeString),
types.NewFieldType(mysql.TypeFloat),
types.NewFieldType(mysql.TypeString),
}
for i := 0; i < len(tps); i++ {
rf := new(ast.ResultField)
Expand All @@ -139,13 +152,19 @@ func TestGc(t *testing.T) {
fields[i] = rf
}
rows := make([]chunk.Row, 0, 2)
row := chunk.MutRowFromValues("tikv", " 127.0.0.1:20161", "log-backup.enable", 1.1).ToRow()
row := chunk.MutRowFromValues("tikv", " 127.0.0.1:20161", "log-backup.enable", "1.1").ToRow()
rows = append(rows, row)
row = chunk.MutRowFromValues("tikv", " 127.0.0.1:20162", "log-backup.enable", 1.1).ToRow()
row = chunk.MutRowFromValues("tikv", " 127.0.0.1:20162", "log-backup.enable", "1.1").ToRow()
rows = append(rows, row)

s := &mockRestrictedSQLExecutor{rows: rows, fields: fields}
ratio, err := utils.GetGcRatio(s)
require.Nil(t, err)
require.Equal(t, ratio, 1.1)
require.Equal(t, ratio, "1.1")

err = utils.SetGcRatio(s, "-1.0")
require.Nil(t, err)
ratio, err = utils.GetGcRatio(s)
require.Nil(t, err)
require.Equal(t, ratio, "-1.0")
}

0 comments on commit 80e2e20

Please sign in to comment.