...
1 package service
2
3 import (
4 "testing"
5 "time"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 var (
11 key = "test"
12 val = []string{"val1", "val2", "val3"}
13 )
14
15 func TestCacheGet(t *testing.T) {
16 t.Parallel()
17
18 c := &cache{
19 cache: make(map[string][]string),
20 }
21
22 c.cache[key] = val
23
24 assert.Equal(t, c.Get(key), val)
25 assert.Nil(t, c.Get("invalid-key"))
26 }
27
28 func TestCacheDelete(t *testing.T) {
29 t.Parallel()
30
31 c := &cache{
32 cache: make(map[string][]string),
33 }
34
35 c.cache[key] = val
36 c.Delete(key)
37
38 assert.Nil(t, c.cache[key])
39 }
40
41 func TestCacheInsert(t *testing.T) {
42 t.Parallel()
43
44 expirationTime := 20 * time.Millisecond
45 c := &cache{
46 cache: make(map[string][]string),
47 expiration: expirationTime,
48 }
49
50 c.Insert(key, val)
51 assert.Equal(t, c.Get(key), val)
52
53 assert.Eventually(t, func() bool {
54 return c.Get(key) == nil
55 }, expirationTime+(5*time.Millisecond), 1*time.Millisecond)
56 }
57
View as plain text