cachenode_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package cache
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "strconv"
  7. "sync"
  8. "testing"
  9. "time"
  10. "github.com/alicebob/miniredis/v2"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/tal-tech/go-zero/core/logx"
  13. "github.com/tal-tech/go-zero/core/mathx"
  14. "github.com/tal-tech/go-zero/core/stat"
  15. "github.com/tal-tech/go-zero/core/stores/redis"
  16. "github.com/tal-tech/go-zero/core/stores/redis/redistest"
  17. "github.com/tal-tech/go-zero/core/syncx"
  18. )
  19. var errTestNotFound = errors.New("not found")
  20. func init() {
  21. logx.Disable()
  22. stat.SetReporter(nil)
  23. }
  24. func TestCacheNode_DelCache(t *testing.T) {
  25. store, clean, err := redistest.CreateRedis()
  26. assert.Nil(t, err)
  27. defer clean()
  28. cn := cacheNode{
  29. rds: store,
  30. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  31. lock: new(sync.Mutex),
  32. unstableExpiry: mathx.NewUnstable(expiryDeviation),
  33. stat: NewStat("any"),
  34. errNotFound: errTestNotFound,
  35. }
  36. assert.Nil(t, cn.Del())
  37. assert.Nil(t, cn.Del([]string{}...))
  38. assert.Nil(t, cn.Del(make([]string, 0)...))
  39. cn.Set("first", "one")
  40. assert.Nil(t, cn.Del("first"))
  41. cn.Set("first", "one")
  42. cn.Set("second", "two")
  43. assert.Nil(t, cn.Del("first", "second"))
  44. }
  45. func TestCacheNode_InvalidCache(t *testing.T) {
  46. s, err := miniredis.Run()
  47. assert.Nil(t, err)
  48. defer s.Close()
  49. cn := cacheNode{
  50. rds: redis.NewRedis(s.Addr(), redis.NodeType),
  51. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  52. lock: new(sync.Mutex),
  53. unstableExpiry: mathx.NewUnstable(expiryDeviation),
  54. stat: NewStat("any"),
  55. errNotFound: errTestNotFound,
  56. }
  57. s.Set("any", "value")
  58. var str string
  59. assert.NotNil(t, cn.Get("any", &str))
  60. assert.Equal(t, "", str)
  61. _, err = s.Get("any")
  62. assert.Equal(t, miniredis.ErrKeyNotFound, err)
  63. }
  64. func TestCacheNode_Take(t *testing.T) {
  65. store, clean, err := redistest.CreateRedis()
  66. assert.Nil(t, err)
  67. defer clean()
  68. cn := cacheNode{
  69. rds: store,
  70. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  71. barrier: syncx.NewSharedCalls(),
  72. lock: new(sync.Mutex),
  73. unstableExpiry: mathx.NewUnstable(expiryDeviation),
  74. stat: NewStat("any"),
  75. errNotFound: errTestNotFound,
  76. }
  77. var str string
  78. err = cn.Take(&str, "any", func(v interface{}) error {
  79. *v.(*string) = "value"
  80. return nil
  81. })
  82. assert.Nil(t, err)
  83. assert.Equal(t, "value", str)
  84. assert.Nil(t, cn.Get("any", &str))
  85. val, err := store.Get("any")
  86. assert.Nil(t, err)
  87. assert.Equal(t, `"value"`, val)
  88. }
  89. func TestCacheNode_TakeNotFound(t *testing.T) {
  90. store, clean, err := redistest.CreateRedis()
  91. assert.Nil(t, err)
  92. defer clean()
  93. cn := cacheNode{
  94. rds: store,
  95. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  96. barrier: syncx.NewSharedCalls(),
  97. lock: new(sync.Mutex),
  98. unstableExpiry: mathx.NewUnstable(expiryDeviation),
  99. stat: NewStat("any"),
  100. errNotFound: errTestNotFound,
  101. }
  102. var str string
  103. err = cn.Take(&str, "any", func(v interface{}) error {
  104. return errTestNotFound
  105. })
  106. assert.True(t, cn.IsNotFound(err))
  107. assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
  108. val, err := store.Get("any")
  109. assert.Nil(t, err)
  110. assert.Equal(t, `*`, val)
  111. store.Set("any", "*")
  112. err = cn.Take(&str, "any", func(v interface{}) error {
  113. return nil
  114. })
  115. assert.True(t, cn.IsNotFound(err))
  116. assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
  117. store.Del("any")
  118. errDummy := errors.New("dummy")
  119. err = cn.Take(&str, "any", func(v interface{}) error {
  120. return errDummy
  121. })
  122. assert.Equal(t, errDummy, err)
  123. }
  124. func TestCacheNode_TakeWithExpire(t *testing.T) {
  125. store, clean, err := redistest.CreateRedis()
  126. assert.Nil(t, err)
  127. defer clean()
  128. cn := cacheNode{
  129. rds: store,
  130. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  131. barrier: syncx.NewSharedCalls(),
  132. lock: new(sync.Mutex),
  133. unstableExpiry: mathx.NewUnstable(expiryDeviation),
  134. stat: NewStat("any"),
  135. errNotFound: errors.New("any"),
  136. }
  137. var str string
  138. err = cn.TakeWithExpire(&str, "any", func(v interface{}, expire time.Duration) error {
  139. *v.(*string) = "value"
  140. return nil
  141. })
  142. assert.Nil(t, err)
  143. assert.Equal(t, "value", str)
  144. assert.Nil(t, cn.Get("any", &str))
  145. val, err := store.Get("any")
  146. assert.Nil(t, err)
  147. assert.Equal(t, `"value"`, val)
  148. }
  149. func TestCacheNode_String(t *testing.T) {
  150. store, clean, err := redistest.CreateRedis()
  151. assert.Nil(t, err)
  152. defer clean()
  153. cn := cacheNode{
  154. rds: store,
  155. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  156. barrier: syncx.NewSharedCalls(),
  157. lock: new(sync.Mutex),
  158. unstableExpiry: mathx.NewUnstable(expiryDeviation),
  159. stat: NewStat("any"),
  160. errNotFound: errors.New("any"),
  161. }
  162. assert.Equal(t, store.Addr, cn.String())
  163. }
  164. func TestCacheValueWithBigInt(t *testing.T) {
  165. store, clean, err := redistest.CreateRedis()
  166. assert.Nil(t, err)
  167. defer clean()
  168. cn := cacheNode{
  169. rds: store,
  170. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  171. barrier: syncx.NewSharedCalls(),
  172. lock: new(sync.Mutex),
  173. unstableExpiry: mathx.NewUnstable(expiryDeviation),
  174. stat: NewStat("any"),
  175. errNotFound: errors.New("any"),
  176. }
  177. const (
  178. key = "key"
  179. value int64 = 323427211229009810
  180. )
  181. assert.Nil(t, cn.Set(key, value))
  182. var val interface{}
  183. assert.Nil(t, cn.Get(key, &val))
  184. assert.Equal(t, strconv.FormatInt(value, 10), fmt.Sprintf("%v", val))
  185. }