forked from mattn/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3_go18_test.go
503 lines (439 loc) · 11.6 KB
/
sqlite3_go18_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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright (C) 2019 Yasuhiro Matsumoto <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// +build go1.8,cgo
package sqlite3
import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"math/rand"
"os"
"sync"
"testing"
"time"
)
func TestNamedParams(t *testing.T) {
tempFilename := TempFilename(t)
defer os.Remove(tempFilename)
db, err := sql.Open("sqlite3", tempFilename)
if err != nil {
t.Fatal("Failed to open database:", err)
}
defer db.Close()
_, err = db.Exec(`
create table foo (id integer, name text, extra text);
`)
if err != nil {
t.Error("Failed to call db.Query:", err)
}
_, err = db.Exec(`insert into foo(id, name, extra) values(:id, :name, :name)`, sql.Named("name", "foo"), sql.Named("id", 1))
if err != nil {
t.Error("Failed to call db.Exec:", err)
}
row := db.QueryRow(`select id, extra from foo where id = :id and extra = :extra`, sql.Named("id", 1), sql.Named("extra", "foo"))
if row == nil {
t.Error("Failed to call db.QueryRow")
}
var id int
var extra string
err = row.Scan(&id, &extra)
if err != nil {
t.Error("Failed to db.Scan:", err)
}
if id != 1 || extra != "foo" {
t.Error("Failed to db.QueryRow: not matched results")
}
}
var (
testTableStatements = []string{
`DROP TABLE IF EXISTS test_table`,
`
CREATE TABLE IF NOT EXISTS test_table (
key1 VARCHAR(64) PRIMARY KEY,
key_id VARCHAR(64) NOT NULL,
key2 VARCHAR(64) NOT NULL,
key3 VARCHAR(64) NOT NULL,
key4 VARCHAR(64) NOT NULL,
key5 VARCHAR(64) NOT NULL,
key6 VARCHAR(64) NOT NULL,
data BLOB NOT NULL
);`,
}
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
func randStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func initDatabase(t *testing.T, db *sql.DB, rowCount int64) {
for _, query := range testTableStatements {
_, err := db.Exec(query)
if err != nil {
t.Fatal(err)
}
}
for i := int64(0); i < rowCount; i++ {
query := `INSERT INTO test_table
(key1, key_id, key2, key3, key4, key5, key6, data)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?);`
args := []interface{}{
randStringBytes(50),
fmt.Sprint(i),
randStringBytes(50),
randStringBytes(50),
randStringBytes(50),
randStringBytes(50),
randStringBytes(50),
randStringBytes(50),
randStringBytes(2048),
}
_, err := db.Exec(query, args...)
if err != nil {
t.Fatal(err)
}
}
}
func TestShortTimeout(t *testing.T) {
srcTempFilename := TempFilename(t)
defer os.Remove(srcTempFilename)
db, err := sql.Open("sqlite3", srcTempFilename)
if err != nil {
t.Fatal(err)
}
defer db.Close()
initDatabase(t, db, 100)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
defer cancel()
query := `SELECT key1, key_id, key2, key3, key4, key5, key6, data
FROM test_table
ORDER BY key2 ASC`
_, err = db.QueryContext(ctx, query)
if err != nil && err != context.DeadlineExceeded {
t.Fatal(err)
}
if ctx.Err() != nil && ctx.Err() != context.DeadlineExceeded {
t.Fatal(ctx.Err())
}
}
func TestExecContextCancel(t *testing.T) {
srcTempFilename := TempFilename(t)
defer os.Remove(srcTempFilename)
db, err := sql.Open("sqlite3", srcTempFilename)
if err != nil {
t.Fatal(err)
}
defer db.Close()
ts := time.Now()
initDatabase(t, db, 1000)
spent := time.Since(ts)
const minTestTime = 100 * time.Millisecond
if spent < minTestTime {
t.Skipf("test will be too racy (spent=%s < min=%s) as ExecContext below will be too fast.",
spent.String(), minTestTime.String(),
)
}
// expected to be extremely slow query
q := `
INSERT INTO test_table (key1, key_id, key2, key3, key4, key5, key6, data)
SELECT t1.key1 || t2.key1, t1.key_id || t2.key_id, t1.key2 || t2.key2, t1.key3 || t2.key3, t1.key4 || t2.key4, t1.key5 || t2.key5, t1.key6 || t2.key6, t1.data || t2.data
FROM test_table t1 LEFT OUTER JOIN test_table t2`
// expect query above take ~ same time as setup above
// This is racy: the context must be valid so sql/db.ExecContext calls the sqlite3 driver.
// It starts the query, the context expires, then calls sqlite3_interrupt
ctx, cancel := context.WithTimeout(context.Background(), minTestTime/2)
defer cancel()
ts = time.Now()
r, err := db.ExecContext(ctx, q)
// racy check
if r != nil {
n, err := r.RowsAffected()
t.Logf("query should not have succeeded: rows=%d; err=%v; duration=%s",
n, err, time.Since(ts).String())
}
if err != context.DeadlineExceeded {
t.Fatal(err, ctx.Err())
}
}
func TestQueryRowContextCancel(t *testing.T) {
srcTempFilename := TempFilename(t)
defer os.Remove(srcTempFilename)
db, err := sql.Open("sqlite3", srcTempFilename)
if err != nil {
t.Fatal(err)
}
defer db.Close()
initDatabase(t, db, 100)
const query = `SELECT key_id FROM test_table ORDER BY key2 ASC`
var keyID string
unexpectedErrors := make(map[string]int)
for i := 0; i < 10000; i++ {
ctx, cancel := context.WithCancel(context.Background())
row := db.QueryRowContext(ctx, query)
cancel()
// it is fine to get "nil" as context cancellation can be handled with delay
if err := row.Scan(&keyID); err != nil && err != context.Canceled {
if err.Error() == "sql: Rows are closed" {
// see https://github.com/golang/go/issues/24431
// fixed in 1.11.1 to properly return context error
continue
}
unexpectedErrors[err.Error()]++
}
}
for errText, count := range unexpectedErrors {
t.Error(errText, count)
}
}
func TestQueryRowContextCancelParallel(t *testing.T) {
srcTempFilename := TempFilename(t)
defer os.Remove(srcTempFilename)
db, err := sql.Open("sqlite3", srcTempFilename)
if err != nil {
t.Fatal(err)
}
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(5)
defer db.Close()
initDatabase(t, db, 100)
const query = `SELECT key_id FROM test_table ORDER BY key2 ASC`
wg := sync.WaitGroup{}
defer wg.Wait()
testCtx, cancel := context.WithCancel(context.Background())
defer cancel()
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
var keyID string
for {
select {
case <-testCtx.Done():
return
default:
}
ctx, cancel := context.WithCancel(context.Background())
row := db.QueryRowContext(ctx, query)
cancel()
_ = row.Scan(&keyID) // see TestQueryRowContextCancel
}
}()
}
var keyID string
for i := 0; i < 10000; i++ {
// note that testCtx is not cancelled during query execution
row := db.QueryRowContext(testCtx, query)
if err := row.Scan(&keyID); err != nil {
t.Fatal(i, err)
}
}
}
func TestExecCancel(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err = db.Exec("create table foo (id integer primary key)"); err != nil {
t.Fatal(err)
}
for n := 0; n < 100; n++ {
ctx, cancel := context.WithCancel(context.Background())
_, err = db.ExecContext(ctx, "insert into foo (id) values (?)", n)
cancel()
if err != nil {
t.Fatal(err)
}
}
}
func doTestOpenContext(t *testing.T, option string) (string, error) {
tempFilename := TempFilename(t)
url := tempFilename + option
defer func() {
err := os.Remove(tempFilename)
if err != nil {
t.Error("temp file remove error:", err)
}
}()
db, err := sql.Open("sqlite3", url)
if err != nil {
return "Failed to open database:", err
}
defer func() {
err = db.Close()
if err != nil {
t.Error("db close error:", err)
}
}()
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
err = db.PingContext(ctx)
cancel()
if err != nil {
return "ping error:", err
}
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
_, err = db.ExecContext(ctx, "drop table foo")
cancel()
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
_, err = db.ExecContext(ctx, "create table foo (id integer)")
cancel()
if err != nil {
return "Failed to create table:", err
}
if stat, err := os.Stat(tempFilename); err != nil || stat.IsDir() {
return "Failed to create ./foo.db", nil
}
return "", nil
}
func TestOpenContext(t *testing.T) {
cases := map[string]bool{
"": true,
"?_txlock=immediate": true,
"?_txlock=deferred": true,
"?_txlock=exclusive": true,
"?_txlock=bogus": false,
}
for option, expectedPass := range cases {
result, err := doTestOpenContext(t, option)
if result == "" {
if !expectedPass {
errmsg := fmt.Sprintf("_txlock error not caught at dbOpen with option: %s", option)
t.Fatal(errmsg)
}
} else if expectedPass {
if err == nil {
t.Fatal(result)
} else {
t.Fatal(result, err)
}
}
}
}
func TestFileCopyTruncate(t *testing.T) {
var err error
tempFilename := TempFilename(t)
defer func() {
err = os.Remove(tempFilename)
if err != nil {
t.Error("temp file remove error:", err)
}
}()
db, err := sql.Open("sqlite3", tempFilename)
if err != nil {
t.Fatal("open error:", err)
}
defer func() {
err = db.Close()
if err != nil {
t.Error("db close error:", err)
}
}()
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
err = db.PingContext(ctx)
cancel()
if err != nil {
t.Fatal("ping error:", err)
}
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
_, err = db.ExecContext(ctx, "drop table foo")
cancel()
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
_, err = db.ExecContext(ctx, "create table foo (id integer)")
cancel()
if err != nil {
t.Fatal("create table error:", err)
}
// copy db to new file
var data []byte
data, err = ioutil.ReadFile(tempFilename)
if err != nil {
t.Fatal("read file error:", err)
}
var f *os.File
f, err = os.Create(tempFilename + "-db-copy")
if err != nil {
t.Fatal("create file error:", err)
}
defer func() {
err = os.Remove(tempFilename + "-db-copy")
if err != nil {
t.Error("temp file moved remove error:", err)
}
}()
_, err = f.Write(data)
if err != nil {
f.Close()
t.Fatal("write file error:", err)
}
err = f.Close()
if err != nil {
t.Fatal("close file error:", err)
}
// truncate current db file
f, err = os.OpenFile(tempFilename, os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
t.Fatal("open file error:", err)
}
err = f.Close()
if err != nil {
t.Fatal("close file error:", err)
}
// test db after file truncate
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
err = db.PingContext(ctx)
cancel()
if err != nil {
t.Fatal("ping error:", err)
}
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
_, err = db.ExecContext(ctx, "drop table foo")
cancel()
if err == nil {
t.Fatal("drop table no error")
}
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
_, err = db.ExecContext(ctx, "create table foo (id integer)")
cancel()
if err != nil {
t.Fatal("create table error:", err)
}
err = db.Close()
if err != nil {
t.Error("db close error:", err)
}
// test copied file
db, err = sql.Open("sqlite3", tempFilename+"-db-copy")
if err != nil {
t.Fatal("open error:", err)
}
defer func() {
err = db.Close()
if err != nil {
t.Error("db close error:", err)
}
}()
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
err = db.PingContext(ctx)
cancel()
if err != nil {
t.Fatal("ping error:", err)
}
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
_, err = db.ExecContext(ctx, "drop table foo")
cancel()
if err != nil {
t.Fatal("drop table error:", err)
}
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
_, err = db.ExecContext(ctx, "create table foo (id integer)")
cancel()
if err != nil {
t.Fatal("create table error:", err)
}
}