Skip to content
quarry Artifact [7346a18654]
Login

Artifact [7346a18654]

Artifact 7346a18654917e680d1d60901306390c577d1c45d7ec2270ccd8d5a443c803c0:


//go:build !js && !wasip1

package obsduck

import (
	"database/sql"
	"net/url"
	"testing"

	"craftdesign.group/quarry/internal/obs"
)

// settingsOf reads the settings every handle must carry.
func settingsOf(t *testing.T, db *sql.DB) map[string]string {
	t.Helper()
	got := map[string]string{}
	for _, name := range []string{"memory_limit", "threads", "temp_directory", "preserve_insertion_order"} {
		var v string
		if err := db.QueryRow(`SELECT CAST(current_setting('` + name + `') AS VARCHAR)`).Scan(&v); err != nil {
			t.Fatal(err)
		}
		got[name] = v
	}
	return got
}

// The store is bounded: a cap on its memory, a few threads, a spill
// directory beside the file, and no insertion-order bookkeeping. Every
// pooled connection and every reopened handle carries them, because they
// are set when the database is opened, not on a connection.
func TestOpenBoundsTheStore(t *testing.T) {
	s, path := reopenSink(t, 4)

	ref, err := sql.Open("duckdb", ":memory:?memory_limit="+url.QueryEscape(memoryLimit))
	if err != nil {
		t.Skipf("duckdb unavailable: %v", err)
	}
	defer ref.Close()
	var wantLimit string
	if err := ref.QueryRow(`SELECT current_setting('memory_limit')`).Scan(&wantLimit); err != nil {
		t.Fatal(err)
	}

	check := func(when string) {
		t.Helper()
		// Hold two connections at once, so the pool has to open a second.
		c1, err := s.db.Conn(t.Context())
		if err != nil {
			t.Fatal(err)
		}
		defer c1.Close()
		got := settingsOf(t, s.db)
		if got["memory_limit"] != wantLimit {
			t.Errorf("FAIL: %s memory_limit = %q, want %q", when, got["memory_limit"], wantLimit)
		}
		if got["threads"] != "2" {
			t.Errorf("FAIL: %s threads = %q", when, got["threads"])
		}
		if got["temp_directory"] != path+".tmp" {
			t.Errorf("FAIL: %s temp_directory = %q, want %q", when, got["temp_directory"], path+".tmp")
		}
		if got["preserve_insertion_order"] != "false" {
			t.Errorf("FAIL: %s preserve_insertion_order = %q", when, got["preserve_insertion_order"])
		}
	}
	check("open")
	if err := s.Reopen(); err != nil {
		t.Fatal(err)
	}
	check("reopen")
	if !t.Failed() {
		t.Log("ok")
	}
}

// The window tables Traffic builds are the connection's, and a pooled
// connection outlives the read: left alone they held the last window's
// raw rows (the 30-day one is the largest) until the next warm replaced
// them. They are dropped when the read ends.
func TestTrafficLeavesNoWindowTablesBehind(t *testing.T) {
	s, now := trafficSink(t)
	if _, err := s.Traffic(obs.TrafficQuery{Now: now, Range: obs.RangeNamed("24h")}); err != nil {
		t.Fatal(err)
	}
	conn, err := s.db.Conn(t.Context())
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()
	rows, err := conn.QueryContext(t.Context(), `SELECT table_name FROM duckdb_tables() WHERE temporary AND table_name IN ('hcur', 'hprev')`)
	if err != nil {
		t.Fatal(err)
	}
	defer rows.Close()
	for rows.Next() {
		var name string
		rows.Scan(&name)
		t.Errorf("FAIL: temp table %s still held after the read", name)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}