This repository has been archived by the owner on Jun 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_test.go
140 lines (130 loc) · 2.83 KB
/
driver_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
package rdsmysql
import (
"database/sql"
"fmt"
"net/url"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type testLogger struct {
mu sync.Mutex
}
func (l *testLogger) Log(keyvals ...interface{}) error {
l.mu.Lock()
defer l.mu.Unlock()
kv := make(map[interface{}]interface{})
for i := 0; i < len(keyvals); i += 2 {
k := keyvals[i]
v := keyvals[i+1]
kv[k] = v
}
fmt.Print("[LOG] ")
args := make([]interface{}, 0)
if msg, ok := kv["msg"].(string); ok {
args = append(args, msg)
delete(kv, "msg")
for k, v := range kv {
args = append(args, fmt.Sprintf("%v=%v", k, v))
}
fmt.Println(args...)
} else {
fmt.Printf(keyvals[0].(string), keyvals[1:]...)
fmt.Println()
}
return nil
}
func TestDriver(t *testing.T) {
db, err := sql.Open(DriverName, "root:@localhost/mysql")
if err != nil {
t.Fatal(err)
}
defer db.Close()
res, err := db.Query("select now()")
if err != nil {
t.Fatal(err)
}
defer res.Close()
for res.Next() {
var val sql.NullString
if err := res.Scan(&val); err != nil {
t.Fatal(err)
}
if !val.Valid {
t.Fatal("should have returned a value")
}
}
}
func TestDriverMultiple(t *testing.T) {
count := 100
var wg sync.WaitGroup
for i := 0; i < count; i++ {
wg.Add(1)
go func() {
defer wg.Done()
db, err := sql.Open(DriverName, "root:@localhost/mysql")
if err != nil {
t.Fatal(err)
}
db.SetMaxOpenConns(1)
defer db.Close()
res, err := db.Query("select now()")
if err != nil {
t.Fatal(err)
}
defer res.Close()
for res.Next() {
var val sql.NullString
if err := res.Scan(&val); err != nil {
t.Fatal(err)
}
if !val.Valid {
t.Fatal("should have returned a value")
}
}
}()
}
wg.Wait()
}
func TestDriverPasswordEscape(t *testing.T) {
up := url.UserPassword("foo", "xZ{G{V?X-R:y%l")
db, err := sql.Open(DriverName, up.String()+"@localhost/mysql")
if err != nil {
t.Fatal(err)
}
defer db.Close()
_, err = db.Query("select now()")
if err == nil {
t.Fatal("should have been an error")
}
assertEq(t, "Error 1045: Access denied for user 'foo'@'localhost' (using password: YES)", err.Error())
}
func TestGetReplicaHostname(t *testing.T) {
c := &connection{
hostname: "pinpt-78718787918791-rds-cluster.cluster-cyi98989111.us-east-1.rds.amazonaws.com",
}
replica, err := c.getReplicaHostname("pinpt-78718787918791-rds-node-1")
if err != nil {
t.Fatal(err)
}
assertEq(t, "pinpt-78718787918791-rds-node-1.cyi98989111.us-east-1.rds.amazonaws.com", replica)
}
func TestExponentialBackoffValue(t *testing.T) {
assert := assert.New(t)
for _, e := range []struct {
in int
out time.Duration
}{
{1, 500 * time.Millisecond},
{2, time.Second},
{3, 2 * time.Second},
{4, 4 * time.Second},
{5, 8 * time.Second},
} {
assert.Equal(e.out, exponentialBackoffValue(e.in))
}
}
func init() {
L = &testLogger{}
}