update
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
Some checks failed
Pipeline: Test, Lint, Build / Get version info (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test Go code (push) Has been cancelled
Pipeline: Test, Lint, Build / Test JS code (push) Has been cancelled
Pipeline: Test, Lint, Build / Lint i18n files (push) Has been cancelled
Pipeline: Test, Lint, Build / Check Docker configuration (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/386) (push) Has been cancelled
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to GHCR (push) Has been cancelled
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Has been cancelled
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Has been cancelled
Pipeline: Test, Lint, Build / Build Windows installers (push) Has been cancelled
Pipeline: Test, Lint, Build / Package/Release (push) Has been cancelled
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
POEditor import / update-translations (push) Has been cancelled
This commit is contained in:
171
plugins/host_cache_test.go
Normal file
171
plugins/host_cache_test.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/navidrome/navidrome/plugins/host/cache"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("CacheService", func() {
|
||||
var service *cacheServiceImpl
|
||||
var ctx context.Context
|
||||
|
||||
BeforeEach(func() {
|
||||
ctx = context.Background()
|
||||
service = newCacheService("test_plugin")
|
||||
})
|
||||
|
||||
Describe("getTTL", func() {
|
||||
It("returns default TTL when seconds is 0", func() {
|
||||
ttl := service.getTTL(0)
|
||||
Expect(ttl).To(Equal(defaultCacheTTL))
|
||||
})
|
||||
|
||||
It("returns default TTL when seconds is negative", func() {
|
||||
ttl := service.getTTL(-10)
|
||||
Expect(ttl).To(Equal(defaultCacheTTL))
|
||||
})
|
||||
|
||||
It("returns correct duration when seconds is positive", func() {
|
||||
ttl := service.getTTL(60)
|
||||
Expect(ttl).To(Equal(time.Minute))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("String Operations", func() {
|
||||
It("sets and gets a string value", func() {
|
||||
_, err := service.SetString(ctx, &cache.SetStringRequest{
|
||||
Key: "string_key",
|
||||
Value: "test_value",
|
||||
TtlSeconds: 300,
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
res, err := service.GetString(ctx, &cache.GetRequest{Key: "string_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res.Exists).To(BeTrue())
|
||||
Expect(res.Value).To(Equal("test_value"))
|
||||
})
|
||||
|
||||
It("returns not exists for missing key", func() {
|
||||
res, err := service.GetString(ctx, &cache.GetRequest{Key: "missing_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res.Exists).To(BeFalse())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Integer Operations", func() {
|
||||
It("sets and gets an integer value", func() {
|
||||
_, err := service.SetInt(ctx, &cache.SetIntRequest{
|
||||
Key: "int_key",
|
||||
Value: 42,
|
||||
TtlSeconds: 300,
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
res, err := service.GetInt(ctx, &cache.GetRequest{Key: "int_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res.Exists).To(BeTrue())
|
||||
Expect(res.Value).To(Equal(int64(42)))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Float Operations", func() {
|
||||
It("sets and gets a float value", func() {
|
||||
_, err := service.SetFloat(ctx, &cache.SetFloatRequest{
|
||||
Key: "float_key",
|
||||
Value: 3.14,
|
||||
TtlSeconds: 300,
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
res, err := service.GetFloat(ctx, &cache.GetRequest{Key: "float_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res.Exists).To(BeTrue())
|
||||
Expect(res.Value).To(Equal(3.14))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Bytes Operations", func() {
|
||||
It("sets and gets a bytes value", func() {
|
||||
byteData := []byte("hello world")
|
||||
_, err := service.SetBytes(ctx, &cache.SetBytesRequest{
|
||||
Key: "bytes_key",
|
||||
Value: byteData,
|
||||
TtlSeconds: 300,
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
res, err := service.GetBytes(ctx, &cache.GetRequest{Key: "bytes_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res.Exists).To(BeTrue())
|
||||
Expect(res.Value).To(Equal(byteData))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Type mismatch handling", func() {
|
||||
It("returns not exists when type doesn't match the getter", func() {
|
||||
// Set string
|
||||
_, err := service.SetString(ctx, &cache.SetStringRequest{
|
||||
Key: "mixed_key",
|
||||
Value: "string value",
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// Try to get as int
|
||||
res, err := service.GetInt(ctx, &cache.GetRequest{Key: "mixed_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res.Exists).To(BeFalse())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Remove Operation", func() {
|
||||
It("removes a value from the cache", func() {
|
||||
// Set a value
|
||||
_, err := service.SetString(ctx, &cache.SetStringRequest{
|
||||
Key: "remove_key",
|
||||
Value: "to be removed",
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// Verify it exists
|
||||
res, err := service.Has(ctx, &cache.HasRequest{Key: "remove_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res.Exists).To(BeTrue())
|
||||
|
||||
// Remove it
|
||||
_, err = service.Remove(ctx, &cache.RemoveRequest{Key: "remove_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// Verify it's gone
|
||||
res, err = service.Has(ctx, &cache.HasRequest{Key: "remove_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res.Exists).To(BeFalse())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Has Operation", func() {
|
||||
It("returns true for existing key", func() {
|
||||
// Set a value
|
||||
_, err := service.SetString(ctx, &cache.SetStringRequest{
|
||||
Key: "existing_key",
|
||||
Value: "exists",
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// Check if it exists
|
||||
res, err := service.Has(ctx, &cache.HasRequest{Key: "existing_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res.Exists).To(BeTrue())
|
||||
})
|
||||
|
||||
It("returns false for non-existing key", func() {
|
||||
res, err := service.Has(ctx, &cache.HasRequest{Key: "non_existing_key"})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(res.Exists).To(BeFalse())
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user