Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
morgo committed Mar 24, 2022
1 parent e24da50 commit 9025154
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 36 deletions.
6 changes: 3 additions & 3 deletions executor/infoschema_cluster_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (s *infosSchemaClusterTableSuite) TestTableStorageStats() {
"test 2",
))
rows := tk.MustQuery("select TABLE_NAME from information_schema.TABLE_STORAGE_STATS where TABLE_SCHEMA = 'mysql';").Rows()
s.Require().Len(rows, 30)
s.Require().Len(rows, 31)

// More tests about the privileges.
tk.MustExec("create user 'testuser'@'localhost'")
Expand All @@ -345,12 +345,12 @@ func (s *infosSchemaClusterTableSuite) TestTableStorageStats() {
Hostname: "localhost",
}, nil, nil))

tk.MustQuery("select count(1) from information_schema.TABLE_STORAGE_STATS where TABLE_SCHEMA = 'mysql'").Check(testkit.Rows("30"))
tk.MustQuery("select count(1) from information_schema.TABLE_STORAGE_STATS where TABLE_SCHEMA = 'mysql'").Check(testkit.Rows("31"))

s.Require().True(tk.Session().Auth(&auth.UserIdentity{
Username: "testuser3",
Hostname: "localhost",
}, nil, nil))

tk.MustQuery("select count(1) from information_schema.TABLE_STORAGE_STATS where TABLE_SCHEMA = 'mysql'").Check(testkit.Rows("30"))
tk.MustQuery("select count(1) from information_schema.TABLE_STORAGE_STATS where TABLE_SCHEMA = 'mysql'").Check(testkit.Rows("31"))
}
14 changes: 1 addition & 13 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1292,28 +1292,16 @@ func TestEnableNoopFunctionsVar(t *testing.T) {
tk.MustQuery(`select @@global.tidb_enable_noop_functions;`).Check(testkit.Rows("OFF"))
tk.MustQuery(`select @@tidb_enable_noop_functions;`).Check(testkit.Rows("OFF"))

err := tk.ExecToErr(`select get_lock('lock1', 2);`)
require.True(t, terror.ErrorEqual(err, expression.ErrFunctionsNoopImpl), fmt.Sprintf("err %v", err))
err = tk.ExecToErr(`select release_lock('lock1');`)
require.True(t, terror.ErrorEqual(err, expression.ErrFunctionsNoopImpl), fmt.Sprintf("err %v", err))

// change session var to 1
tk.MustExec(`set tidb_enable_noop_functions=1;`)
tk.MustQuery(`select @@tidb_enable_noop_functions;`).Check(testkit.Rows("ON"))
tk.MustQuery(`select @@global.tidb_enable_noop_functions;`).Check(testkit.Rows("OFF"))
tk.MustQuery(`select get_lock("lock", 10)`).Check(testkit.Rows("1"))
tk.MustQuery(`select release_lock("lock")`).Check(testkit.Rows("1"))

// restore to 0
tk.MustExec(`set tidb_enable_noop_functions=0;`)
tk.MustQuery(`select @@tidb_enable_noop_functions;`).Check(testkit.Rows("OFF"))
tk.MustQuery(`select @@global.tidb_enable_noop_functions;`).Check(testkit.Rows("OFF"))

err = tk.ExecToErr(`select get_lock('lock2', 10);`)
require.True(t, terror.ErrorEqual(err, expression.ErrFunctionsNoopImpl), fmt.Sprintf("err %v", err))
err = tk.ExecToErr(`select release_lock('lock2');`)
require.True(t, terror.ErrorEqual(err, expression.ErrFunctionsNoopImpl), fmt.Sprintf("err %v", err))

// set test
require.Error(t, tk.ExecToErr(`set tidb_enable_noop_functions='abc'`))
require.Error(t, tk.ExecToErr(`set tidb_enable_noop_functions=11`))
Expand All @@ -1324,7 +1312,7 @@ func TestEnableNoopFunctionsVar(t *testing.T) {
tk.MustExec(`set tidb_enable_noop_functions=0;`)
tk.MustQuery(`select @@tidb_enable_noop_functions;`).Check(testkit.Rows("OFF"))

err = tk.ExecToErr("SET SESSION tx_read_only = 1")
err := tk.ExecToErr("SET SESSION tx_read_only = 1")
require.True(t, terror.ErrorEqual(err, variable.ErrFunctionsNoopImpl), fmt.Sprintf("err %v", err))

tk.MustExec("SET SESSION tx_read_only = 0")
Expand Down
19 changes: 9 additions & 10 deletions expression/builtin_miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,15 @@ func (b *builtinLockSig) evalInt(row chunk.Row) (int64, bool, error) {
if err != nil {
return 0, isNull, err
}
sessVars := b.ctx.GetSessionVars()
// TODO: timeout can be -1 as well?
if isNull || isNullTimeout || lockName == "" || timeout < 0 {
// for insert ignore stmt, the StrictSQLMode and ignoreErr should both be considered.
if !sessVars.StmtCtx.BadNullAsWarning {
return 0, false, errIncorrectArgs.GenWithStackByArgs("get_lock")
}
err := errIncorrectArgs.GenWithStackByArgs("get_lock")
sessVars.StmtCtx.AppendWarning(err)
return 0, false, nil
// Validate that neither argument is NULL and there is a lockName
if isNull || isNullTimeout || lockName == "" {
return 0, false, errIncorrectArgs.GenWithStackByArgs("get_lock")
}
// A timeout less than zero is expected to be treated as unlimited.
// Because of our implementation being based on pessimitic locks,
// We set the timeout to the maximum value of innodb_lock_wait_timeout.
if timeout < 0 {
timeout = 1073741824
}
err = b.ctx.GetAdvisoryLock(lockName, timeout)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions expression/builtin_miscellaneous_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"time"

"github.com/google/uuid"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/vitess"
Expand Down Expand Up @@ -240,7 +239,7 @@ func (b *builtinLockSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) er
for i := range i64s {
i64s[i] = 1
}
return errors.New("GET_LOCK does not support vectorized evaluation yet")
return nil
}

func (b *builtinDurationAnyValueSig) vectorized() bool {
Expand Down Expand Up @@ -644,7 +643,7 @@ func (b *builtinReleaseLockSig) vecEvalInt(input *chunk.Chunk, result *chunk.Col
for i := range i64s {
i64s[i] = 1
}
return errors.New("release lock does not support vectorization yet")
return nil
}

func (b *builtinVitessHashSig) vectorized() bool {
Expand Down
4 changes: 2 additions & 2 deletions expression/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ func TestIsNullFunc(t *testing.T) {
func TestLock(t *testing.T) {
ctx := createContext(t)
lock := funcs[ast.GetLock]
f, err := lock.getFunction(ctx, datumsToConstants(types.MakeDatums(nil, 1)))
f, err := lock.getFunction(ctx, datumsToConstants(types.MakeDatums("mylock", 1)))
require.NoError(t, err)
v, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
require.Equal(t, int64(1), v.GetInt64())

releaseLock := funcs[ast.ReleaseLock]
f, err = releaseLock.getFunction(ctx, datumsToConstants(types.MakeDatums(1)))
f, err = releaseLock.getFunction(ctx, datumsToConstants(types.MakeDatums("mylock")))
require.NoError(t, err)
v, err = evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
Expand Down
2 changes: 0 additions & 2 deletions expression/integration_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4183,8 +4183,6 @@ func TestNoopFunctions(t *testing.T) {
"SELECT * FROM t1 LOCK IN SHARE MODE",
"SELECT * FROM t1 GROUP BY a DESC",
"SELECT * FROM t1 GROUP BY a ASC",
"SELECT GET_LOCK('acdc', 10)",
"SELECT RELEASE_LOCK('acdc')",
}

for _, stmt := range stmts {
Expand Down
3 changes: 2 additions & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ func TestMiscellaneousBuiltin(t *testing.T) {
tk.MustQuery("select a,any_value(b),sum(c) from t1 group by a order by a;").Check(testkit.Rows("1 10 0", "2 30 0"))

// for locks
tk.MustExec(`set tidb_enable_noop_functions=1;`)
result := tk.MustQuery(`SELECT GET_LOCK('test_lock1', 10);`)
result.Check(testkit.Rows("1"))
result = tk.MustQuery(`SELECT GET_LOCK('test_lock2', 10);`)
Expand All @@ -330,6 +329,8 @@ func TestMiscellaneousBuiltin(t *testing.T) {
result.Check(testkit.Rows("1"))
result = tk.MustQuery(`SELECT RELEASE_LOCK('test_lock1');`)
result.Check(testkit.Rows("1"))
result = tk.MustQuery(`SELECT RELEASE_LOCK('test_lock3');`) // not acquired
result.Check(testkit.Rows("0"))
}

func TestConvertToBit(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ const (
KEY table_create_time (table_id, create_time)
);`

CreateAdvisoryLocks = `CREATE TABLE mysql.advisory_locks (
// CreateAdvisoryLocks stores the advisory locks (get_lock, release_lock).
CreateAdvisoryLocks = `CREATE TABLE IF NOT EXISTS mysql.advisory_locks (
lock_name VARCHAR(255) NOT NULL PRIMARY KEY
);`
)
Expand Down
4 changes: 3 additions & 1 deletion sessionctx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ type Context interface {
GetStmtStats() *stmtstats.StatementStats
// ShowProcess returns ProcessInfo running in current Context
ShowProcess() *util.ProcessInfo

// GetAdvisoryLock acquires an advisory lock (aka GET_LOCK()).
GetAdvisoryLock(string, int64) error
// ReleaseAdvisoryLock releases an advisory lock (aka RELEASE_LOCK()).
ReleaseAdvisoryLock(string) bool
// ReleaseAllAdvisoryLocks releases all advisory locks that this session holds.
ReleaseAllAdvisoryLocks() int
}

Expand Down
3 changes: 3 additions & 0 deletions util/mock/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,17 @@ func (c *Context) GetStmtStats() *stmtstats.StatementStats {
return nil
}

// GetAdvisoryLock acquires an advisory lock
func (c *Context) GetAdvisoryLock(lockName string, timeout int64) error {
return nil
}

// ReleaseAdvisoryLock releases an advisory lock
func (c *Context) ReleaseAdvisoryLock(lockName string) bool {
return true
}

// ReleaseAllAdvisoryLocks releases all advisory locks
func (c *Context) ReleaseAllAdvisoryLocks() int {
return 0
}
Expand Down

0 comments on commit 9025154

Please sign in to comment.