package service import ( "testing" "time" "github.com/stretchr/testify/assert" ) var ( key = "test" val = []string{"val1", "val2", "val3"} ) func TestCacheGet(t *testing.T) { t.Parallel() c := &cache{ cache: make(map[string][]string), } c.cache[key] = val assert.Equal(t, c.Get(key), val) assert.Nil(t, c.Get("invalid-key")) } func TestCacheDelete(t *testing.T) { t.Parallel() c := &cache{ cache: make(map[string][]string), } c.cache[key] = val c.Delete(key) assert.Nil(t, c.cache[key]) } func TestCacheInsert(t *testing.T) { t.Parallel() expirationTime := 20 * time.Millisecond c := &cache{ cache: make(map[string][]string), expiration: expirationTime, } c.Insert(key, val) assert.Equal(t, c.Get(key), val) assert.Eventually(t, func() bool { return c.Get(key) == nil }, expirationTime+(5*time.Millisecond), 1*time.Millisecond) }