forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidb_test.go
251 lines (225 loc) · 6.68 KB
/
tidb_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2015 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 tidb
import (
"fmt"
"os"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/juju/errors"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/auth"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/testleak"
goctx "golang.org/x/net/context"
"google.golang.org/grpc"
)
func TestT(t *testing.T) {
logLevel := os.Getenv("log_level")
logutil.InitLogger(&logutil.LogConfig{
Level: logLevel,
})
CustomVerboseFlag = true
TestingT(t)
}
var _ = Suite(&testMainSuite{})
type testMainSuite struct {
dbName string
store kv.Storage
dom *domain.Domain
}
type brokenStore struct{}
func (s *brokenStore) Open(schema string) (kv.Storage, error) {
return nil, errors.New("try again later")
}
func (s *testMainSuite) SetUpSuite(c *C) {
testleak.BeforeTest()
s.dbName = "test_main_db"
s.store = newStore(c, s.dbName)
dom, err := BootstrapSession(s.store)
c.Assert(err, IsNil)
s.dom = dom
}
func (s *testMainSuite) TearDownSuite(c *C) {
defer testleak.AfterTest(c)()
s.dom.Close()
err := s.store.Close()
c.Assert(err, IsNil)
removeStore(c, s.dbName)
}
// Testcase for arg type.
func (s *testMainSuite) TestCheckArgs(c *C) {
checkArgs(nil, true, false, int8(1), int16(1), int32(1), int64(1), 1,
uint8(1), uint16(1), uint32(1), uint64(1), uint(1), float32(1), float64(1),
"abc", []byte("abc"), time.Now(), time.Hour, time.Local)
}
func (s *testMainSuite) TestIsQuery(c *C) {
tbl := []struct {
sql string
ok bool
}{
{"/*comment*/ select 1;", true},
{"/*comment*/ /*comment*/ select 1;", true},
{"select /*comment*/ 1 /*comment*/;", true},
{"(select /*comment*/ 1 /*comment*/);", true},
}
for _, t := range tbl {
c.Assert(IsQuery(t.sql), Equals, t.ok, Commentf(t.sql))
}
}
func (s *testMainSuite) TestTrimSQL(c *C) {
tbl := []struct {
sql string
target string
}{
{"/*comment*/ select 1; ", "select 1;"},
{"/*comment*/ /*comment*/ select 1;", "select 1;"},
{"select /*comment*/ 1 /*comment*/;", "select /*comment*/ 1 /*comment*/;"},
{"/*comment select 1; ", "/*comment select 1;"},
}
for _, t := range tbl {
c.Assert(trimSQL(t.sql), Equals, t.target, Commentf(t.sql))
}
}
func (s *testMainSuite) TestRetryOpenStore(c *C) {
begin := time.Now()
RegisterStore("dummy", &brokenStore{})
_, err := newStoreWithRetry("dummy://dummy-store", 3)
c.Assert(err, NotNil)
elapse := time.Since(begin)
c.Assert(uint64(elapse), GreaterEqual, uint64(3*time.Second))
}
func (s *testMainSuite) TestRetryDialPumpClient(c *C) {
retryDialPumpClientMustFail := func(binlogSocket string, clientCon *grpc.ClientConn, maxRetries int, dialerOpt grpc.DialOption) (err error) {
return util.RunWithRetry(maxRetries, 10, func() (bool, error) {
// Assume that it'll always return an error.
return true, errors.New("must fail")
})
}
begin := time.Now()
err := retryDialPumpClientMustFail("", nil, 3, nil)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "must fail")
elapse := time.Since(begin)
c.Assert(uint64(elapse), GreaterEqual, uint64(6*10*time.Millisecond))
}
func (s *testMainSuite) TestSysSessionPoolGoroutineLeak(c *C) {
c.Skip("make leak should check it")
// TODO: testleak package should be able to find this leak.
store, dom := newStoreWithBootstrap(c, s.dbName+"goroutine_leak")
defer dom.Close()
defer store.Close()
se, err := createSession(store)
c.Assert(err, IsNil)
// Test an issue that sysSessionPool doesn't call session's Close, cause
// asyncGetTSWorker goroutine leak.
before := runtime.NumGoroutine()
count := 200
var wg sync.WaitGroup
wg.Add(count)
for i := 0; i < count; i++ {
go func(se *session) {
_, _, err := se.ExecRestrictedSQL(se, "select * from mysql.user limit 1")
c.Assert(err, IsNil)
wg.Done()
}(se)
}
wg.Wait()
se.sysSessionPool().Close()
c.Assert(se.sysSessionPool().IsClosed(), Equals, true)
for i := 0; i < 300; i++ {
// After and before should be Equal, but this test may be disturbed by other factors.
// So I relax the strict check to make CI more stable.
after := runtime.NumGoroutine()
if after-before < 3 {
return
}
time.Sleep(20 * time.Millisecond)
}
after := runtime.NumGoroutine()
c.Assert(after-before, Less, 3)
}
func newStore(c *C, dbPath string) kv.Storage {
store, err := tikv.NewMockTikvStore()
c.Assert(err, IsNil)
return store
}
func newStoreWithBootstrap(c *C, dbPath string) (kv.Storage, *domain.Domain) {
store, err := tikv.NewMockTikvStore()
c.Assert(err, IsNil)
dom, err := BootstrapSession(store)
c.Assert(err, IsNil)
return store, dom
}
var testConnID uint64
func newSession(c *C, store kv.Storage, dbName string) Session {
se, err := CreateSession(store)
id := atomic.AddUint64(&testConnID, 1)
se.SetConnectionID(id)
c.Assert(err, IsNil)
se.Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, []byte("012345678901234567890"))
mustExecSQL(c, se, "create database if not exists "+dbName)
mustExecSQL(c, se, "use "+dbName)
return se
}
func removeStore(c *C, dbPath string) {
os.RemoveAll(dbPath)
}
func exec(se Session, sql string, args ...interface{}) (ast.RecordSet, error) {
if len(args) == 0 {
rs, err := se.Execute(goctx.Background(), sql)
if err == nil && len(rs) > 0 {
return rs[0], nil
}
return nil, err
}
stmtID, _, _, err := se.PrepareStmt(sql)
if err != nil {
return nil, err
}
rs, err := se.ExecutePreparedStmt(stmtID, args...)
if err != nil {
return nil, err
}
return rs, nil
}
func mustExecSQL(c *C, se Session, sql string, args ...interface{}) ast.RecordSet {
rs, err := exec(se, sql, args...)
c.Assert(err, IsNil)
return rs
}
func match(c *C, row []types.Datum, expected ...interface{}) {
c.Assert(len(row), Equals, len(expected))
for i := range row {
got := fmt.Sprintf("%v", row[i].GetValue())
need := fmt.Sprintf("%v", expected[i])
c.Assert(got, Equals, need)
}
}
func mustExecFailed(c *C, se Session, sql string, args ...interface{}) {
r, err := exec(se, sql, args...)
if err == nil && r != nil {
// sometimes we may meet error after executing first row.
_, err = r.Next()
}
c.Assert(err, NotNil)
}