Skip to content

Commit 9148566

Browse files
SunRunAwayjackysp
authored andcommitted
executor: fix two data races in tests (#11120)
1 parent e2426c3 commit 9148566

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

config/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ func GetGlobalConfig() *Config {
432432
return globalConf.Load().(*Config)
433433
}
434434

435+
// StoreGlobalConfig stores a new config to the globalConf. It mostly uses in the test to avoid some data races.
436+
func StoreGlobalConfig(config *Config) {
437+
globalConf.Store(config)
438+
}
439+
435440
// ReloadGlobalConfig reloads global configuration for this server.
436441
func ReloadGlobalConfig() error {
437442
confReloadLock.Lock()

executor/executor_test.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,27 @@ func (s *testSuite) TearDownSuite(c *C) {
135135
s.store.Close()
136136
}
137137

138+
func enablePessimisticTxn(enable bool) {
139+
newConf := config.NewConfig()
140+
newConf.PessimisticTxn.Enable = enable
141+
config.StoreGlobalConfig(newConf)
142+
}
143+
144+
func (s *testSuite) TestPessimisticSelectForUpdate(c *C) {
145+
defer func() { enablePessimisticTxn(false) }()
146+
enablePessimisticTxn(true)
147+
tk := testkit.NewTestKit(c, s.store)
148+
tk.MustExec("use test")
149+
tk.MustExec("drop table if exists t")
150+
tk.MustExec("create table t(id int primary key, a int)")
151+
tk.MustExec("insert into t values(1, 1)")
152+
tk.MustExec("begin PESSIMISTIC")
153+
tk.MustQuery("select a from t where id=1 for update").Check(testkit.Rows("1"))
154+
tk.MustExec("update t set a=a+1 where id=1")
155+
tk.MustExec("commit")
156+
tk.MustQuery("select a from t where id=1").Check(testkit.Rows("2"))
157+
}
158+
138159
func (s *testSuite) TearDownTest(c *C) {
139160
tk := testkit.NewTestKit(c, s.store)
140161
tk.MustExec("use test")
@@ -3960,6 +3981,12 @@ func (s *testOOMSuite) TestDistSQLMemoryControl(c *C) {
39603981
tk.Se.GetSessionVars().MemQuotaDistSQL = -1
39613982
}
39623983

3984+
func setOOMAction(action string) {
3985+
newConf := config.NewConfig()
3986+
newConf.OOMAction = action
3987+
config.StoreGlobalConfig(newConf)
3988+
}
3989+
39633990
func (s *testSuite) TestOOMPanicAction(c *C) {
39643991
tk := testkit.NewTestKit(c, s.store)
39653992
tk.MustExec("use test")
@@ -3971,7 +3998,11 @@ func (s *testSuite) TestOOMPanicAction(c *C) {
39713998
}
39723999
tk.Se.SetSessionManager(sm)
39734000
s.domain.ExpensiveQueryHandle().SetSessionManager(sm)
3974-
config.GetGlobalConfig().OOMAction = config.OOMActionCancel
4001+
orgAction := config.GetGlobalConfig().OOMAction
4002+
setOOMAction(config.OOMActionCancel)
4003+
defer func() {
4004+
setOOMAction(orgAction)
4005+
}()
39754006
tk.MustExec("set @@tidb_mem_quota_query=1;")
39764007
err := tk.QueryToErr("select sum(b) from t group by a;")
39774008
c.Assert(err, NotNil)

0 commit comments

Comments
 (0)