check / check (push) Canceled after 0s
magic.DetectFormat returns an imagerequest.Format. ValidateMagicBytes returns the format it checked against the upstream Content-Type. The cache stores that format in the source's row, so a variant made from a cached source uses it too. The proxy hands it to Process, which still refuses input libvips would load with another loader, such as an SVG passing as AVIF. Format.MIMEType declares each MIME string once, and the fetcher, the cache's variant read and the Accept matching use it. magic.MIMEType, magic.ImageFormat, MIMEToImageFormat, magic's SVG detection, detectFormat, formatFromString, FormatToMIME and Result.InputFormat are gone. Model: opus-5-5
144 lines
3.3 KiB
Go
144 lines
3.3 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"log/slog"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
|
|
"sneak.berlin/go/pixa/internal/config"
|
|
)
|
|
|
|
// TestConcurrentWritesAllSucceed opens a database the way pixad does and
|
|
// writes to it from several goroutines at once, as one request's writes and
|
|
// the background eviction pass do. Every write must succeed, none failing
|
|
// with "database is locked", whether or not db_url already has parameters,
|
|
// and the parameters it has must still apply.
|
|
func TestConcurrentWritesAllSucceed(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
query string
|
|
wantJournalMode string
|
|
}{
|
|
{
|
|
name: "db_url without parameters",
|
|
query: "",
|
|
wantJournalMode: "delete",
|
|
},
|
|
{
|
|
name: "db_url with the WAL parameter",
|
|
query: "?_pragma=journal_mode(WAL)",
|
|
wantJournalMode: "wal",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
dbURL := "file:" + filepath.Join(t.TempDir(), "state.sqlite3") + tt.query
|
|
|
|
d := &Database{
|
|
log: slog.New(slog.DiscardHandler),
|
|
config: &config.Config{DBURL: dbURL},
|
|
}
|
|
|
|
err := d.connect(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("failed to connect to %q: %v", dbURL, err)
|
|
}
|
|
|
|
t.Cleanup(func() { _ = d.db.Close() })
|
|
|
|
writeConcurrently(t, d.db)
|
|
|
|
var journalMode string
|
|
|
|
err = d.db.QueryRowContext(t.Context(), "PRAGMA journal_mode").
|
|
Scan(&journalMode)
|
|
if err != nil {
|
|
t.Fatalf("failed to read the journal mode: %v", err)
|
|
}
|
|
|
|
if journalMode != tt.wantJournalMode {
|
|
t.Errorf("journal mode = %q, want %q", journalMode, tt.wantJournalMode)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// writeConcurrently runs writeLikeOneRequest from several goroutines at once
|
|
// and checks that every write was made.
|
|
func writeConcurrently(t *testing.T, db *sql.DB) {
|
|
t.Helper()
|
|
|
|
const (
|
|
writers = 4
|
|
requestsEach = 20
|
|
totalRequests = writers * requestsEach
|
|
)
|
|
|
|
ctx := t.Context()
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for writer := range writers {
|
|
wg.Go(func() {
|
|
for request := range requestsEach {
|
|
key := fmt.Sprintf("%d-%d", writer, request)
|
|
|
|
err := writeLikeOneRequest(ctx, db, key)
|
|
if err != nil {
|
|
t.Errorf("writer %d: %v", writer, err)
|
|
|
|
return
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
var sources int
|
|
|
|
err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM source_content`).
|
|
Scan(&sources)
|
|
if err != nil {
|
|
t.Fatalf("failed to count the writes: %v", err)
|
|
}
|
|
|
|
if sources != totalRequests {
|
|
t.Errorf("%d source_content rows, want %d", sources, totalRequests)
|
|
}
|
|
}
|
|
|
|
// writeLikeOneRequest makes the writes one request and the eviction pass
|
|
// make: it stores a source, records a transformed image and deletes that
|
|
// record again.
|
|
func writeLikeOneRequest(ctx context.Context, db *sql.DB, key string) error {
|
|
_, err := db.ExecContext(ctx, `INSERT INTO source_content
|
|
(source_key, size_bytes, format) VALUES (?, 1, 'jpeg')`, key)
|
|
if err != nil {
|
|
return fmt.Errorf("storing a source: %w", err)
|
|
}
|
|
|
|
_, err = db.ExecContext(ctx, `INSERT INTO variant_content
|
|
(cache_key, size_bytes) VALUES (?, 1)`, key)
|
|
if err != nil {
|
|
return fmt.Errorf("recording a transformed image: %w", err)
|
|
}
|
|
|
|
_, err = db.ExecContext(ctx,
|
|
`DELETE FROM variant_content WHERE cache_key = ?`, key)
|
|
if err != nil {
|
|
return fmt.Errorf("evicting a transformed image: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|