Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
larscom committed Sep 2, 2023
1 parent 6d4dc6b commit 2a7e17a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ func (c *Cache[Key, Value]) Channel() <-chan Entry[Key, Value] {
itemchn := make(chan Entry[Key, Value], e.Count())

go func() {
defer close(itemchn)

for item := range e.IterBuffered() {
itemchn <- Entry[Key, Value]{
Key: item.Key,
Value: item.Val.value,
}
}
close(itemchn)
}()

return itemchn
Expand Down
28 changes: 28 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ func Test_WithExpireAfterWrite(t *testing.T) {

wg.Wait()
})
t.Run("channel", func(t *testing.T) {
ttl := time.Millisecond * 10
c := createCache(WithExpireAfterWrite[int, int](ttl))
c.Put(1, 100)

<-time.After(time.Millisecond * 15)
c.Put(2, 200)

var wg sync.WaitGroup
wg.Add(1)

for item := range c.Channel() {
assert.Equal(t, 2, item.Key)
assert.Equal(t, 200, item.Value)
wg.Done()
}

wg.Wait()
})
t.Run("get", func(t *testing.T) {
ttl := time.Millisecond * 10
c := createCache(WithExpireAfterWrite[int, int](ttl))
Expand Down Expand Up @@ -277,6 +296,15 @@ func Test_WithExpireAfterWrite(t *testing.T) {
has := c.Has(1)
assert.False(t, has)
})
t.Run("isEmpty", func(t *testing.T) {
ttl := time.Millisecond * 10
c := createCache(WithExpireAfterWrite[int, int](ttl))

c.Put(1, 100)
<-time.After(time.Millisecond * 15)

assert.True(t, c.IsEmpty())
})
t.Run("keys", func(t *testing.T) {
ttl := time.Millisecond * 10
c := createCache(WithExpireAfterWrite[int, int](ttl))
Expand Down

0 comments on commit 2a7e17a

Please sign in to comment.