| 13 | ) |
| 14 | |
| 15 | func TestRedisCache(t *testing.T) { |
| 16 | c, err := mockRedisCache() |
| 17 | require.Nil(t, err) |
| 18 | defer c.redis.Close() |
| 19 | |
| 20 | keys := []string{"key1", "key2", "key3"} |
| 21 | bufs := [][]byte{[]byte("data1"), []byte("data2"), []byte("data3")} |
| 22 | miss := []string{"miss1", "miss2"} |
| 23 | |
| 24 | // ensure input correctness |
| 25 | nHit := len(keys) |
| 26 | require.Len(t, bufs, nHit) |
| 27 | |
| 28 | nMiss := len(miss) |
| 29 | |
| 30 | ctx := context.Background() |
| 31 | |
| 32 | c.Store(ctx, keys, bufs) |
| 33 | |
| 34 | // test hits |
| 35 | found, data, missed := c.Fetch(ctx, keys) |
| 36 | |
| 37 | require.Len(t, found, nHit) |
| 38 | require.Len(t, missed, 0) |
| 39 | for i := 0; i < nHit; i++ { |
| 40 | require.Equal(t, keys[i], found[i]) |
| 41 | require.Equal(t, bufs[i], data[i]) |
| 42 | } |
| 43 | |
| 44 | _, foundKey := c.FetchKey(ctx, "key1") |
| 45 | assert.True(t, foundKey) |
| 46 | |
| 47 | // test misses |
| 48 | found, _, missed = c.Fetch(ctx, miss) |
| 49 | |
| 50 | require.Len(t, found, 0) |
| 51 | require.Len(t, missed, nMiss) |
| 52 | for i := 0; i < nMiss; i++ { |
| 53 | require.Equal(t, miss[i], missed[i]) |
| 54 | } |
| 55 | |
| 56 | _, foundKey = c.FetchKey(ctx, miss[0]) |
| 57 | assert.False(t, foundKey) |
| 58 | } |
| 59 | |
| 60 | func mockRedisCache() (*RedisCache, error) { |
| 61 | redisServer, err := miniredis.Run() |