Skip to content

Commit 9dedced

Browse files
authored
Merge pull request #1740 from zhaque44/cache-test-updates
chore: update cache_test.go (add cache exp test & cache delete test)
2 parents ba6d000 + 7a89359 commit 9dedced

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

internal/cache/cache_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,55 @@ func TestCache(t *testing.T) {
8585
g.Expect(found).To(BeFalse())
8686
g.Expect(item).To(BeNil())
8787
}
88+
89+
func TestCacheExpiration(t *testing.T) {
90+
g := NewWithT(t)
91+
cache := New(10, 0)
92+
93+
key := "testKey"
94+
value := "testValue"
95+
expiration := 1 * time.Second
96+
97+
err := cache.Add(key, value, expiration)
98+
g.Expect(err).ToNot(HaveOccurred())
99+
100+
newExpiration := 2 * time.Second
101+
cache.SetExpiration(key, newExpiration)
102+
actualExpiration := cache.GetExpiration(key)
103+
104+
g.Expect(actualExpiration).Should(BeNumerically("~", newExpiration, 100*time.Millisecond))
105+
106+
g.Expect(cache.HasExpired(key)).To(BeFalse())
107+
108+
time.Sleep(newExpiration + 100*time.Millisecond)
109+
110+
g.Expect(cache.HasExpired(key)).To(BeTrue())
111+
112+
g.Expect(cache.GetExpiration(key)).To(BeZero())
113+
114+
nonExistentKey := "nonExistent"
115+
cache.SetExpiration(nonExistentKey, 1*time.Second)
116+
g.Expect(cache.GetExpiration(nonExistentKey)).To(BeZero())
117+
118+
g.Expect(cache.HasExpired(nonExistentKey)).To(BeTrue())
119+
}
120+
121+
func TestCacheDeleteClear(t *testing.T) {
122+
g := NewWithT(t)
123+
cache := New(3, 0)
124+
125+
err := cache.Add("key1", "value1", 0)
126+
g.Expect(err).ToNot(HaveOccurred())
127+
err = cache.Add("key2", "value2", 0)
128+
g.Expect(err).ToNot(HaveOccurred())
129+
err = cache.Add("key3", "value3", 0)
130+
g.Expect(err).ToNot(HaveOccurred())
131+
132+
cache.Delete("key2")
133+
_, found := cache.Get("key2")
134+
g.Expect(found).To(BeFalse())
135+
g.Expect(cache.ItemCount()).To(Equal(2))
136+
137+
cache.Clear()
138+
g.Expect(cache.ItemCount()).To(Equal(0))
139+
}

0 commit comments

Comments
 (0)