Skip to content
quarry Artifact [9457a6243d]
Login

Artifact [9457a6243d]

Artifact 9457a6243d20868b78cd00063ceab5a4813b3e25995d3f8775f7b5fb51110de9:


package sqlmem

import (
	"context"
	"database/sql"
	"path/filepath"
	"sync/atomic"
	"testing"

	"github.com/ncruces/go-sqlite3"
	"github.com/ncruces/go-sqlite3/driver"
	_ "github.com/ncruces/go-sqlite3/embed"
)

func open(t *testing.T, limit *int64, retired *atomic.Int64) *sql.DB {
	t.Helper()
	dsn := "file:" + filepath.Join(t.TempDir(), "t.db") + "?_pragma=temp_store(MEMORY)&_pragma=cache_size(-4000)"
	c, err := (&driver.SQLite{}).OpenConnector(dsn)
	if err != nil {
		t.Fatalf("FAIL: open: %v", err)
	}
	db := sql.OpenDB(Connector{Inner: c, Limit: limit, Retired: retired})
	db.SetMaxIdleConns(1)
	t.Cleanup(func() { db.Close() })
	return db
}

// committedOf is the memory of the one idle connection in db's pool.
func committedOf(t *testing.T, db *sql.DB) int64 {
	t.Helper()
	c, err := db.Conn(context.Background())
	if err != nil {
		t.Fatalf("FAIL: conn: %v", err)
	}
	defer c.Close()
	var n int64
	var ok bool
	if err := c.Raw(func(dc any) error {
		n, ok = Committed(dc.(interface{ Raw() *sqlite3.Conn }).Raw())
		return nil
	}); err != nil {
		t.Fatalf("FAIL: raw: %v", err)
	}
	if !ok {
		t.Fatalf("FAIL: the driver's memory field is not where Committed reads it; a driver upgrade moved it")
	}
	return n
}

// heavy sorts enough rows in memory to grow the connection well past a
// megabyte or two.
func heavy(t *testing.T, db *sql.DB) {
	t.Helper()
	var n int
	q := `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)`
	if err := db.QueryRow(q).Scan(&n); err != nil {
		t.Fatalf("FAIL: heavy query: %v", err)
	}
}

// The measure is real: a heavy query grows what Committed reports.
func TestCommittedReadsTheDriver(t *testing.T) {
	off := int64(0) // no limit: the connection is kept so it can be measured
	db := open(t, &off, nil)
	if _, err := db.Exec(`CREATE TABLE t(x)`); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	before := committedOf(t, db)
	heavy(t, db)
	after := committedOf(t, db)
	if before <= 0 {
		t.Errorf("FAIL: a fresh connection reports %d bytes", before)
	}
	if after < before+(8<<20) {
		t.Errorf("FAIL: a heavy sort grew the connection from %d to %d, want at least 8 MiB more", before, after)
	}
	if !t.Failed() {
		t.Logf("ok: %d -> %d bytes", before, after)
	}
}

// A connection past the limit is retired when it is returned, and the
// next caller gets a small one; one under the limit is kept.
func TestAGrownConnectionIsRetired(t *testing.T) {
	limit := int64(8 << 20)
	var retired atomic.Int64
	db := open(t, &limit, &retired)
	if _, err := db.Exec(`CREATE TABLE t(x)`); err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	small := committedOf(t, db)
	if retired.Load() != 0 {
		t.Errorf("FAIL: a small connection was retired")
	}
	heavy(t, db)
	if got := retired.Load(); got != 1 {
		t.Errorf("FAIL: %d connections retired after a heavy query, want 1", got)
	}
	if n, b := Retired(); n < 1 || b <= limit {
		t.Errorf("FAIL: the totals say %d connections, %d bytes", n, b)
	}
	if after := committedOf(t, db); after > limit {
		t.Errorf("FAIL: the pool still hands out a connection of %d bytes, limit %d", after, limit)
	} else if after > small*2 {
		t.Errorf("FAIL: the next connection holds %d bytes where a fresh one held %d", after, small)
	}
	// Work still runs on the new connection, and inside a transaction.
	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.Errorf("FAIL: insert: %v", err)
	}
	if err := tx.Commit(); err != nil {
		t.Errorf("FAIL: commit: %v", err)
	}
	var n int
	if err := db.QueryRow(`SELECT count(*) FROM t`).Scan(&n); err != nil || n != 1 {
		t.Errorf("FAIL: read back %d rows, err %v", n, err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A connection that is not the driver's is passed through untouched.
func TestLeanLeavesOtherConnectionsAlone(t *testing.T) {
	limit := int64(1)
	db := open(t, &limit, nil)
	c, err := db.Conn(context.Background())
	if err != nil {
		t.Fatalf("FAIL: %v", err)
	}
	defer c.Close()
	if err := c.Raw(func(dc any) error {
		if _, ok := dc.(interface{ IsValid() bool }); !ok {
			t.Errorf("FAIL: the pool's connection does not validate")
		}
		return nil
	}); err != nil {
		t.Errorf("FAIL: %v", err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}