From f77861d4f830c401434549e5ab60bee6b99339c0 Mon Sep 17 00:00:00 2001 From: JmPotato Date: Fri, 18 Jun 2021 17:51:31 +0800 Subject: [PATCH] Refine the tests Signed-off-by: JmPotato --- kv/txn_scope_var.go | 1 + session/session_test.go | 39 ++++++++++++++++++++++++++++++----- sessionctx/variable/sysvar.go | 4 ++-- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/kv/txn_scope_var.go b/kv/txn_scope_var.go index d365a12a3306f..c9c2b62dee578 100644 --- a/kv/txn_scope_var.go +++ b/kv/txn_scope_var.go @@ -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, diff --git a/session/session_test.go b/session/session_test.go index afe42cc353574..a9f9a4699d020 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -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 @@ -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) @@ -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) { @@ -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") @@ -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 diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index ee01ad6d9fb87..ddc1065853c79 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -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") }