check / check (push) Successful in 1m58s
`internal/ptr` asks Google, Cloudflare, AdGuard and Quad9 in turn, 2 seconds in all per address. It keeps answers in memory and writes them through to `cache.db`, opened by the new `internal/cachedb`. A reserved address is not sent to the resolvers. The cache holds at most 250,000 addresses. Judgement calls: - Each resolver gets an equal share of the time left, so a hung resolver does not use up the 2 seconds. - The negative TTL is the smaller of the SOA minimum and the SOA's own TTL (RFC 2308). - Names that are not host names are dropped, as `net.Resolver.LookupAddr` does. Model: opus-5-5
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package cachedb
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// TestOpenCreatesTheFileInWALMode opens a new database, and then opens it
|
|
// again, as a restart does.
|
|
func TestOpenCreatesTheFileInWALMode(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
dir := t.TempDir()
|
|
|
|
for range 2 {
|
|
db, err := Open(t.Context(), dir)
|
|
if err != nil {
|
|
t.Fatalf("Open: %v", err)
|
|
}
|
|
|
|
var mode string
|
|
|
|
err = db.QueryRowContext(t.Context(), "PRAGMA journal_mode").Scan(&mode)
|
|
if err != nil {
|
|
t.Fatalf("reading the journal mode: %v", err)
|
|
}
|
|
|
|
if mode != "wal" {
|
|
t.Errorf("journal mode %q, want wal", mode)
|
|
}
|
|
|
|
var rows int
|
|
|
|
err = db.QueryRowContext(t.Context(),
|
|
"SELECT COUNT(*) FROM hostnames").Scan(&rows)
|
|
if err != nil {
|
|
t.Fatalf("reading the hostnames table: %v", err)
|
|
}
|
|
|
|
err = db.Close()
|
|
if err != nil {
|
|
t.Fatalf("Close: %v", err)
|
|
}
|
|
}
|
|
|
|
_, err := os.Stat(filepath.Join(dir, fileName))
|
|
if err != nil {
|
|
t.Errorf("the file is not in the state directory: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestOpenFailsWhenTheDirectoryDoesNotExist checks that a state directory that
|
|
// is not there is reported, not ignored.
|
|
func TestOpenFailsWhenTheDirectoryDoesNotExist(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db, err := Open(t.Context(), filepath.Join(t.TempDir(), "missing"))
|
|
if err == nil {
|
|
_ = db.Close()
|
|
|
|
t.Fatal("Open succeeded in a directory that does not exist")
|
|
}
|
|
}
|