mirror of
https://github.com/hanzoai/traces.git
synced 2026-10-30 18:20:41 +00:00
* implement simple cache GET * make better errors and add cache to tempo local * improve memcache logs * delete unused method * changelog * use new method in other places * use otel tracing * log missed and hitted keys * remove batched requests * remove span loggers from memcached
74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/go-kit/log"
|
|
"github.com/go-redis/redis/v8"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRedisCache(t *testing.T) {
|
|
c, err := mockRedisCache()
|
|
require.Nil(t, err)
|
|
defer c.redis.Close()
|
|
|
|
keys := []string{"key1", "key2", "key3"}
|
|
bufs := [][]byte{[]byte("data1"), []byte("data2"), []byte("data3")}
|
|
miss := []string{"miss1", "miss2"}
|
|
|
|
// ensure input correctness
|
|
nHit := len(keys)
|
|
require.Len(t, bufs, nHit)
|
|
|
|
nMiss := len(miss)
|
|
|
|
ctx := context.Background()
|
|
|
|
c.Store(ctx, keys, bufs)
|
|
|
|
// test hits
|
|
found, data, missed := c.Fetch(ctx, keys)
|
|
|
|
require.Len(t, found, nHit)
|
|
require.Len(t, missed, 0)
|
|
for i := 0; i < nHit; i++ {
|
|
require.Equal(t, keys[i], found[i])
|
|
require.Equal(t, bufs[i], data[i])
|
|
}
|
|
|
|
_, foundKey := c.FetchKey(ctx, "key1")
|
|
assert.True(t, foundKey)
|
|
|
|
// test misses
|
|
found, _, missed = c.Fetch(ctx, miss)
|
|
|
|
require.Len(t, found, 0)
|
|
require.Len(t, missed, nMiss)
|
|
for i := 0; i < nMiss; i++ {
|
|
require.Equal(t, miss[i], missed[i])
|
|
}
|
|
|
|
_, foundKey = c.FetchKey(ctx, miss[0])
|
|
assert.False(t, foundKey)
|
|
}
|
|
|
|
func mockRedisCache() (*RedisCache, error) {
|
|
redisServer, err := miniredis.Run()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
redisClient := &RedisClient{
|
|
expiration: time.Minute,
|
|
timeout: 100 * time.Millisecond,
|
|
rdb: redis.NewUniversalClient(&redis.UniversalOptions{
|
|
Addrs: []string{redisServer.Addr()},
|
|
}),
|
|
}
|
|
return NewRedisCache("mock", redisClient, nil, log.NewNopLogger()), nil
|
|
}
|