Also use SimpleCache in cache.HTTPClient

This commit is contained in:
Deluan
2024-06-21 17:40:18 -04:00
parent 29bc17acd7
commit 29b7b740ce
3 changed files with 67 additions and 25 deletions
+37
View File
@@ -2,6 +2,7 @@ package cache
import (
"errors"
"fmt"
"time"
. "github.com/onsi/ginkgo/v2"
@@ -82,4 +83,40 @@ var _ = Describe("SimpleCache", func() {
Expect(keys).To(ConsistOf("key1", "key2"))
})
})
Describe("Options", func() {
Context("when size limit is set", func() {
BeforeEach(func() {
cache = NewSimpleCache[string](Options{
SizeLimit: 2,
})
})
It("should not add more items than the size limit", func() {
for i := 1; i <= 3; i++ {
err := cache.Add(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))
Expect(err).NotTo(HaveOccurred())
}
Expect(cache.Keys()).To(ConsistOf("key2", "key3"))
})
})
Context("when default TTL is set", func() {
BeforeEach(func() {
cache = NewSimpleCache[string](Options{
DefaultTTL: 10 * time.Millisecond,
})
})
It("should expire items after the default TTL", func() {
_ = cache.Add("key", "value")
time.Sleep(50 * time.Millisecond)
_, err := cache.Get("key")
Expect(err).To(HaveOccurred())
})
})
})
})