cachedcollection_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package mongoc
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "runtime"
  8. "sync"
  9. "sync/atomic"
  10. "testing"
  11. "time"
  12. "github.com/alicebob/miniredis"
  13. "github.com/globalsign/mgo"
  14. "github.com/globalsign/mgo/bson"
  15. "github.com/stretchr/testify/assert"
  16. "github.com/tal-tech/go-zero/core/stat"
  17. "github.com/tal-tech/go-zero/core/stores/internal"
  18. "github.com/tal-tech/go-zero/core/stores/mongo"
  19. "github.com/tal-tech/go-zero/core/stores/redis"
  20. )
  21. func init() {
  22. stat.SetReporter(nil)
  23. }
  24. func TestStat(t *testing.T) {
  25. resetStats()
  26. s, err := miniredis.Run()
  27. if err != nil {
  28. t.Error(err)
  29. }
  30. r := redis.NewRedis(s.Addr(), redis.NodeType)
  31. cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
  32. c := newCollection(dummyConn{}, cach)
  33. for i := 0; i < 10; i++ {
  34. var str string
  35. if err = c.cache.Take(&str, "name", func(v interface{}) error {
  36. *v.(*string) = "zero"
  37. return nil
  38. }); err != nil {
  39. t.Error(err)
  40. }
  41. }
  42. assert.Equal(t, uint64(10), atomic.LoadUint64(&stats.Total))
  43. assert.Equal(t, uint64(9), atomic.LoadUint64(&stats.Hit))
  44. }
  45. func TestStatCacheFails(t *testing.T) {
  46. resetStats()
  47. log.SetOutput(ioutil.Discard)
  48. defer log.SetOutput(os.Stdout)
  49. r := redis.NewRedis("localhost:59999", redis.NodeType)
  50. cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
  51. c := newCollection(dummyConn{}, cach)
  52. for i := 0; i < 20; i++ {
  53. var str string
  54. err := c.FindOne(&str, "name", bson.M{})
  55. assert.NotNil(t, err)
  56. }
  57. assert.Equal(t, uint64(20), atomic.LoadUint64(&stats.Total))
  58. assert.Equal(t, uint64(0), atomic.LoadUint64(&stats.Hit))
  59. assert.Equal(t, uint64(20), atomic.LoadUint64(&stats.Miss))
  60. assert.Equal(t, uint64(0), atomic.LoadUint64(&stats.DbFails))
  61. }
  62. func TestStatDbFails(t *testing.T) {
  63. resetStats()
  64. s, err := miniredis.Run()
  65. if err != nil {
  66. t.Error(err)
  67. }
  68. r := redis.NewRedis(s.Addr(), redis.NodeType)
  69. cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
  70. c := newCollection(dummyConn{}, cach)
  71. for i := 0; i < 20; i++ {
  72. var str string
  73. err := c.cache.Take(&str, "name", func(v interface{}) error {
  74. return errors.New("db failed")
  75. })
  76. assert.NotNil(t, err)
  77. }
  78. assert.Equal(t, uint64(20), atomic.LoadUint64(&stats.Total))
  79. assert.Equal(t, uint64(0), atomic.LoadUint64(&stats.Hit))
  80. assert.Equal(t, uint64(20), atomic.LoadUint64(&stats.DbFails))
  81. }
  82. func TestStatFromMemory(t *testing.T) {
  83. resetStats()
  84. s, err := miniredis.Run()
  85. if err != nil {
  86. t.Error(err)
  87. }
  88. r := redis.NewRedis(s.Addr(), redis.NodeType)
  89. cach := internal.NewCacheNode(r, sharedCalls, stats, mgo.ErrNotFound)
  90. c := newCollection(dummyConn{}, cach)
  91. var all sync.WaitGroup
  92. var wait sync.WaitGroup
  93. all.Add(10)
  94. wait.Add(4)
  95. go func() {
  96. var str string
  97. if err := c.cache.Take(&str, "name", func(v interface{}) error {
  98. *v.(*string) = "zero"
  99. return nil
  100. }); err != nil {
  101. t.Error(err)
  102. }
  103. wait.Wait()
  104. runtime.Gosched()
  105. all.Done()
  106. }()
  107. for i := 0; i < 4; i++ {
  108. go func() {
  109. var str string
  110. wait.Done()
  111. if err := c.cache.Take(&str, "name", func(v interface{}) error {
  112. *v.(*string) = "zero"
  113. return nil
  114. }); err != nil {
  115. t.Error(err)
  116. }
  117. all.Done()
  118. }()
  119. }
  120. for i := 0; i < 5; i++ {
  121. go func() {
  122. var str string
  123. if err := c.cache.Take(&str, "name", func(v interface{}) error {
  124. *v.(*string) = "zero"
  125. return nil
  126. }); err != nil {
  127. t.Error(err)
  128. }
  129. all.Done()
  130. }()
  131. }
  132. all.Wait()
  133. assert.Equal(t, uint64(10), atomic.LoadUint64(&stats.Total))
  134. assert.Equal(t, uint64(9), atomic.LoadUint64(&stats.Hit))
  135. }
  136. func resetStats() {
  137. atomic.StoreUint64(&stats.Total, 0)
  138. atomic.StoreUint64(&stats.Hit, 0)
  139. atomic.StoreUint64(&stats.Miss, 0)
  140. atomic.StoreUint64(&stats.DbFails, 0)
  141. }
  142. type dummyConn struct {
  143. }
  144. func (c dummyConn) Find(query interface{}) mongo.Query {
  145. return dummyQuery{}
  146. }
  147. func (c dummyConn) FindId(id interface{}) mongo.Query {
  148. return dummyQuery{}
  149. }
  150. func (c dummyConn) Insert(docs ...interface{}) error {
  151. return nil
  152. }
  153. func (c dummyConn) Remove(selector interface{}) error {
  154. return nil
  155. }
  156. func (dummyConn) Pipe(pipeline interface{}) mongo.Pipe {
  157. return nil
  158. }
  159. func (c dummyConn) RemoveAll(selector interface{}) (*mgo.ChangeInfo, error) {
  160. return nil, nil
  161. }
  162. func (c dummyConn) RemoveId(id interface{}) error {
  163. return nil
  164. }
  165. func (c dummyConn) Update(selector, update interface{}) error {
  166. return nil
  167. }
  168. func (c dummyConn) UpdateId(id, update interface{}) error {
  169. return nil
  170. }
  171. func (c dummyConn) Upsert(selector, update interface{}) (*mgo.ChangeInfo, error) {
  172. return nil, nil
  173. }
  174. type dummyQuery struct {
  175. }
  176. func (d dummyQuery) All(result interface{}) error {
  177. return nil
  178. }
  179. func (d dummyQuery) Apply(change mgo.Change, result interface{}) (*mgo.ChangeInfo, error) {
  180. return nil, nil
  181. }
  182. func (d dummyQuery) Count() (int, error) {
  183. return 0, nil
  184. }
  185. func (d dummyQuery) Distinct(key string, result interface{}) error {
  186. return nil
  187. }
  188. func (d dummyQuery) Explain(result interface{}) error {
  189. return nil
  190. }
  191. func (d dummyQuery) For(result interface{}, f func() error) error {
  192. return nil
  193. }
  194. func (d dummyQuery) MapReduce(job *mgo.MapReduce, result interface{}) (*mgo.MapReduceInfo, error) {
  195. return nil, nil
  196. }
  197. func (d dummyQuery) One(result interface{}) error {
  198. return nil
  199. }
  200. func (d dummyQuery) Batch(n int) mongo.Query {
  201. return d
  202. }
  203. func (d dummyQuery) Collation(collation *mgo.Collation) mongo.Query {
  204. return d
  205. }
  206. func (d dummyQuery) Comment(comment string) mongo.Query {
  207. return d
  208. }
  209. func (d dummyQuery) Hint(indexKey ...string) mongo.Query {
  210. return d
  211. }
  212. func (d dummyQuery) Iter() mongo.Iter {
  213. return &mgo.Iter{}
  214. }
  215. func (d dummyQuery) Limit(n int) mongo.Query {
  216. return d
  217. }
  218. func (d dummyQuery) LogReplay() mongo.Query {
  219. return d
  220. }
  221. func (d dummyQuery) Prefetch(p float64) mongo.Query {
  222. return d
  223. }
  224. func (d dummyQuery) Select(selector interface{}) mongo.Query {
  225. return d
  226. }
  227. func (d dummyQuery) SetMaxScan(n int) mongo.Query {
  228. return d
  229. }
  230. func (d dummyQuery) SetMaxTime(duration time.Duration) mongo.Query {
  231. return d
  232. }
  233. func (d dummyQuery) Skip(n int) mongo.Query {
  234. return d
  235. }
  236. func (d dummyQuery) Snapshot() mongo.Query {
  237. return d
  238. }
  239. func (d dummyQuery) Sort(fields ...string) mongo.Query {
  240. return d
  241. }
  242. func (d dummyQuery) Tail(timeout time.Duration) mongo.Iter {
  243. return &mgo.Iter{}
  244. }