Don't cache transcoded files if the request was cancelled (#2041)
* Don't cache transcoded files if the request was cancelled (or there was a transcoding error) * Add context to logs * Simplify Wait error handling * Fix flaky test * Change log level for "populating cache" error message * Small cleanups
This commit is contained in:
Vendored
+82
-10
@@ -2,12 +2,14 @@ package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/navidrome/navidrome/conf"
|
||||
"github.com/navidrome/navidrome/conf/configtest"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
@@ -15,16 +17,18 @@ import (
|
||||
// Call NewFileCache and wait for it to be ready
|
||||
func callNewFileCache(name, cacheSize, cacheFolder string, maxItems int, getReader ReadFunc) *fileCache {
|
||||
fc := NewFileCache(name, cacheSize, cacheFolder, maxItems, getReader)
|
||||
Eventually(func() bool { return fc.Ready(context.TODO()) }).Should(BeTrue())
|
||||
return fc
|
||||
Eventually(func() bool { return fc.Ready(context.Background()) }).Should(BeTrue())
|
||||
return fc.(*fileCache)
|
||||
}
|
||||
|
||||
var _ = Describe("File Caches", func() {
|
||||
BeforeEach(func() {
|
||||
conf.Server.DataFolder, _ = os.MkdirTemp("", "file_caches")
|
||||
})
|
||||
AfterEach(func() {
|
||||
_ = os.RemoveAll(conf.Server.DataFolder)
|
||||
tmpDir, _ := os.MkdirTemp("", "file_caches")
|
||||
DeferCleanup(func() {
|
||||
configtest.SetupConfig()
|
||||
_ = os.RemoveAll(tmpDir)
|
||||
})
|
||||
conf.Server.DataFolder = tmpDir
|
||||
})
|
||||
|
||||
Describe("NewFileCache", func() {
|
||||
@@ -56,7 +60,7 @@ var _ = Describe("File Caches", func() {
|
||||
return strings.NewReader(arg.Key()), nil
|
||||
})
|
||||
// First call is a MISS
|
||||
s, err := fc.Get(context.TODO(), &testArg{"test"})
|
||||
s, err := fc.Get(context.Background(), &testArg{"test"})
|
||||
Expect(err).To(BeNil())
|
||||
Expect(s.Cached).To(BeFalse())
|
||||
Expect(s.Closer).To(BeNil())
|
||||
@@ -64,7 +68,7 @@ var _ = Describe("File Caches", func() {
|
||||
|
||||
// Second call is a HIT
|
||||
called = false
|
||||
s, err = fc.Get(context.TODO(), &testArg{"test"})
|
||||
s, err = fc.Get(context.Background(), &testArg{"test"})
|
||||
Expect(err).To(BeNil())
|
||||
Expect(io.ReadAll(s)).To(Equal([]byte("test")))
|
||||
Expect(s.Cached).To(BeTrue())
|
||||
@@ -79,22 +83,90 @@ var _ = Describe("File Caches", func() {
|
||||
return strings.NewReader(arg.Key()), nil
|
||||
})
|
||||
// First call is a MISS
|
||||
s, err := fc.Get(context.TODO(), &testArg{"test"})
|
||||
s, err := fc.Get(context.Background(), &testArg{"test"})
|
||||
Expect(err).To(BeNil())
|
||||
Expect(s.Cached).To(BeFalse())
|
||||
Expect(io.ReadAll(s)).To(Equal([]byte("test")))
|
||||
|
||||
// Second call is also a MISS
|
||||
called = false
|
||||
s, err = fc.Get(context.TODO(), &testArg{"test"})
|
||||
s, err = fc.Get(context.Background(), &testArg{"test"})
|
||||
Expect(err).To(BeNil())
|
||||
Expect(io.ReadAll(s)).To(Equal([]byte("test")))
|
||||
Expect(s.Cached).To(BeFalse())
|
||||
Expect(called).To(BeTrue())
|
||||
})
|
||||
|
||||
Context("reader errors", func() {
|
||||
When("creating a reader fails", func() {
|
||||
It("does not cache", func() {
|
||||
fc := callNewFileCache("test", "1KB", "test", 0, func(ctx context.Context, arg Item) (io.Reader, error) {
|
||||
return nil, errors.New("failed")
|
||||
})
|
||||
|
||||
_, err := fc.Get(context.Background(), &testArg{"test"})
|
||||
Expect(err).To(MatchError("failed"))
|
||||
})
|
||||
})
|
||||
When("reader returns error", func() {
|
||||
It("does not cache", func() {
|
||||
fc := callNewFileCache("test", "1KB", "test", 0, func(ctx context.Context, arg Item) (io.Reader, error) {
|
||||
return errFakeReader{errors.New("read failure")}, nil
|
||||
})
|
||||
|
||||
s, err := fc.Get(context.Background(), &testArg{"test"})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
_, _ = io.Copy(io.Discard, s)
|
||||
// TODO How to make the fscache reader return the underlying reader error?
|
||||
//Expect(err).To(MatchError("read failure"))
|
||||
|
||||
// Data should not be cached (or eventually be removed from cache)
|
||||
Eventually(func() bool {
|
||||
s, _ = fc.Get(context.Background(), &testArg{"test"})
|
||||
if s != nil {
|
||||
return s.Cached
|
||||
}
|
||||
return false
|
||||
}).Should(BeFalse())
|
||||
})
|
||||
})
|
||||
When("context is canceled", func() {
|
||||
It("does not cache", func() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
fc := callNewFileCache("test", "1KB", "test", 0, func(ctx context.Context, arg Item) (io.Reader, error) {
|
||||
return &ctxFakeReader{ctx}, nil
|
||||
})
|
||||
|
||||
s, err := fc.Get(ctx, &testArg{"test"})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
cancel()
|
||||
b := make([]byte, 10)
|
||||
_, err = s.Read(b)
|
||||
// TODO Should be context.Canceled error
|
||||
Expect(err).To(MatchError(io.EOF))
|
||||
|
||||
// Data should not be cached (or eventually be removed from cache)
|
||||
Eventually(func() bool {
|
||||
s, _ = fc.Get(context.Background(), &testArg{"test"})
|
||||
if s != nil {
|
||||
return s.Cached
|
||||
}
|
||||
return false
|
||||
}).Should(BeFalse())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
type testArg struct{ s string }
|
||||
|
||||
func (t *testArg) Key() string { return t.s }
|
||||
|
||||
type errFakeReader struct{ err error }
|
||||
|
||||
func (e errFakeReader) Read([]byte) (int, error) { return 0, e.err }
|
||||
|
||||
type ctxFakeReader struct{ ctx context.Context }
|
||||
|
||||
func (e *ctxFakeReader) Read([]byte) (int, error) { return 0, e.ctx.Err() }
|
||||
|
||||
Reference in New Issue
Block a user