Files
sqlite3/tests/parallel/parallel_test.go

366 lines
7.0 KiB
Go
Raw Normal View History

2023-02-07 03:11:59 +00:00
package tests
2023-01-26 00:05:52 +00:00
import (
2024-09-27 12:33:57 +01:00
"context"
2024-08-30 09:32:30 +01:00
"errors"
2023-02-13 15:16:58 +00:00
"io"
2024-08-30 09:32:30 +01:00
"log"
2024-08-05 21:25:47 +01:00
"net/url"
2023-01-26 00:05:52 +00:00
"os"
2023-02-13 14:49:15 +00:00
"os/exec"
2023-01-26 00:05:52 +00:00
"path/filepath"
"testing"
2024-02-02 23:41:34 +00:00
"time"
2023-01-27 01:45:38 +00:00
"golang.org/x/sync/errgroup"
2023-01-26 00:05:52 +00:00
"github.com/ncruces/go-sqlite3"
_ "github.com/ncruces/go-sqlite3/embed"
2024-06-02 10:33:20 +01:00
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
"github.com/ncruces/go-sqlite3/vfs"
2024-04-18 01:39:47 +01:00
_ "github.com/ncruces/go-sqlite3/vfs/adiantum"
2023-11-16 01:16:38 +00:00
"github.com/ncruces/go-sqlite3/vfs/memdb"
2024-10-19 08:58:55 +01:00
_ "github.com/ncruces/go-sqlite3/vfs/xts"
2023-01-26 00:05:52 +00:00
)
2024-08-30 09:32:30 +01:00
func TestMain(m *testing.M) {
sqlite3.AutoExtension(func(c *sqlite3.Conn) error {
return c.ConfigLog(func(code sqlite3.ExtendedErrorCode, msg string) {
// Having to do journal recovery is unexpected.
if errors.Is(code, sqlite3.NOTICE) {
log.Panicf("%v (%d): %s", code, code, msg)
} else {
log.Printf("%v (%d): %s", code, code, msg)
}
})
})
m.Run()
}
2024-04-18 01:39:47 +01:00
func Test_parallel(t *testing.T) {
2024-05-02 23:22:43 +01:00
if !vfs.SupportsFileLocking {
t.Skip("skipping without locks")
}
2023-03-01 13:27:50 +00:00
var iter int
if testing.Short() {
iter = 1000
} else {
iter = 5000
}
2023-05-26 04:59:54 +01:00
name := "file:" +
2024-08-05 21:25:47 +01:00
filepath.ToSlash(filepath.Join(t.TempDir(), "test.db")) +
2023-05-26 04:59:54 +01:00
"?_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(truncate)" +
"&_pragma=synchronous(off)"
testParallel(t, name, iter)
testIntegrity(t, name)
}
2024-04-18 01:39:47 +01:00
func Test_wal(t *testing.T) {
if !vfs.SupportsSharedMemory {
t.Skip("skipping without shared memory")
}
2024-08-30 01:27:57 +01:00
var iter int
if testing.Short() {
iter = 1000
} else {
iter = 2500
}
name := "file:" +
2024-08-05 21:25:47 +01:00
filepath.ToSlash(filepath.Join(t.TempDir(), "test.db")) +
"?_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(wal)" +
"&_pragma=synchronous(off)"
2024-08-30 01:27:57 +01:00
testParallel(t, name, iter)
testIntegrity(t, name)
}
2024-04-18 01:39:47 +01:00
func Test_memdb(t *testing.T) {
2023-05-26 04:59:54 +01:00
var iter int
if testing.Short() {
iter = 1000
} else {
iter = 5000
}
2024-08-05 21:25:47 +01:00
name := memdb.TestDB(t, url.Values{
"_pragma": {"busy_timeout(10000)"},
})
2023-03-01 13:27:50 +00:00
testParallel(t, name, iter)
2023-02-13 15:57:32 +00:00
testIntegrity(t, name)
2023-02-13 14:49:15 +00:00
}
2024-04-18 01:39:47 +01:00
func Test_adiantum(t *testing.T) {
2024-05-02 23:22:43 +01:00
if !vfs.SupportsFileLocking {
t.Skip("skipping without locks")
}
2024-04-18 01:39:47 +01:00
var iter int
if testing.Short() {
2024-10-19 08:58:55 +01:00
iter = 500
2024-04-18 01:39:47 +01:00
} else {
2024-10-19 08:58:55 +01:00
iter = 2500
2024-04-18 01:39:47 +01:00
}
name := "file:" +
filepath.ToSlash(filepath.Join(t.TempDir(), "test.db")) +
"?vfs=adiantum" +
2024-08-05 21:25:47 +01:00
"&_pragma=hexkey(e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855)" +
"&_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(truncate)" +
"&_pragma=synchronous(off)"
2024-04-18 01:39:47 +01:00
testParallel(t, name, iter)
testIntegrity(t, name)
}
2024-10-19 08:58:55 +01:00
func Test_xts(t *testing.T) {
if !vfs.SupportsFileLocking {
t.Skip("skipping without locks")
}
var iter int
if testing.Short() {
iter = 500
} else {
iter = 2500
}
name := "file:" +
filepath.ToSlash(filepath.Join(t.TempDir(), "test.db")) +
"?vfs=xts" +
"&_pragma=hexkey(e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855)" +
"&_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(truncate)" +
"&_pragma=synchronous(off)"
testParallel(t, name, iter)
testIntegrity(t, name)
}
2023-02-13 14:49:15 +00:00
func TestMultiProcess(t *testing.T) {
2024-05-02 23:22:43 +01:00
if !vfs.SupportsFileLocking {
t.Skip("skipping without locks")
}
2023-02-13 14:49:15 +00:00
if testing.Short() {
2023-03-01 12:16:36 +00:00
t.Skip("skipping in short mode")
2023-02-13 14:49:15 +00:00
}
2023-05-26 04:59:54 +01:00
file := filepath.Join(t.TempDir(), "test.db")
t.Setenv("TestMultiProcess_dbfile", file)
2024-08-05 21:25:47 +01:00
name := "file:" + filepath.ToSlash(file) +
2023-05-26 04:59:54 +01:00
"?_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(truncate)" +
"&_pragma=synchronous(off)"
2023-02-13 15:57:32 +00:00
2024-09-04 18:44:09 +01:00
exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(exe, append(os.Args[1:], "-test.v", "-test.run=TestChildProcess")...)
2023-02-13 15:16:58 +00:00
out, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
2023-02-13 14:49:15 +00:00
if err := cmd.Start(); err != nil {
2023-01-27 01:45:38 +00:00
t.Fatal(err)
}
2023-02-13 15:16:58 +00:00
var buf [3]byte
// Wait for child to start.
2023-12-12 02:03:02 +00:00
if _, err := io.ReadFull(out, buf[:]); err != nil {
2023-02-13 15:16:58 +00:00
t.Fatal(err)
2023-12-12 02:03:02 +00:00
} else if str := string(buf[:]); str != "===" {
t.Fatal(str)
2023-02-13 15:16:58 +00:00
}
2023-02-13 15:57:32 +00:00
testParallel(t, name, 1000)
if err := cmd.Wait(); err != nil {
2023-02-20 13:30:01 +00:00
t.Error(err)
2023-02-13 15:57:32 +00:00
}
testIntegrity(t, name)
2023-02-13 14:49:15 +00:00
}
func TestChildProcess(t *testing.T) {
2023-05-26 04:59:54 +01:00
file := os.Getenv("TestMultiProcess_dbfile")
if file == "" || testing.Short() {
2023-02-22 14:19:56 +00:00
t.SkipNow()
2023-02-13 14:49:15 +00:00
}
2024-08-05 21:25:47 +01:00
name := "file:" + filepath.ToSlash(file) +
2023-05-26 04:59:54 +01:00
"?_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(truncate)" +
"&_pragma=synchronous(off)"
2023-02-13 15:57:32 +00:00
testParallel(t, name, 1000)
2023-02-13 14:49:15 +00:00
}
2024-06-11 10:52:07 +01:00
func Benchmark_parallel(b *testing.B) {
if !vfs.SupportsSharedMemory {
b.Skip("skipping without shared memory")
}
sqlite3.Initialize()
b.ResetTimer()
name := "file:" +
filepath.Join(b.TempDir(), "test.db") +
"?_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(truncate)" +
"&_pragma=synchronous(off)"
testParallel(b, name, b.N)
}
func Benchmark_wal(b *testing.B) {
if !vfs.SupportsSharedMemory {
b.Skip("skipping without shared memory")
}
sqlite3.Initialize()
b.ResetTimer()
name := "file:" +
filepath.Join(b.TempDir(), "test.db") +
"?_pragma=busy_timeout(10000)" +
"&_pragma=journal_mode(wal)" +
"&_pragma=synchronous(off)"
testParallel(b, name, b.N)
}
2024-04-18 01:39:47 +01:00
func Benchmark_memdb(b *testing.B) {
2024-05-03 12:38:40 +01:00
sqlite3.Initialize()
b.ResetTimer()
2024-08-05 21:25:47 +01:00
name := memdb.TestDB(b, url.Values{
"_pragma": {"busy_timeout(10000)"},
})
2023-11-16 01:16:38 +00:00
testParallel(b, name, b.N)
}
func testParallel(t testing.TB, name string, n int) {
2023-01-27 01:45:38 +00:00
writer := func() error {
2023-02-13 15:57:32 +00:00
db, err := sqlite3.Open(name)
2023-01-27 01:45:38 +00:00
if err != nil {
return err
}
defer db.Close()
2024-09-27 12:33:57 +01:00
err = db.BusyHandler(func(ctx context.Context, count int) (retry bool) {
select {
case <-time.After(time.Millisecond):
return true
case <-ctx.Done():
return false
}
2024-02-02 23:41:34 +00:00
})
if err != nil {
return err
}
2023-01-27 01:45:38 +00:00
err = db.Exec(`CREATE TABLE IF NOT EXISTS users (id INT, name VARCHAR(10))`)
if err != nil {
2023-02-08 00:00:53 +00:00
return err
2023-01-27 01:45:38 +00:00
}
2023-02-27 12:07:48 +00:00
err = db.Exec(`INSERT INTO users (id, name) VALUES (0, 'go'), (1, 'zig'), (2, 'whatever')`)
2023-01-27 01:45:38 +00:00
if err != nil {
2023-02-08 00:00:53 +00:00
return err
2023-01-27 01:45:38 +00:00
}
return db.Close()
}
reader := func() error {
2023-02-13 15:57:32 +00:00
db, err := sqlite3.Open(name)
2023-01-27 01:45:38 +00:00
if err != nil {
return err
}
defer db.Close()
stmt, _, err := db.Prepare(`SELECT id, name FROM users`)
if err != nil {
return err
}
2023-02-13 15:57:32 +00:00
defer stmt.Close()
2023-01-27 01:45:38 +00:00
row := 0
for stmt.Step() {
row++
}
if err := stmt.Err(); err != nil {
return err
}
if row%3 != 0 {
t.Errorf("got %d rows, want multiple of 3", row)
}
err = stmt.Close()
if err != nil {
return err
}
return db.Close()
}
2023-02-13 14:49:15 +00:00
err := writer()
2023-01-27 01:45:38 +00:00
if err != nil {
t.Fatal(err)
}
var group errgroup.Group
2023-03-01 13:27:50 +00:00
group.SetLimit(6)
2023-02-13 14:49:15 +00:00
for i := 0; i < n; i++ {
2023-01-27 01:45:38 +00:00
if i&7 != 7 {
group.Go(reader)
} else {
group.Go(writer)
}
}
err = group.Wait()
if err != nil {
2023-02-23 02:22:57 +00:00
t.Error(err)
2023-01-27 01:45:38 +00:00
}
}
2023-02-13 15:57:32 +00:00
2023-11-16 01:16:38 +00:00
func testIntegrity(t testing.TB, name string) {
2023-02-13 15:57:32 +00:00
db, err := sqlite3.Open(name)
if err != nil {
t.Fatal(err)
}
defer db.Close()
test := `PRAGMA integrity_check`
if testing.Short() {
test = `PRAGMA quick_check`
}
stmt, _, err := db.Prepare(test)
if err != nil {
t.Fatal(err)
}
defer stmt.Close()
for stmt.Step() {
if row := stmt.ColumnText(0); row != "ok" {
t.Error(row)
}
}
if err := stmt.Err(); err != nil {
t.Fatal(err)
}
err = stmt.Close()
if err != nil {
t.Fatal(err)
}
err = db.Close()
if err != nil {
t.Fatal(err)
}
}