forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_test.go
More file actions
254 lines (224 loc) · 6.77 KB
/
parallel_test.go
File metadata and controls
254 lines (224 loc) · 6.77 KB
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
252
253
254
// Copyright 2016 The Cockroach Authors.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Radu Berinde (radu@cockroachlabs.com)
//
// The parallel_test adds an orchestration layer on top of the logic_test code
// with the capability of running multiple test data files in parallel.
//
// Each test lives in a separate subdir under testdata/paralleltest. Each test
// dir contains a "test.yaml" file along with a set of files in logic test
// format. The test.yaml file corresponds to the parTestSpec structure below.
package sql_test
import (
gosql "database/sql"
"flag"
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"strings"
"testing"
"golang.org/x/net/context"
"gopkg.in/yaml.v2"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/stop"
)
var (
paralleltestdata = flag.String("partestdata", "testdata/parallel_test/[^.]*", "test data glob")
)
type parallelTest struct {
*testing.T
ctx context.Context
cluster serverutils.TestClusterInterface
clients [][]*gosql.DB
}
func (t *parallelTest) close() {
t.clients = nil
if t.cluster != nil {
t.cluster.Stopper().Stop(context.TODO())
}
}
func (t *parallelTest) processTestFile(path string, nodeIdx int, db *gosql.DB, ch chan bool) {
if ch != nil {
defer func() { ch <- true }()
}
// Set up a dummy logicTest structure to use that code.
l := &logicTest{
t: t.T,
cluster: t.cluster,
nodeIdx: nodeIdx,
db: db,
user: security.RootUser,
verbose: testing.Verbose() || log.V(1),
}
if err := l.processTestFile(path, testClusterConfig{}); err != nil {
t.Error(err)
}
}
func (t *parallelTest) getClient(nodeIdx, clientIdx int) *gosql.DB {
for len(t.clients[nodeIdx]) <= clientIdx {
// Add a client.
pgURL, cleanupFunc := sqlutils.PGUrl(t.T,
t.cluster.Server(nodeIdx).ServingAddr(),
"TestParallel",
url.User(security.RootUser))
db, err := gosql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
sqlutils.MakeSQLRunner(t, db).Exec("SET DATABASE = test")
t.cluster.Stopper().AddCloser(
stop.CloserFn(func() {
_ = db.Close()
cleanupFunc()
}))
t.clients[nodeIdx] = append(t.clients[nodeIdx], db)
}
return t.clients[nodeIdx][clientIdx]
}
type parTestRunEntry struct {
Node int `yaml:"node"`
File string `yaml:"file"`
}
type parTestSpec struct {
SkipReason string `yaml:"skip_reason"`
// ClusterSize is the number of nodes in the cluster. If 0, single node.
ClusterSize int `yaml:"cluster_size"`
RangeSplitSize int `yaml:"range_split_size"`
// Run contains a set of "run lists". The files in a runlist are run in
// parallel and they complete before the next run list start.
Run [][]parTestRunEntry `yaml:"run"`
}
func (t *parallelTest) run(dir string) {
// Process the spec file.
mainFile := filepath.Join(dir, "test.yaml")
yamlData, err := ioutil.ReadFile(mainFile)
if err != nil {
t.Fatalf("%s: %s", mainFile, err)
}
var spec parTestSpec
err = yaml.Unmarshal(yamlData, &spec)
if err != nil {
t.Fatalf("%s: %s", mainFile, err)
}
if spec.SkipReason != "" {
t.Skip(spec.SkipReason)
}
log.Infof(t.ctx, "Running test %s", dir)
if testing.Verbose() || log.V(1) {
log.Infof(t.ctx, "spec: %+v", spec)
}
t.setup(&spec)
defer t.close()
for runListIdx, runList := range spec.Run {
if testing.Verbose() || log.V(1) {
var descr []string
for _, re := range runList {
descr = append(descr, fmt.Sprintf("%d:%s", re.Node, re.File))
}
log.Infof(t.ctx, "%s: run list %d: %s", mainFile, runListIdx,
strings.Join(descr, ", "))
}
// Store the number of clients used so far (per node).
numClients := make([]int, spec.ClusterSize)
ch := make(chan bool)
for _, re := range runList {
client := t.getClient(re.Node, numClients[re.Node])
numClients[re.Node]++
go t.processTestFile(filepath.Join(dir, re.File), re.Node, client, ch)
}
// Wait for all clients to complete.
for range runList {
<-ch
}
}
}
func (t *parallelTest) setup(spec *parTestSpec) {
if spec.ClusterSize == 0 {
spec.ClusterSize = 1
}
if testing.Verbose() || log.V(1) {
log.Infof(t.ctx, "Cluster Size: %d", spec.ClusterSize)
}
args := base.TestClusterArgs{
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
SQLExecutor: &sql.ExecutorTestingKnobs{
WaitForGossipUpdate: true,
CheckStmtStringChange: true,
},
},
},
}
t.cluster = serverutils.StartTestCluster(t, spec.ClusterSize, args)
t.clients = make([][]*gosql.DB, spec.ClusterSize)
for i := range t.clients {
t.clients[i] = append(t.clients[i], t.cluster.ServerConn(i))
}
r0 := sqlutils.MakeSQLRunner(t, t.clients[0][0])
if spec.RangeSplitSize != 0 {
if testing.Verbose() || log.V(1) {
log.Infof(t.ctx, "Setting range split size: %d", spec.RangeSplitSize)
}
zoneCfg := config.DefaultZoneConfig()
zoneCfg.RangeMaxBytes = int64(spec.RangeSplitSize)
zoneCfg.RangeMinBytes = zoneCfg.RangeMaxBytes / 2
buf, err := protoutil.Marshal(&zoneCfg)
if err != nil {
t.Fatal(err)
}
objID := keys.RootNamespaceID
r0.Exec(`UPDATE system.zones SET config = $2 WHERE id = $1`, objID, buf)
}
if testing.Verbose() || log.V(1) {
log.Infof(t.ctx, "Creating database")
}
r0.Exec("CREATE DATABASE test")
for i := range t.clients {
sqlutils.MakeSQLRunner(t, t.clients[i][0]).Exec("SET DATABASE = test")
}
if testing.Verbose() || log.V(1) {
log.Infof(t.ctx, "Test setup done")
}
}
func TestParallel(t *testing.T) {
defer leaktest.AfterTest(t)()
glob := string(*paralleltestdata)
paths, err := filepath.Glob(glob)
if err != nil {
t.Fatal(err)
}
if len(paths) == 0 {
t.Fatalf("No testfiles found (glob: %s)", glob)
}
total := 0
for _, path := range paths {
t.Run(filepath.Base(path), func(t *testing.T) {
pt := parallelTest{T: t, ctx: context.Background()}
pt.run(path)
total++
})
}
log.Infof(context.Background(), "%d parallel tests passed", total)
}