1
1
package tests
2
2
3
3
import (
4
- "context"
5
4
"errors"
5
+ "fmt"
6
6
"io"
7
7
"log"
8
8
"net/url"
9
9
"os"
10
10
"os/exec"
11
11
"path/filepath"
12
12
"testing"
13
- "time"
14
13
15
14
"golang.org/x/sync/errgroup"
16
15
@@ -24,6 +23,7 @@ import (
24
23
)
25
24
26
25
func TestMain (m * testing.M ) {
26
+ sqlite3 .Initialize ()
27
27
sqlite3 .AutoExtension (func (c * sqlite3.Conn ) error {
28
28
return c .ConfigLog (func (code sqlite3.ExtendedErrorCode , msg string ) {
29
29
// Having to do journal recovery is unexpected.
@@ -54,6 +54,7 @@ func Test_parallel(t *testing.T) {
54
54
"?_pragma=busy_timeout(10000)" +
55
55
"&_pragma=journal_mode(truncate)" +
56
56
"&_pragma=synchronous(off)"
57
+ createDB (t , name )
57
58
testParallel (t , name , iter )
58
59
testIntegrity (t , name )
59
60
}
@@ -75,6 +76,7 @@ func Test_wal(t *testing.T) {
75
76
"?_pragma=busy_timeout(10000)" +
76
77
"&_pragma=journal_mode(wal)" +
77
78
"&_pragma=synchronous(off)"
79
+ createDB (t , name )
78
80
testParallel (t , name , iter )
79
81
testIntegrity (t , name )
80
82
}
@@ -90,6 +92,7 @@ func Test_memdb(t *testing.T) {
90
92
name := memdb .TestDB (t , url.Values {
91
93
"_pragma" : {"busy_timeout(10000)" },
92
94
})
95
+ createDB (t , name )
93
96
testParallel (t , name , iter )
94
97
testIntegrity (t , name )
95
98
}
@@ -113,6 +116,7 @@ func Test_adiantum(t *testing.T) {
113
116
"&_pragma=busy_timeout(10000)" +
114
117
"&_pragma=journal_mode(truncate)" +
115
118
"&_pragma=synchronous(off)"
119
+ createDB (t , name )
116
120
testParallel (t , name , iter )
117
121
testIntegrity (t , name )
118
122
}
@@ -136,6 +140,7 @@ func Test_xts(t *testing.T) {
136
140
"&_pragma=busy_timeout(10000)" +
137
141
"&_pragma=journal_mode(truncate)" +
138
142
"&_pragma=synchronous(off)"
143
+ createDB (t , name )
139
144
testParallel (t , name , iter )
140
145
testIntegrity (t , name )
141
146
}
@@ -155,14 +160,17 @@ func Test_MultiProcess_rollback(t *testing.T) {
155
160
"?_pragma=busy_timeout(10000)" +
156
161
"&_pragma=journal_mode(truncate)" +
157
162
"&_pragma=synchronous(off)"
163
+ createDB (t , name )
158
164
159
165
exe , err := os .Executable ()
160
166
if err != nil {
161
167
t .Fatal (err )
162
168
}
163
169
164
- cmd := exec .Command (exe , append (os .Args [1 :], "-test.v" , "-test.run=Test_ChildProcess_rollback" )... )
170
+ cmd := exec .Command (exe , append (os .Args [1 :],
171
+ "-test.v" , "-test.count=1" , "-test.run=Test_ChildProcess_rollback" )... )
165
172
out , err := cmd .StdoutPipe ()
173
+ cmd .Stderr = os .Stderr
166
174
if err != nil {
167
175
t .Fatal (err )
168
176
}
@@ -214,14 +222,17 @@ func Test_MultiProcess_wal(t *testing.T) {
214
222
"?_pragma=busy_timeout(10000)" +
215
223
"&_pragma=journal_mode(wal)" +
216
224
"&_pragma=synchronous(off)"
225
+ createDB (t , name )
217
226
218
227
exe , err := os .Executable ()
219
228
if err != nil {
220
229
t .Fatal (err )
221
230
}
222
231
223
- cmd := exec .Command (exe , append (os .Args [1 :], "-test.v" , "-test.run=Test_ChildProcess_wal" )... )
232
+ cmd := exec .Command (exe , append (os .Args [1 :],
233
+ "-test.v" , "-test.count=1" , "-test.run=Test_ChildProcess_wal" )... )
224
234
out , err := cmd .StdoutPipe ()
235
+ cmd .Stderr = os .Stderr
225
236
if err != nil {
226
237
t .Fatal (err )
227
238
}
@@ -263,14 +274,14 @@ func Benchmark_parallel(b *testing.B) {
263
274
b .Skip ("skipping without shared memory" )
264
275
}
265
276
266
- sqlite3 .Initialize ()
267
- b .ResetTimer ()
268
-
269
277
name := "file:" +
270
278
filepath .Join (b .TempDir (), "test.db" ) +
271
279
"?_pragma=busy_timeout(10000)" +
272
280
"&_pragma=journal_mode(truncate)" +
273
281
"&_pragma=synchronous(off)"
282
+ createDB (b , name )
283
+
284
+ b .ResetTimer ()
274
285
testParallel (b , name , b .N )
275
286
}
276
287
@@ -279,55 +290,51 @@ func Benchmark_wal(b *testing.B) {
279
290
b .Skip ("skipping without shared memory" )
280
291
}
281
292
282
- sqlite3 .Initialize ()
283
- b .ResetTimer ()
284
-
285
293
name := "file:" +
286
294
filepath .Join (b .TempDir (), "test.db" ) +
287
295
"?_pragma=busy_timeout(10000)" +
288
296
"&_pragma=journal_mode(wal)" +
289
297
"&_pragma=synchronous(off)"
298
+ createDB (b , name )
299
+
300
+ b .ResetTimer ()
290
301
testParallel (b , name , b .N )
291
302
}
292
303
293
304
func Benchmark_memdb (b * testing.B ) {
294
- sqlite3 .Initialize ()
295
- b .ResetTimer ()
296
-
297
305
name := memdb .TestDB (b , url.Values {
298
306
"_pragma" : {"busy_timeout(10000)" },
299
307
})
308
+ createDB (b , name )
309
+
310
+ b .ResetTimer ()
300
311
testParallel (b , name , b .N )
301
312
}
302
313
314
+ func createDB (t testing.TB , name string ) {
315
+ db , err := sqlite3 .Open (name )
316
+ if err != nil {
317
+ t .Fatal (err )
318
+ }
319
+ defer db .Close ()
320
+
321
+ err = db .Exec (`CREATE TABLE IF NOT EXISTS users (id INT, name VARCHAR(10))` )
322
+ if err != nil {
323
+ t .Fatal (err )
324
+ }
325
+ }
326
+
303
327
func testParallel (t testing.TB , name string , n int ) {
304
328
writer := func () error {
305
329
db , err := sqlite3 .Open (name )
306
330
if err != nil {
307
- return err
331
+ return fmt . Errorf ( "writer: open: %w" , err )
308
332
}
309
333
defer db .Close ()
310
334
311
- err = db .BusyHandler (func (ctx context.Context , count int ) (retry bool ) {
312
- select {
313
- case <- time .After (time .Millisecond ):
314
- return true
315
- case <- ctx .Done ():
316
- return false
317
- }
318
- })
319
- if err != nil {
320
- return err
321
- }
322
-
323
- err = db .Exec (`CREATE TABLE IF NOT EXISTS users (id INT, name VARCHAR(10))` )
324
- if err != nil {
325
- return err
326
- }
327
-
328
335
err = db .Exec (`INSERT INTO users (id, name) VALUES (0, 'go'), (1, 'zig'), (2, 'whatever')` )
329
336
if err != nil {
330
- return err
337
+ return fmt . Errorf ( "writer: insert: %w" , err )
331
338
}
332
339
333
340
return db .Close ()
@@ -336,13 +343,13 @@ func testParallel(t testing.TB, name string, n int) {
336
343
reader := func () error {
337
344
db , err := sqlite3 .Open (name )
338
345
if err != nil {
339
- return err
346
+ return fmt . Errorf ( "reader: open: %w" , err )
340
347
}
341
348
defer db .Close ()
342
349
343
350
stmt , _ , err := db .Prepare (`SELECT id, name FROM users` )
344
351
if err != nil {
345
- return err
352
+ return fmt . Errorf ( "reader: select: %w" , err )
346
353
}
347
354
defer stmt .Close ()
348
355
@@ -351,15 +358,15 @@ func testParallel(t testing.TB, name string, n int) {
351
358
row ++
352
359
}
353
360
if err := stmt .Err (); err != nil {
354
- return err
361
+ return fmt . Errorf ( "reader: step: %w" , err )
355
362
}
356
363
if row % 3 != 0 {
357
- t .Errorf ("got %d rows, want multiple of 3" , row )
364
+ return fmt .Errorf ("reader: got %d rows, want multiple of 3" , row )
358
365
}
359
366
360
367
err = stmt .Close ()
361
368
if err != nil {
362
- return err
369
+ return fmt . Errorf ( "reader: close: %w" , err )
363
370
}
364
371
365
372
return db .Close ()
0 commit comments