navidrome/core/file_caches_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
2.8 KiB
Go
Raw Permalink Normal View History

package core
2020-04-09 19:15:01 +02:00
import (
2020-07-24 19:30:27 +02:00
"context"
"fmt"
"io"
2020-04-09 19:15:01 +02:00
"io/ioutil"
"os"
"path/filepath"
2020-07-24 19:30:27 +02:00
"strings"
2020-04-09 19:15:01 +02:00
"github.com/deluan/navidrome/conf"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
2020-07-24 21:40:27 +02:00
// 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() }).Should(BeTrue())
return fc
}
2020-04-09 19:15:01 +02:00
var _ = Describe("File Caches", func() {
BeforeEach(func() {
conf.Server.DataFolder, _ = ioutil.TempDir("", "file_caches")
})
AfterEach(func() {
os.RemoveAll(conf.Server.DataFolder)
})
Describe("NewFileCache", func() {
2020-04-09 19:15:01 +02:00
It("creates the cache folder", func() {
2020-07-24 21:40:27 +02:00
Expect(callNewFileCache("test", "1k", "test", 0, nil)).ToNot(BeNil())
2020-04-09 19:15:01 +02:00
2020-04-09 19:36:05 +02:00
_, err := os.Stat(filepath.Join(conf.Server.DataFolder, "test"))
2020-04-09 19:15:01 +02:00
Expect(os.IsNotExist(err)).To(BeFalse())
})
2020-04-09 19:36:05 +02:00
It("creates the cache folder with invalid size", func() {
2020-07-24 21:40:27 +02:00
fc := callNewFileCache("test", "abc", "test", 0, nil)
Expect(fc.cache).ToNot(BeNil())
Expect(fc.disabled).To(BeFalse())
2020-04-09 19:36:05 +02:00
})
It("returns empty if cache size is '0'", func() {
2020-07-24 21:40:27 +02:00
fc := callNewFileCache("test", "0", "test", 0, nil)
Expect(fc.cache).To(BeNil())
Expect(fc.disabled).To(BeTrue())
})
2020-04-09 19:15:01 +02:00
})
Describe("FileCache", func() {
2020-07-24 19:30:27 +02:00
It("caches data if cache is enabled", func() {
called := false
2020-07-24 21:40:27 +02:00
fc := callNewFileCache("test", "1KB", "test", 0, func(ctx context.Context, arg fmt.Stringer) (io.Reader, error) {
2020-07-24 19:30:27 +02:00
called = true
return strings.NewReader(arg.String()), nil
})
// First call is a MISS
s, err := fc.Get(context.TODO(), &testArg{"test"})
Expect(err).To(BeNil())
2020-07-25 00:46:08 +02:00
Expect(s.Cached).To(BeFalse())
2020-07-24 19:30:27 +02:00
Expect(ioutil.ReadAll(s)).To(Equal([]byte("test")))
// Second call is a HIT
called = false
s, err = fc.Get(context.TODO(), &testArg{"test"})
Expect(err).To(BeNil())
Expect(ioutil.ReadAll(s)).To(Equal([]byte("test")))
2020-07-25 00:46:08 +02:00
Expect(s.Cached).To(BeTrue())
2020-07-24 19:30:27 +02:00
Expect(called).To(BeFalse())
})
It("does not cache data if cache is disabled", func() {
called := false
2020-07-24 21:40:27 +02:00
fc := callNewFileCache("test", "0", "test", 0, func(ctx context.Context, arg fmt.Stringer) (io.Reader, error) {
2020-07-24 19:30:27 +02:00
called = true
return strings.NewReader(arg.String()), nil
})
// First call is a MISS
s, err := fc.Get(context.TODO(), &testArg{"test"})
Expect(err).To(BeNil())
2020-07-25 00:46:08 +02:00
Expect(s.Cached).To(BeFalse())
2020-07-24 19:30:27 +02:00
Expect(ioutil.ReadAll(s)).To(Equal([]byte("test")))
2020-07-24 19:30:27 +02:00
// Second call is also a MISS
called = false
s, err = fc.Get(context.TODO(), &testArg{"test"})
Expect(err).To(BeNil())
Expect(ioutil.ReadAll(s)).To(Equal([]byte("test")))
2020-07-25 00:46:08 +02:00
Expect(s.Cached).To(BeFalse())
2020-07-24 19:30:27 +02:00
Expect(called).To(BeTrue())
})
})
2020-04-09 19:15:01 +02:00
})
2020-07-24 19:30:27 +02:00
type testArg struct{ s string }
func (t *testArg) String() string { return t.s }