This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cluster_test.go
278 lines (247 loc) · 6.62 KB
/
cluster_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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2014 by [email protected] (Peering GmbH)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package clustersql
import (
"database/sql"
"fmt"
"github.com/BurntSushi/toml"
"github.com/go-sql-driver/mysql"
"os"
"sync"
"sync/atomic"
"testing"
)
var db *sql.DB
type NodeCfg struct {
Name string
HostName string
Port int
UserName string
Password string
DBName string
}
type Config struct {
Nodes []NodeCfg
}
func TestOpen(t *testing.T) {
cfgfile := os.Getenv("DBCONFIG")
if cfgfile == "" {
cfgfile = "config.toml"
}
cfg := new(Config)
if f, err := os.Open(cfgfile); err != nil {
t.Fatal(err, "(did you set the DBCONFIG env variable?)")
} else {
if _, err := toml.DecodeReader(f, cfg); err != nil {
t.Fatal(err)
}
}
d := NewDriver(mysql.MySQLDriver{})
for _, ncfg := range cfg.Nodes {
if ncfg.Password != "" {
d.AddNode(ncfg.Name, fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", ncfg.UserName, ncfg.Password, ncfg.HostName, ncfg.Port, ncfg.DBName))
} else {
d.AddNode(ncfg.Name, fmt.Sprintf("%s@tcp(%s:%d)/%s", ncfg.UserName, ncfg.HostName, ncfg.Port, ncfg.DBName))
}
}
sql.Register("cluster", d)
var err error
db, err = sql.Open("cluster", "galera")
if err != nil {
t.Error(err)
}
}
func TestDrop(t *testing.T) {
_, err := db.Exec("DROP TABLE IF EXISTS test")
if err != nil {
t.Fatalf(err.Error())
}
}
func TestCreate(t *testing.T) {
_, err := db.Exec("CREATE TABLE test (value BOOL)")
if err != nil {
t.Fatalf(err.Error())
}
}
func TestEmptySelect(t *testing.T) {
rows, err := db.Query("SELECT * FROM test")
if err != nil {
t.Fatalf(err.Error())
}
if rows.Next() {
t.Fatalf("unexpected data in empty table")
}
}
func TestInsert(t *testing.T) {
res, err := db.Exec("INSERT INTO test VALUES (1)")
if err != nil {
t.Fatalf(err.Error())
}
count, err := res.RowsAffected()
if err != nil {
t.Fatalf("res.RowsAffected() returned error: %s", err.Error())
}
if count != 1 {
t.Fatalf("Expected 1 affected row, got %d", count)
}
id, err := res.LastInsertId()
if err != nil {
t.Fatalf("res.LastInsertId() returned error: %s", err.Error())
}
if id != 0 {
t.Fatalf("Expected InsertID 0, got %d", id)
}
}
func TestSelect(t *testing.T) {
rows, err := db.Query("SELECT value FROM test")
if err != nil {
t.Fatalf(err.Error())
}
if rows.Next() {
var out bool
rows.Scan(&out)
if true != out {
t.Errorf("true != %t", out)
}
if rows.Next() {
t.Error("unexpected data")
}
} else {
t.Error("no data")
}
}
func TestUpdate(t *testing.T) {
res, err := db.Exec("UPDATE test SET value = ? WHERE value = ?", false, true)
if err != nil {
t.Fatalf(err.Error())
}
count, err := res.RowsAffected()
if err != nil {
t.Fatalf("res.RowsAffected() returned error: %s", err.Error())
}
if count != 1 {
t.Fatalf("Expected 1 affected row, got %d", count)
}
rows, err := db.Query("SELECT value FROM test")
if err != nil {
t.Fatalf(err.Error())
}
if rows.Next() {
var out bool
rows.Scan(&out)
if false != out {
t.Errorf("false != %t", out)
}
if rows.Next() {
t.Error("unexpected data")
}
} else {
t.Error("no data")
}
}
func TestDelete(t *testing.T) {
res, err := db.Exec("DELETE FROM test WHERE value = ?", false)
if err != nil {
t.Fatalf(err.Error())
}
count, err := res.RowsAffected()
if err != nil {
t.Fatalf("res.RowsAffected() returned error: %s", err.Error())
}
if count != 1 {
t.Fatalf("Expected 1 affected row, got %d", count)
}
// Check for unexpected rows
res, err = db.Exec("DELETE FROM test")
if err != nil {
t.Fatalf(err.Error())
}
count, err = res.RowsAffected()
if err != nil {
t.Fatalf("res.RowsAffected() returned error: %s", err.Error())
}
if count != 0 {
t.Fatalf("Expected 0 affected row, got %d", count)
}
}
func TestConcurrent(t *testing.T) {
var max int
err := db.QueryRow("SELECT @@max_connections").Scan(&max)
if err != nil {
t.Fatalf("%s", err.Error())
}
// max = 100
fmt.Printf("Testing up to %d concurrent connections \r\n", max)
var remaining, succeeded int32 = int32(max), 0
var wg sync.WaitGroup
wg.Add(max)
var fatalError string
var once sync.Once
fatalf := func(s string, vals ...interface{}) {
once.Do(func() {
fatalError = fmt.Sprintf(s, vals...)
})
}
for i := 0; i < max; i++ {
go func(id int) {
defer wg.Done()
tx, err := db.Begin()
atomic.AddInt32(&remaining, -1)
fmt.Printf("%d ", remaining)
if err != nil {
if err.Error() != "Error 1040: Too many connections" {
//fmt.Printf("Begin: Error on Conn %d: %s", id, err.Error())
fatalf("Begin: Error on Conn %d: %s", id, err.Error())
// t.Logf("Begin: Error on Conn %d: %s", id, err.Error())
}
//fmt.Printf(" whoops ")
return
}
// keep the connection busy until all connections are open
for remaining > 0 {
if _, err = tx.Exec("DO 1"); err != nil {
//fmt.Printf("Exec: Error on Conn %d: %s", id, err.Error())
fatalf("Exec: Error on Conn %d: %s", id, err.Error())
return
}
}
if err = tx.Commit(); err != nil {
//fmt.Printf("Error on Conn %d: %s", id, err.Error())
fatalf("Error on Conn %d: %s", id, err.Error())
return
}
// everything went fine with this connection
atomic.AddInt32(&succeeded, 1)
}(i)
}
fmt.Println("waiting")
// wait until all conections are open
wg.Wait()
fmt.Println("waited")
if fatalError != "" {
t.Fatal(fatalError)
}
t.Logf("Reached %d concurrent connections\r\n", succeeded)
}