package sqlmem
import (
"bytes"
"context"
"database/sql"
"database/sql/driver"
"errors"
"log"
"os"
"path/filepath"
"strings"
"testing"
"github.com/ncruces/go-sqlite3"
sqldriver "github.com/ncruces/go-sqlite3/driver"
)
// ceilingConnector gives every connection a memory ceiling, the way the
// forge's reader pool does.
type ceilingConnector struct {
driver.Connector
max int64
}
func (c ceilingConnector) Connect(ctx context.Context) (driver.Conn, error) {
return c.Connector.Connect(sqlite3.WithMaxMemory(ctx, c.max))
}
const poolSlots = 2
// ceilingDB is a pool of poolSlots connections with a 16 MiB ceiling.
func ceilingDB(t *testing.T) *sql.DB {
t.Helper()
dsn := "file:" + filepath.Join(t.TempDir(), "t.db") + "?_pragma=temp_store(MEMORY)&_pragma=cache_size(-4000)"
c, err := (&sqldriver.SQLite{}).OpenConnector(dsn)
if err != nil {
t.Fatalf("FAIL: open: %v", err)
}
off := int64(0)
db := sql.OpenDB(Connector{Inner: ceilingConnector{c, 16 << 20}, Limit: &off})
db.SetMaxOpenConns(poolSlots)
t.Cleanup(func() { db.Close() })
return db
}
// pastCeiling needs more memory than the ceiling allows.
const pastCeiling = `WITH RECURSIVE s(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i < 400000)
SELECT count(*) FROM (SELECT i, hex(randomblob(40)) AS r FROM s ORDER BY r)`
func wantPanicErr(t *testing.T, what string, err error) {
t.Helper()
var pe *PanicError
switch {
case err == nil:
t.Errorf("FAIL: %s: no error", what)
case errors.Is(err, driver.ErrBadConn):
t.Errorf("FAIL: %s: got ErrBadConn, which database/sql retries: %v", what, err)
case !errors.As(err, &pe):
t.Errorf("FAIL: %s: not a *PanicError: %v", what, err)
case !strings.Contains(err.Error(), "out of memory"):
t.Errorf("FAIL: %s: the error does not name the memory ceiling: %v", what, err)
}
}
func wantFree(t *testing.T, db *sql.DB, what string) {
t.Helper()
if s := db.Stats(); s.InUse != 0 {
t.Errorf("FAIL: %s: %d connections still checked out", what, s.InUse)
}
var one int
if err := db.QueryRow(`SELECT 1`).Scan(&one); err != nil || one != 1 {
t.Errorf("FAIL: %s: next query: %d, %v", what, one, err)
}
}
// A query past the ceiling is an error to its caller, its connection is
// dropped, and doing it more often than the pool has slots never drains it.
func TestPanickingQueryFreesItsSlot(t *testing.T) {
db := ceilingDB(t)
before := Panics()
const rounds = poolSlots + 2
for range rounds {
var n int
wantPanicErr(t, "query", db.QueryRow(pastCeiling).Scan(&n))
wantFree(t, db, "after query")
}
// An Exec that panics is one error too, not a retry: one panic each.
_, err := db.Exec(`CREATE TABLE big AS ` + strings.Replace(pastCeiling, "SELECT count(*) FROM", "SELECT * FROM", 1))
wantPanicErr(t, "exec", err)
wantFree(t, db, "after exec")
if got := Panics() - before; got != rounds+1 {
t.Errorf("FAIL: the counter moved %d, want %d (one per panic, none retried)", got, rounds+1)
}
if s := db.Stats(); s.OpenConnections > poolSlots {
t.Errorf("FAIL: %d connections open, bound %d", s.OpenConnections, poolSlots)
}
if !t.Failed() {
t.Log("ok")
}
}
// The panic can come while rows are being read.
func TestPanicWhileIteratingFreesItsSlot(t *testing.T) {
db := ceilingDB(t)
before := Panics()
for range poolSlots + 1 {
rows, err := db.Query(pastCeiling)
if err != nil {
t.Fatalf("FAIL: query: %v", err)
}
for rows.Next() {
}
wantPanicErr(t, "rows", rows.Err())
if err := rows.Close(); err != nil {
t.Errorf("FAIL: close after a panic: %v", err)
}
wantFree(t, db, "after rows")
}
if got := Panics() - before; got != poolSlots+1 {
t.Errorf("FAIL: the counter moved %d, want %d", got, poolSlots+1)
}
if !t.Failed() {
t.Log("ok")
}
}
// A panic inside a transaction ends it: nothing it did survives, it
// cannot commit, and its slot is freed.
func TestPanicInTransactionRollsBackAndFreesItsSlot(t *testing.T) {
db := ceilingDB(t)
if _, err := db.Exec(`CREATE TABLE t(x)`); err != nil {
t.Fatalf("FAIL: %v", err)
}
for range poolSlots + 1 {
tx, err := db.Begin()
if err != nil {
t.Fatalf("FAIL: begin: %v", err)
}
if _, err := tx.Exec(`INSERT INTO t VALUES (1)`); err != nil {
t.Fatalf("FAIL: insert: %v", err)
}
var n int
wantPanicErr(t, "in tx", tx.QueryRow(pastCeiling).Scan(&n))
// Later calls in the same transaction say why, and are never
// ErrBadConn, which database/sql would retry half-way.
_, err = tx.Exec(`INSERT INTO t VALUES (2)`)
var pe *PanicError
if !errors.As(err, &pe) || errors.Is(err, driver.ErrBadConn) {
t.Errorf("FAIL: a later Exec in the dead transaction gave %v, want one wrapping the *PanicError", err)
}
if err := tx.Commit(); err == nil {
t.Errorf("FAIL: a transaction whose connection panicked committed")
}
tx.Rollback()
wantFree(t, db, "after tx")
}
var n int
if err := db.QueryRow(`SELECT count(*) FROM t`).Scan(&n); err != nil || n != 0 {
t.Errorf("FAIL: %d rows survived the discarded transactions, err %v", n, err)
}
if !t.Failed() {
t.Log("ok")
}
}
type panicConn struct {
conn
with any
}
func (p panicConn) ExecContext(context.Context, string, []driver.NamedValue) (driver.Result, error) {
panic(p.with)
}
// Any panic inside a guarded call is recovered, counted, logged once with
// its stack, and kills the connection; the split counters say which kind.
func TestAnyPanicInsideTheDriverIsRecoveredLoudly(t *testing.T) {
var logged bytes.Buffer
log.SetOutput(&logged)
t.Cleanup(func() { log.SetOutput(os.Stderr) })
var nilMap map[string]int
var rt any
func() {
defer func() { rt = recover() }()
nilMap["x"] = 1
}()
other := []any{"unreachable", errors.New("plain"), rt, &sqlite3.Error{}}
for _, v := range other {
l := &lean{conn: panicConn{with: v}}
oomBefore, otherBefore := PanicsOOM(), PanicsOther()
logged.Reset()
_, err := l.ExecContext(context.Background(), "x", nil)
var pe *PanicError
if !errors.As(err, &pe) || errors.Is(err, driver.ErrBadConn) {
t.Errorf("FAIL: %T gave %v, want a *PanicError", v, err)
}
if l.IsValid() {
t.Errorf("FAIL: %T: a connection that panicked is valid", v)
}
if PanicsOther()-otherBefore != 1 || PanicsOOM() != oomBefore {
t.Errorf("FAIL: %T: counters moved other %d oom %d", v, PanicsOther()-otherBefore, PanicsOOM()-oomBefore)
}
if n := strings.Count(logged.String(), "ERROR"); n != 1 || !strings.Contains(logged.String(), "goroutine") {
t.Errorf("FAIL: %T: logged %d times, want once with a stack: %q", v, n, logged.String())
}
logged.Reset()
if _, err := l.ExecContext(context.Background(), "x", nil); !errors.Is(err, driver.ErrBadConn) {
t.Errorf("FAIL: a call on a dead connection outside a transaction gave %v, want ErrBadConn (nothing ran, retry is safe)", err)
}
if logged.Len() != 0 {
t.Errorf("FAIL: a call on a dead connection logged again")
}
}
l := &lean{conn: panicConn{with: errors.New("sqlite3: out of memory")}}
oomBefore := PanicsOOM()
l.ExecContext(context.Background(), "x", nil)
if PanicsOOM()-oomBefore != 1 {
t.Errorf("FAIL: an out-of-memory panic did not move the OOM counter")
}
if !t.Failed() {
t.Log("ok")
}
}