variable: add tests for setter/getter funcs (#26634)

This commit is contained in:
Morgan Tocker
2021-07-27 00:58:15 -06:00
committed by GitHub
parent 8642902337
commit 01ac21c194
2 changed files with 30 additions and 6 deletions

View File

@ -800,7 +800,7 @@ var defaultSysVars = []*SysVar{
{Scope: ScopeGlobal, Name: InitConnect, Value: ""},
/* TiDB specific variables */
{Scope: ScopeGlobal, Name: TiDBEnableLocalTxn, Value: BoolToOnOff(DefTiDBEnableLocalTxn), Hidden: true, Type: TypeBool, GetSession: func(sv *SessionVars) (string, error) {
{Scope: ScopeGlobal, Name: TiDBEnableLocalTxn, Value: BoolToOnOff(DefTiDBEnableLocalTxn), Hidden: true, Type: TypeBool, GetGlobal: func(sv *SessionVars) (string, error) {
return BoolToOnOff(EnableLocalTxn.Load()), nil
}, SetGlobal: func(s *SessionVars, val string) error {
oldVal := EnableLocalTxn.Load()
@ -1692,7 +1692,7 @@ var defaultSysVars = []*SysVar{
return nil
}},
// variable for top SQL feature.
{Scope: ScopeGlobal, Name: TiDBEnableTopSQL, Value: BoolToOnOff(DefTiDBTopSQLEnable), Type: TypeBool, Hidden: true, AllowEmpty: true, GetSession: func(s *SessionVars) (string, error) {
{Scope: ScopeGlobal, Name: TiDBEnableTopSQL, Value: BoolToOnOff(DefTiDBTopSQLEnable), Type: TypeBool, Hidden: true, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) {
return BoolToOnOff(TopSQLVariable.Enable.Load()), nil
}, SetGlobal: func(vars *SessionVars, s string) error {
TopSQLVariable.Enable.Store(TiDBOptOn(s))
@ -1705,7 +1705,7 @@ var defaultSysVars = []*SysVar{
TopSQLVariable.AgentAddress.Store(s)
return nil
}},
{Scope: ScopeGlobal, Name: TiDBTopSQLPrecisionSeconds, Value: strconv.Itoa(DefTiDBTopSQLPrecisionSeconds), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: math.MaxInt64, AllowEmpty: true, GetSession: func(s *SessionVars) (string, error) {
{Scope: ScopeGlobal, Name: TiDBTopSQLPrecisionSeconds, Value: strconv.Itoa(DefTiDBTopSQLPrecisionSeconds), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: math.MaxInt64, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) {
return strconv.FormatInt(TopSQLVariable.PrecisionSeconds.Load(), 10), nil
}, SetGlobal: func(vars *SessionVars, s string) error {
val, err := strconv.ParseInt(s, 10, 64)
@ -1715,7 +1715,7 @@ var defaultSysVars = []*SysVar{
TopSQLVariable.PrecisionSeconds.Store(val)
return nil
}},
{Scope: ScopeGlobal, Name: TiDBTopSQLMaxStatementCount, Value: strconv.Itoa(DefTiDBTopSQLMaxStatementCount), Type: TypeInt, Hidden: true, MinValue: 0, MaxValue: 5000, AllowEmpty: true, GetSession: func(s *SessionVars) (string, error) {
{Scope: ScopeGlobal, Name: TiDBTopSQLMaxStatementCount, Value: strconv.Itoa(DefTiDBTopSQLMaxStatementCount), Type: TypeInt, Hidden: true, MinValue: 0, MaxValue: 5000, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) {
return strconv.FormatInt(TopSQLVariable.MaxStatementCount.Load(), 10), nil
}, SetGlobal: func(vars *SessionVars, s string) error {
val, err := strconv.ParseInt(s, 10, 64)
@ -1725,7 +1725,7 @@ var defaultSysVars = []*SysVar{
TopSQLVariable.MaxStatementCount.Store(val)
return nil
}},
{Scope: ScopeGlobal, Name: TiDBTopSQLMaxCollect, Value: strconv.Itoa(DefTiDBTopSQLMaxCollect), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: 500000, AllowEmpty: true, GetSession: func(s *SessionVars) (string, error) {
{Scope: ScopeGlobal, Name: TiDBTopSQLMaxCollect, Value: strconv.Itoa(DefTiDBTopSQLMaxCollect), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: 500000, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) {
return strconv.FormatInt(TopSQLVariable.MaxCollect.Load(), 10), nil
}, SetGlobal: func(vars *SessionVars, s string) error {
val, err := strconv.ParseInt(s, 10, 64)
@ -1735,7 +1735,7 @@ var defaultSysVars = []*SysVar{
TopSQLVariable.MaxCollect.Store(val)
return nil
}},
{Scope: ScopeGlobal, Name: TiDBTopSQLReportIntervalSeconds, Value: strconv.Itoa(DefTiDBTopSQLReportIntervalSeconds), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: 1 * 60 * 60, AllowEmpty: true, GetSession: func(s *SessionVars) (string, error) {
{Scope: ScopeGlobal, Name: TiDBTopSQLReportIntervalSeconds, Value: strconv.Itoa(DefTiDBTopSQLReportIntervalSeconds), Type: TypeInt, Hidden: true, MinValue: 1, MaxValue: 1 * 60 * 60, AllowEmpty: true, GetGlobal: func(s *SessionVars) (string, error) {
return strconv.FormatInt(TopSQLVariable.ReportIntervalSeconds.Load(), 10), nil
}, SetGlobal: func(vars *SessionVars, s string) error {
val, err := strconv.ParseInt(s, 10, 64)

View File

@ -620,6 +620,30 @@ func (*testSysVarSuite) TestDefaultValuesAreSettable(c *C) {
}
}
// This tests that sysvars are logically correct with getter and setter functions.
// i.e. it doesn't make sense to have a SetSession function on a variable that is only globally scoped.
func (*testSysVarSuite) TestSettersandGetters(c *C) {
for _, sv := range GetSysVars() {
if !sv.HasSessionScope() {
// There are some historial exceptions where global variables are loaded into the session.
// Please don't add to this list, the behavior is not MySQL compatible.
switch sv.Name {
case TiDBEnableChangeMultiSchema, TiDBDDLReorgBatchSize, TiDBEnableAlterPlacement,
TiDBMaxDeltaSchemaCount, InitConnect, MaxPreparedStmtCount,
TiDBDDLReorgWorkerCount, TiDBDDLErrorCountLimit, TiDBRowFormatVersion,
TiDBEnableTelemetry, TiDBEnablePointGetCache:
continue
}
c.Assert(sv.SetSession, IsNil)
c.Assert(sv.GetSession, IsNil)
}
if !sv.HasGlobalScope() {
c.Assert(sv.SetGlobal, IsNil)
c.Assert(sv.GetGlobal, IsNil)
}
}
}
func (*testSysVarSuite) TestSecureAuth(c *C) {
sv := GetSysVar(SecureAuth)
vars := NewSessionVars()