@@ -4,6 +4,7 @@ package utils_test
4
4
5
5
import (
6
6
"context"
7
+ "strings"
7
8
"testing"
8
9
9
10
"github.com/pingcap/errors"
@@ -35,7 +36,19 @@ func (m *mockRestrictedSQLExecutor) ExecRestrictedSQL(ctx context.Context, opts
35
36
if m .errHappen {
36
37
return nil , nil , errors .New ("injected error" )
37
38
}
38
- return m .rows , m .fields , nil
39
+
40
+ if strings .Contains (sql , "show config" ) {
41
+ return m .rows , m .fields , nil
42
+ } else if strings .Contains (sql , "set config" ) && strings .Contains (sql , "gc.ratio-threshold" ) {
43
+ value := args [0 ].(string )
44
+
45
+ for _ , r := range m .rows {
46
+ d := types.Datum {}
47
+ d .SetString (value , "" )
48
+ chunk .MutRow (r ).SetDatum (3 , d )
49
+ }
50
+ }
51
+ return nil , nil , nil
39
52
}
40
53
41
54
func TestIsLogBackupEnabled (t * testing.T ) {
@@ -115,3 +128,43 @@ func TestCheckLogBackupTaskExist(t *testing.T) {
115
128
utils .LogBackupTaskCountDec ()
116
129
require .False (t , utils .CheckLogBackupTaskExist ())
117
130
}
131
+
132
+ func TestGc (t * testing.T ) {
133
+ // config format:
134
+ // MySQL [(none)]> show config where name = 'gc.ratio-threshold';
135
+ // +------+-------------------+--------------------+-------+
136
+ // | Type | Instance | Name | Value |
137
+ // +------+-------------------+--------------------+-------+
138
+ // | tikv | 172.16.6.46:3460 | gc.ratio-threshold | 1.1 |
139
+ // | tikv | 172.16.6.47:3460 | gc.ratio-threshold | 1.1 |
140
+ // +------+-------------------+--------------------+-------+
141
+ fields := make ([]* ast.ResultField , 4 )
142
+ tps := []* types.FieldType {
143
+ types .NewFieldType (mysql .TypeString ),
144
+ types .NewFieldType (mysql .TypeString ),
145
+ types .NewFieldType (mysql .TypeString ),
146
+ types .NewFieldType (mysql .TypeString ),
147
+ }
148
+ for i := 0 ; i < len (tps ); i ++ {
149
+ rf := new (ast.ResultField )
150
+ rf .Column = new (model.ColumnInfo )
151
+ rf .Column .FieldType = * tps [i ]
152
+ fields [i ] = rf
153
+ }
154
+ rows := make ([]chunk.Row , 0 , 2 )
155
+ row := chunk .MutRowFromValues ("tikv" , " 127.0.0.1:20161" , "log-backup.enable" , "1.1" ).ToRow ()
156
+ rows = append (rows , row )
157
+ row = chunk .MutRowFromValues ("tikv" , " 127.0.0.1:20162" , "log-backup.enable" , "1.1" ).ToRow ()
158
+ rows = append (rows , row )
159
+
160
+ s := & mockRestrictedSQLExecutor {rows : rows , fields : fields }
161
+ ratio , err := utils .GetGcRatio (s )
162
+ require .Nil (t , err )
163
+ require .Equal (t , ratio , "1.1" )
164
+
165
+ err = utils .SetGcRatio (s , "-1.0" )
166
+ require .Nil (t , err )
167
+ ratio , err = utils .GetGcRatio (s )
168
+ require .Nil (t , err )
169
+ require .Equal (t , ratio , "-1.0" )
170
+ }
0 commit comments