forked from vitessio/vitess
/
testutils_test.go
172 lines (150 loc) · 4.22 KB
/
testutils_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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabletserver
import (
"fmt"
"html/template"
"math/rand"
"reflect"
"strings"
"testing"
"time"
"github.com/youtube/vitess/go/sqldb"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/mysqlctl"
"github.com/youtube/vitess/go/vt/vttest/fakesqldb"
)
type dummyChecker struct {
}
func (dummyChecker) CheckMySQL() {}
var DummyChecker = dummyChecker{}
type fakeCallInfo struct {
remoteAddr string
username string
text string
html string
}
func (fci *fakeCallInfo) RemoteAddr() string {
return fci.remoteAddr
}
func (fci *fakeCallInfo) Username() string {
return fci.username
}
func (fci *fakeCallInfo) Text() string {
return fci.text
}
func (fci *fakeCallInfo) HTML() template.HTML {
return template.HTML(fci.html)
}
type testUtils struct{}
func newTestUtils() *testUtils {
return &testUtils{}
}
func (util *testUtils) checkEqual(t *testing.T, expected interface{}, result interface{}) {
if !reflect.DeepEqual(expected, result) {
t.Fatalf("expect to get: %v, but got: %v", expected, result)
}
}
func (util *testUtils) checkTabletErrorWithRecover(t *testing.T, tabletErrType int, tabletErrStr string) {
err := recover()
if err == nil {
t.Fatalf("should get error")
}
util.checkTabletError(t, err, tabletErrType, tabletErrStr)
}
func (util *testUtils) checkTabletError(t *testing.T, err interface{}, tabletErrType int, tabletErrStr string) {
tabletError, ok := err.(*TabletError)
if !ok {
t.Fatalf("should return a TabletError, but got err: %v", err)
}
if tabletError.ErrorType != tabletErrType {
t.Fatalf("should return a TabletError with error type: %s", util.getTabletErrorString(tabletErrType))
}
if !strings.Contains(tabletError.Error(), tabletErrStr) {
t.Fatalf("expect the tablet error should contain string: '%s', but it does not. Got tablet error: '%s'", tabletErrStr, tabletError.Error())
}
}
func (util *testUtils) getTabletErrorString(tabletErrorType int) string {
switch tabletErrorType {
case ErrFail:
return "ErrFail"
case ErrRetry:
return "ErrRetry"
case ErrFatal:
return "ErrFatal"
case ErrTxPoolFull:
return "ErrTxPoolFull"
case ErrNotInTx:
return "ErrNotInTx"
}
return ""
}
func (util *testUtils) newMysqld(dbconfigs *dbconfigs.DBConfigs) mysqlctl.MysqlDaemon {
return mysqlctl.NewMysqld(
"",
"",
mysqlctl.NewMycnf(0, 6802),
&dbconfigs.Dba,
&dbconfigs.App.ConnParams,
&dbconfigs.Repl,
)
}
func (util *testUtils) newDBConfigs(db *fakesqldb.DB) dbconfigs.DBConfigs {
appDBConfig := dbconfigs.DBConfig{
ConnParams: sqldb.ConnParams{Engine: db.Name},
Keyspace: "test_keyspace",
Shard: "0",
}
return dbconfigs.DBConfigs{
App: appDBConfig,
}
}
func (util *testUtils) newQueryServiceConfig() Config {
randID := rand.Int63()
config := DefaultQsConfig
config.StatsPrefix = fmt.Sprintf("Stats-%d-", randID)
config.DebugURLPrefix = fmt.Sprintf("/debug-%d-", randID)
config.RowCache.StatsPrefix = fmt.Sprintf("Stats-%d-", randID)
config.PoolNamePrefix = fmt.Sprintf("Pool-%d-", randID)
config.StrictMode = true
config.RowCache.Binary = "ls"
config.RowCache.Connections = 100
config.EnablePublishStats = false
return config
}
func (util *testUtils) newConnPool() *ConnPool {
return NewConnPool(
"ConnPool",
100,
10*time.Second,
false,
NewQueryServiceStats("", false),
DummyChecker,
)
}
func newTestSchemaInfo(
queryCacheSize int,
reloadTime time.Duration,
idleTimeout time.Duration,
enablePublishStats bool) *SchemaInfo {
randID := rand.Int63()
name := fmt.Sprintf("TestSchemaInfo-%d-", randID)
queryServiceStats := NewQueryServiceStats(name, enablePublishStats)
return NewSchemaInfo(
name,
DummyChecker,
queryCacheSize,
reloadTime,
idleTimeout,
newTestSchemaInfoCachePool(enablePublishStats, queryServiceStats),
map[string]string{
debugQueryPlansKey: fmt.Sprintf("/debug/query_plans_%d", randID),
debugQueryStatsKey: fmt.Sprintf("/debug/query_stats_%d", randID),
debugTableStatsKey: fmt.Sprintf("/debug/table_stats_%d", randID),
debugSchemaKey: fmt.Sprintf("/debug/schema_%d", randID),
},
enablePublishStats,
queryServiceStats,
)
}