check / check (push) Waiting to run
imgcache.FileStore is a directory of files named by a hex key, at <ab>/<cd>/<key>. It writes a file under a temp name, syncs it and renames it into place. It opens, reports the size of and deletes the file for a key. The cache keeps its sources in one FileStore and its variants in another, so ContentStorage and VariantStorage are gone. FileStore has no Exists: the reconciliation pass checks a variant's file with Size, and the tests look for the file on disk. Cache.Variant reads the content type from the file's first bytes itself. storage.go is now filestore.go, and the unused testdata/red.avif is gone. Model: opus-5-5
138 lines
4.1 KiB
Go
138 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)
|
|
}
|
|
|
|
_, err = os.Stat(cache.srcContent.keyToPath(string(evicted)))
|
|
if err == nil {
|
|
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")
|
|
}
|
|
|
|
_, err = os.Stat(cache.srcContent.keyToPath(string(req.Source.Key())))
|
|
if err == nil {
|
|
t.Error("the source's file is on disk although its row was not written")
|
|
}
|
|
}
|