-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconn_test.go
193 lines (169 loc) · 4.71 KB
/
conn_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
package sql
// Special thanks to @soniah for tests below.
import (
"bytes"
"context"
"database/sql"
"fmt"
"io/ioutil"
"log"
"os"
"sync"
"testing"
"text/tabwriter"
"time"
"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/ory/dockertest"
"github.com/stretchr/testify/assert"
)
// nolint:gochecknoglobals
var dockerPool *dockertest.Pool // the connection to docker
// nolint:gochecknoglobals
var systemdb *sql.DB // the connection to the mysql 'system' database
// nolint:gochecknoglobals
var sqlConfig *mysql.Config // the mysql container and config for connecting to other databases
// nolint:gochecknoglobals
var testMu *sync.Mutex // controls access to sqlConfig
func TestMain(m *testing.M) {
_ = mysql.SetLogger(log.New(ioutil.Discard, "", 0)) // silence mysql logger
testMu = &sync.Mutex{}
var err error
dockerPool, err = dockertest.NewPool("")
if err != nil {
log.Fatalf("could not connect to docker: %s", err)
}
dockerPool.MaxWait = time.Minute * 2
runOptions := dockertest.RunOptions{
Repository: "mysql",
Tag: "5.6",
Env: []string{"MYSQL_ROOT_PASSWORD=secret"},
}
mysqlContainer, err := dockerPool.RunWithOptions(&runOptions)
if err != nil {
log.Fatalf("could not start mysqlContainer: %s", err)
}
sqlConfig = &mysql.Config{
User: "root",
Passwd: "secret",
Net: "tcp",
Addr: fmt.Sprintf("localhost:%s", mysqlContainer.GetPort("3306/tcp")),
DBName: "mysql",
AllowNativePasswords: true,
}
if err = dockerPool.Retry(func() error {
systemdb, err = sql.Open("mysql", sqlConfig.FormatDSN())
if err != nil {
return err
}
return systemdb.Ping()
}); err != nil {
log.Fatal(err)
}
code := m.Run()
// You can't defer this because os.Exit ignores defer
if err := dockerPool.Purge(mysqlContainer); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
os.Exit(code)
}
func TestCancel(t *testing.T) {
var err error
_, err = systemdb.Exec("create database TestCancel")
assert.NoError(t, err)
testMu.Lock()
testCancelConfig := sqlConfig
testMu.Unlock()
testCancelConfig.DBName = "TestCancel"
var dbStd *sql.DB
if err := dockerPool.Retry(func() error {
dbStd, err = sql.Open("mysql", testCancelConfig.FormatDSN())
if err != nil {
return err
}
return dbStd.Ping()
}); err != nil {
log.Fatal(err)
}
dbKiller, err := sql.Open("mysql", testCancelConfig.FormatDSN())
dbKiller.SetMaxOpenConns(1)
pool := &DB{DB: dbStd, KillerPool: dbKiller}
procs, err := helperFullProcessList(dbStd)
assert.NoError(t, err)
filterDB := func(m mySQLProcInfo) bool { return m.DB == "TestCancel" }
filterState := func(m mySQLProcInfo) bool { return m.State == "executing" }
procs = procs.Filter(filterDB, filterState)
assert.Len(t, procs, 0)
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
defer cancel()
conn, err := pool.Conn(ctx)
assert.NoError(t, err)
defer conn.Close()
go func() {
_, err = conn.ExecContext(ctx, "select benchmark(9999999999, md5('I like traffic lights'))")
assert.Equal(t, context.DeadlineExceeded, err)
}()
ticker := time.NewTicker(100 * time.Millisecond)
Loop:
for {
select {
case <-ticker.C:
procs, err := helperFullProcessList(dbStd)
assert.NoError(t, err)
procs = procs.Filter(filterDB, filterState)
assert.Len(t, procs, 1)
case <-ctx.Done():
time.Sleep(3000 * time.Millisecond)
procs, err := helperFullProcessList(dbStd)
assert.NoError(t, err)
procs = procs.Filter(filterDB, filterState)
assert.Len(t, procs, 0)
break Loop
}
}
}
type mySQLProcInfo struct {
ID int64 `db:"Id"`
User string `db:"User"`
Host string `db:"Host"`
DB string `db:"db"`
Command string `db:"Command"`
Time int `db:"Time"`
State string `db:"State"`
Info *string `db:"Info"`
}
type mySQLProcsInfo []mySQLProcInfo
func helperFullProcessList(db *sql.DB) (mySQLProcsInfo, error) {
dbx := sqlx.NewDb(db, "mysql")
var procs []mySQLProcInfo
if err := dbx.Select(&procs, "show full processlist"); err != nil {
return nil, err
}
return procs, nil
}
func (ms mySQLProcsInfo) String() string {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "ID\tUser\tHost\tDB\tCommand\tTime\tState\tInfo")
for _, m := range ms {
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n", m.ID, m.User, m.Host, m.DB, m.Command, m.Time,
m.State, m.Info)
}
w.Flush()
return buf.String()
}
func (ms mySQLProcsInfo) Filter(fns ...func(m mySQLProcInfo) bool) (result mySQLProcsInfo) {
for _, m := range ms {
ok := true
for _, fn := range fns {
if !fn(m) {
ok = false
break
}
}
if ok {
result = append(result, m)
}
}
return result
}