forked from talent-plan/tinysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_vars_cache_test.go
98 lines (89 loc) · 2.75 KB
/
global_vars_cache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package domain
import (
"time"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/testleak"
)
var _ = Suite(&testGVCSuite{})
type testGVCSuite struct{}
func (gvcSuite *testGVCSuite) TestSimple(c *C) {
defer testleak.AfterTest(c)()
testleak.BeforeTest()
store, err := mockstore.NewMockTikvStore()
c.Assert(err, IsNil)
defer store.Close()
ddlLease := 50 * time.Millisecond
dom := NewDomain(store, ddlLease, 0, mockFactory)
err = dom.Init(ddlLease, sysMockFactory)
c.Assert(err, IsNil)
defer dom.Close()
// Get empty global vars cache.
gvc := dom.GetGlobalVarsCache()
succ, rows, fields := gvc.Get()
c.Assert(succ, IsFalse)
c.Assert(rows, IsNil)
c.Assert(fields, IsNil)
// Get a variable from global vars cache.
rf := getResultField("c", 1, 0)
rf1 := getResultField("c1", 2, 1)
ft := &types.FieldType{
Tp: mysql.TypeString,
Charset: charset.CharsetBin,
Collate: charset.CollationBin,
}
ft1 := &types.FieldType{
Tp: mysql.TypeString,
Charset: charset.CharsetBin,
Collate: charset.CollationBin,
}
ck := chunk.NewChunkWithCapacity([]*types.FieldType{ft, ft1}, 1024)
ck.AppendString(0, "variable1")
ck.AppendString(1, "value1")
row := ck.GetRow(0)
gvc.Update([]chunk.Row{row}, []*ast.ResultField{rf, rf1})
succ, rows, fields = gvc.Get()
c.Assert(succ, IsTrue)
c.Assert(rows[0], Equals, row)
c.Assert(fields, DeepEquals, []*ast.ResultField{rf, rf1})
// Disable the cache.
gvc.Disable()
succ, rows, fields = gvc.Get()
c.Assert(succ, IsFalse)
c.Assert(rows[0], Equals, row)
c.Assert(fields, DeepEquals, []*ast.ResultField{rf, rf1})
}
func getResultField(colName string, id, offset int) *ast.ResultField {
return &ast.ResultField{
Column: &model.ColumnInfo{
Name: model.NewCIStr(colName),
ID: int64(id),
Offset: offset,
FieldType: types.FieldType{
Tp: mysql.TypeString,
Charset: charset.CharsetUTF8,
Collate: charset.CollationUTF8,
},
},
TableAsName: model.NewCIStr("tbl"),
DBName: model.NewCIStr("test"),
}
}