check / check (push) Canceled after 0s
The new package internal/imageproxy holds Proxy, which was imgcache.Service, with its Config, ImageResponse, CacheStatus and errors. imageproxy.New makes and registers the counters of upstream fetches and transforms. internal/imgcache holds the cache only, with its hit and miss counters, and imgcache.CacheConfig is imgcache.Config. The cache reads and writes each kind it keeps with one pair of methods: Variant and PutVariant, Source and PutSource, Failure and PutFailure. Source replaces LookupSource followed by GetSourceContent. imgcache.go and module.go are gone. The proxy's tests moved with it. The cache's tests check that the reconciliation pass removes an empty or cut file and its row. The proxy's tests remove them by hand, then check that Get fetches or converts again. Model: opus-5-5
136 lines
4.1 KiB
Go
136 lines
4.1 KiB
Go
package imgcache
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"sneak.berlin/go/pixa/internal/imagerequest"
|
|
)
|
|
|
|
// TestPutSource_StoresIdenticalBytesOfTwoURLsTwice stores the same bytes
|
|
// for two source URLs and checks that each URL has a source_content row of
|
|
// its own and a file of its own, named by its source key under
|
|
// cache/sources/<ab>/<cd>/.
|
|
func TestPutSource_StoresIdenticalBytesOfTwoURLsTwice(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cache, stateDir := newEvictionTestCache(t, 1<<30)
|
|
content := bytes.Repeat([]byte{0x5A}, 400)
|
|
|
|
keys := map[string]imagerequest.SourceKey{
|
|
"/a.jpg": storeEvictionTestSource(t, cache, testHostCDN, "/a.jpg", content),
|
|
"/b.jpg": storeEvictionTestSource(t, cache, testHostCDN, "/b.jpg", content),
|
|
}
|
|
t.Logf("source keys: %v", keys)
|
|
|
|
if keys["/a.jpg"] == keys["/b.jpg"] {
|
|
t.Fatalf("both source URLs got the key %s", keys["/a.jpg"])
|
|
}
|
|
|
|
for sourcePath, key := range keys {
|
|
rows := countRows(t, cache,
|
|
`SELECT COUNT(*) FROM source_content WHERE source_key = ?`, string(key))
|
|
if rows != 1 {
|
|
t.Errorf("%s has %d source_content rows, want 1", sourcePath, rows)
|
|
}
|
|
|
|
name := string(key)
|
|
file := filepath.Join(stateDir, "cache", "sources", name[0:2], name[2:4], name)
|
|
|
|
stored, err := os.ReadFile(file) //nolint:gosec // path in the test's state dir
|
|
if err != nil || !bytes.Equal(stored, content) {
|
|
t.Errorf("file of %s at %s holds %d bytes (error %v), want the %d stored",
|
|
sourcePath, file, len(stored), err, len(content))
|
|
}
|
|
}
|
|
|
|
usage, err := cache.UsageBytes(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("UsageBytes() error = %v", err)
|
|
}
|
|
|
|
if usage != 800 {
|
|
t.Errorf("UsageBytes() = %d, want 800, the 400 bytes once for each URL", usage)
|
|
}
|
|
}
|
|
|
|
// TestEvictSourceBlob_LeavesSourceWithIdenticalBytesServed stores the same
|
|
// bytes for two source URLs and evicts the first. Its row and file must be
|
|
// gone, and the second must still be found and read from the cache.
|
|
func TestEvictSourceBlob_LeavesSourceWithIdenticalBytesServed(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cache, _ := newEvictionTestCache(t, 1<<30)
|
|
content := bytes.Repeat([]byte{0x5B}, 400)
|
|
|
|
evicted := storeEvictionTestSource(t, cache, testHostCDN, "/evicted.jpg", content)
|
|
storeEvictionTestSource(t, cache, testHostCDN, "/kept.jpg", content)
|
|
|
|
err := cache.evictSourceBlob(t.Context(), evicted)
|
|
if err != nil {
|
|
t.Fatalf("evictSourceBlob() error = %v", err)
|
|
}
|
|
|
|
if cache.srcContent.Exists(evicted) {
|
|
t.Error("the evicted source's file is still on disk")
|
|
}
|
|
|
|
if n := countRows(t, cache,
|
|
`SELECT COUNT(*) FROM source_content WHERE source_key = ?`, string(evicted),
|
|
); n != 0 {
|
|
t.Errorf("the evicted source still has %d source_content rows", n)
|
|
}
|
|
|
|
reader, _, err := cache.Source(t.Context(),
|
|
imagerequest.Source{Host: testHostCDN, Path: "/kept.jpg"})
|
|
t.Logf("Source() of the kept source: error %v", err)
|
|
|
|
if err != nil {
|
|
t.Fatalf("Source() of the kept source error = %v; want it cached", err)
|
|
}
|
|
|
|
defer func() { _ = reader.Close() }()
|
|
|
|
served, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
t.Fatalf("reading the kept source: %v", err)
|
|
}
|
|
|
|
if !bytes.Equal(served, content) {
|
|
t.Errorf("the kept source reads as %d bytes, want the %d stored",
|
|
len(served), len(content))
|
|
}
|
|
}
|
|
|
|
// TestPutSource_FailedRowWriteLeavesNoFile drops source_content, so that
|
|
// PutSource cannot write the source's row, and checks that the store fails
|
|
// and leaves no file under the source's key.
|
|
func TestPutSource_FailedRowWriteLeavesNoFile(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cache, _ := newEvictionTestCache(t, 1<<30)
|
|
|
|
_, err := cache.db.ExecContext(t.Context(), `DROP TABLE source_content`)
|
|
if err != nil {
|
|
t.Fatalf("failed to drop source_content: %v", err)
|
|
}
|
|
|
|
req := &imagerequest.Request{
|
|
Source: imagerequest.Source{Host: testHostCDN, Path: testPathCat},
|
|
}
|
|
key, err := cache.PutSource(t.Context(), req.Source,
|
|
bytes.NewReader([]byte("source bytes")))
|
|
t.Logf("PutSource() = %q, error = %v", key, err)
|
|
|
|
if err == nil {
|
|
t.Error("PutSource() error = nil, want the failed row write")
|
|
}
|
|
|
|
if cache.srcContent.Exists(req.Source.Key()) {
|
|
t.Error("the source's file is on disk although its row was not written")
|
|
}
|
|
}
|