cachenode_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package cache
  2. import (
  3. "errors"
  4. "math/rand"
  5. "sync"
  6. "testing"
  7. "time"
  8. "github.com/alicebob/miniredis"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/tal-tech/go-zero/core/logx"
  11. "github.com/tal-tech/go-zero/core/mathx"
  12. "github.com/tal-tech/go-zero/core/stat"
  13. "github.com/tal-tech/go-zero/core/stores/redis"
  14. )
  15. func init() {
  16. logx.Disable()
  17. stat.SetReporter(nil)
  18. }
  19. func TestCacheNode_DelCache(t *testing.T) {
  20. s, err := miniredis.Run()
  21. assert.Nil(t, err)
  22. defer s.Close()
  23. cn := cacheNode{
  24. rds: redis.NewRedis(s.Addr(), redis.NodeType),
  25. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  26. lock: new(sync.Mutex),
  27. unstableExpiry: mathx.NewUnstable(expiryDeviation),
  28. stat: NewCacheStat("any"),
  29. errNotFound: errors.New("any"),
  30. }
  31. assert.Nil(t, cn.DelCache())
  32. assert.Nil(t, cn.DelCache([]string{}...))
  33. assert.Nil(t, cn.DelCache(make([]string, 0)...))
  34. cn.SetCache("first", "one")
  35. assert.Nil(t, cn.DelCache("first"))
  36. cn.SetCache("first", "one")
  37. cn.SetCache("second", "two")
  38. assert.Nil(t, cn.DelCache("first", "second"))
  39. }
  40. func TestCacheNode_InvalidCache(t *testing.T) {
  41. s, err := miniredis.Run()
  42. assert.Nil(t, err)
  43. defer s.Close()
  44. cn := cacheNode{
  45. rds: redis.NewRedis(s.Addr(), redis.NodeType),
  46. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  47. lock: new(sync.Mutex),
  48. unstableExpiry: mathx.NewUnstable(expiryDeviation),
  49. stat: NewCacheStat("any"),
  50. errNotFound: errors.New("any"),
  51. }
  52. s.Set("any", "value")
  53. var str string
  54. assert.NotNil(t, cn.GetCache("any", &str))
  55. assert.Equal(t, "", str)
  56. _, err = s.Get("any")
  57. assert.Equal(t, miniredis.ErrKeyNotFound, err)
  58. }