Skip to content

Commit

Permalink
Refine the tests
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato committed Jun 18, 2021
1 parent 07f12de commit f77861d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions kv/txn_scope_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (t TxnScopeVar) GetTxnScope() string {
return t.txnScope
}

// NewTxnScopeVar is used to create a new TxnScopeVar.
func NewTxnScopeVar(varValue string, txnScope string) TxnScopeVar {
return TxnScopeVar{
varValue: varValue,
Expand Down
39 changes: 34 additions & 5 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3345,8 +3345,8 @@ func (s *testSessionSuite2) TestPerStmtTaskID(c *C) {
}

func (s *testSessionSerialSuite) TestSetTxnScope(c *C) {
// With EnableLocalTxn is true.
c.Assert(failpoint.Enable("tikvclient/injectEnableLocalTxn", `return(true)`), IsNil)
defer failpoint.Disable("tikvclient/injectEnableLocalTxn")
failpoint.Enable("tikvclient/injectTxnScope", `return("")`)
tk := testkit.NewTestKitWithInit(c, s.store)
// assert default value
Expand All @@ -3358,7 +3358,6 @@ func (s *testSessionSerialSuite) TestSetTxnScope(c *C) {
result = tk.MustQuery("select @@txn_scope;")
result.Check(testkit.Rows(kv.GlobalTxnScope))
c.Assert(tk.Se.GetSessionVars().CheckAndGetTxnScope(), Equals, kv.GlobalTxnScope)
failpoint.Disable("tikvclient/injectTxnScope")
failpoint.Enable("tikvclient/injectTxnScope", `return("bj")`)
defer failpoint.Disable("tikvclient/injectTxnScope")
tk = testkit.NewTestKitWithInit(c, s.store)
Expand All @@ -3371,11 +3370,41 @@ func (s *testSessionSerialSuite) TestSetTxnScope(c *C) {
result = tk.MustQuery("select @@txn_scope;")
result.Check(testkit.Rows(kv.GlobalTxnScope))
c.Assert(tk.Se.GetSessionVars().CheckAndGetTxnScope(), Equals, kv.GlobalTxnScope)

// assert set invalid txn_scope
err := tk.ExecToErr("set @@txn_scope='foo'")
c.Assert(err, NotNil)
c.Assert(err.Error(), Matches, `.*txn_scope value should be global or local.*`)

// With EnableLocalTxn is false.
c.Assert(failpoint.Enable("tikvclient/injectEnableLocalTxn", `return(false)`), IsNil)
tk = testkit.NewTestKitWithInit(c, s.store)
// assert default value
result = tk.MustQuery("select @@txn_scope;")
result.Check(testkit.Rows(kv.GlobalTxnScope))
c.Assert(tk.Se.GetSessionVars().CheckAndGetTxnScope(), Equals, kv.GlobalTxnScope)
// assert set sys variable
tk.MustExec("set @@session.txn_scope = 'local';")
result = tk.MustQuery("select @@txn_scope;")
result.Check(testkit.Rows(kv.GlobalTxnScope))
c.Assert(tk.Se.GetSessionVars().CheckAndGetTxnScope(), Equals, kv.GlobalTxnScope)
failpoint.Enable("tikvclient/injectTxnScope", `return("bj")`)
tk = testkit.NewTestKitWithInit(c, s.store)
// assert default value
result = tk.MustQuery("select @@txn_scope;")
result.Check(testkit.Rows(kv.GlobalTxnScope))
c.Assert(tk.Se.GetSessionVars().CheckAndGetTxnScope(), Equals, kv.GlobalTxnScope)
// assert set sys variable
tk.MustExec("set @@session.txn_scope = 'global';")
result = tk.MustQuery("select @@txn_scope;")
result.Check(testkit.Rows(kv.GlobalTxnScope))
c.Assert(tk.Se.GetSessionVars().CheckAndGetTxnScope(), Equals, kv.GlobalTxnScope)
// assert set invalid txn_scope
err = tk.ExecToErr("set @@txn_scope='foo'")
c.Assert(err, NotNil)
c.Assert(err.Error(), Matches, `.*txn_scope value should be global or local.*`)

failpoint.Disable("tikvclient/injectTxnScope")
failpoint.Disable("tikvclient/injectEnableLocalTxn")
}

func (s *testSessionSerialSuite) TestGlobalAndLocalTxn(c *C) {
Expand All @@ -3384,6 +3413,8 @@ func (s *testSessionSerialSuite) TestGlobalAndLocalTxn(c *C) {
if *tikvmockstore.WithTiKV {
return
}
c.Assert(failpoint.Enable("tikvclient/injectEnableLocalTxn", `return(true)`), IsNil)
defer failpoint.Disable("tikvclient/injectEnableLocalTxn")
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t1;")
defer tk.MustExec("drop table if exists t1")
Expand Down Expand Up @@ -3468,8 +3499,6 @@ PARTITION BY RANGE (c) (
result = tk.MustQuery("select * from t1") // read dc-1 and dc-2 with global scope
c.Assert(len(result.Rows()), Equals, 3)

c.Assert(failpoint.Enable("tikvclient/injectEnableLocalTxn", `return(true)`), IsNil)
defer failpoint.Disable("tikvclient/injectEnableLocalTxn")
failpoint.Enable("tikvclient/injectTxnScope", `return("dc-1")`)
defer failpoint.Disable("tikvclient/injectTxnScope")
// set txn_scope to local
Expand Down
4 changes: 2 additions & 2 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,9 @@ var defaultSysVars = []*SysVar{
}(), SetSession: func(s *SessionVars, val string) error {
switch val {
case kv.GlobalTxnScope:
fallthrough
s.TxnScope = kv.NewTxnScopeVar(kv.GlobalTxnScope, s.TxnScope.GetTxnScope())
case kv.LocalTxnScope:
s.TxnScope = kv.NewTxnScopeVar(val, s.TxnScope.GetTxnScope())
s.TxnScope = kv.GetTxnScopeVar()
default:
return ErrWrongValueForVar.GenWithStack("@@txn_scope value should be global or local")
}
Expand Down

0 comments on commit f77861d

Please sign in to comment.