cache_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package vip
  2. import (
  3. "context"
  4. "fmt"
  5. . "github.com/smartystreets/goconvey/convey"
  6. "go-common/app/service/live/xuser/model"
  7. "go-common/library/log"
  8. "testing"
  9. "time"
  10. )
  11. func TestDao_GetVipFromCache(t *testing.T) {
  12. initd()
  13. Convey("test get vip cache", t, testWithTestUser(func(u *TestUser) {
  14. var (
  15. ctx = context.Background()
  16. info *model.VipInfo
  17. err error
  18. conn = d.redis.Get(ctx)
  19. key = getUserCacheKey(u.Uid)
  20. )
  21. log.Info("TestDao_GetVipFromCache uid(%d), key(%s)", u.Uid, key)
  22. // delete key at begin
  23. conn.Do("DEL", key)
  24. // should get nil info and err
  25. Convey("should get nil info and err", func() {
  26. info, err = d.GetVipFromCache(ctx, u.Uid)
  27. So(info, ShouldBeNil)
  28. So(err, ShouldBeNil)
  29. })
  30. // set empty data
  31. Convey("set empty data", func() {
  32. conn.Do("HSET", key, _vipFieldName, "")
  33. info, err = d.GetVipFromCache(ctx, u.Uid)
  34. So(info, ShouldBeNil)
  35. So(err, ShouldBeNil)
  36. })
  37. // set not a json
  38. Convey("set not a json", func() {
  39. conn.Do("HSET", key, _vipFieldName, "test")
  40. info, err = d.GetVipFromCache(ctx, u.Uid)
  41. So(info, ShouldBeNil)
  42. So(err, ShouldBeNil)
  43. })
  44. // test vip/svip string format
  45. Convey("test vip/svip string format", func() {
  46. vipTime := time.Now().Add(time.Hour * 12).Format(model.TimeNano)
  47. svipTime := time.Now().AddDate(0, -1, 0).Format(model.TimeNano)
  48. conn.Do("HSET", key, _vipFieldName, fmt.Sprintf(`{"vip":"1","vip_time":"%s","svip":0,"svip_time":"%s"}`, vipTime, svipTime))
  49. info, err = d.GetVipFromCache(ctx, u.Uid)
  50. So(info.Vip, ShouldEqual, 1)
  51. So(info.Svip, ShouldEqual, 0)
  52. So(err, ShouldBeNil)
  53. })
  54. // set valid data
  55. Convey("set valid data", func() {
  56. vipTime := time.Now().Add(time.Hour * 12).Format(model.TimeNano)
  57. conn.Do("HSET", key, _vipFieldName, fmt.Sprintf(`{"vip":1,"vip_time":"%s","svip":0,"svip_time":"0000-00-00 00:00:00"}`, vipTime))
  58. info, err = d.GetVipFromCache(ctx, u.Uid)
  59. So(info, ShouldNotBeNil)
  60. So(info.Vip, ShouldEqual, 1)
  61. So(info.VipTime, ShouldEqual, vipTime)
  62. So(info.Svip, ShouldEqual, 0)
  63. So(info.SvipTime, ShouldEqual, model.TimeEmpty)
  64. So(err, ShouldBeNil)
  65. })
  66. // expired vip time
  67. Convey("set valid but expired vip time", func() {
  68. vip := 1
  69. vipTime := time.Now().AddDate(0, 0, -1).Format(model.TimeNano)
  70. svip := 1
  71. svipTime := time.Now().AddDate(0, -1, -1).Format(model.TimeNano)
  72. conn.Do("HSET", key, _vipFieldName, fmt.Sprintf(`{"vip":%d,"vip_time":"%s","svip":%d,"svip_time":"%s"}`, vip, vipTime, svip, svipTime))
  73. info, err = d.GetVipFromCache(ctx, u.Uid)
  74. log.Info("expired vip time, info(%+v)", info)
  75. So(info, ShouldNotBeNil)
  76. So(info.Vip, ShouldEqual, 0)
  77. So(info.VipTime, ShouldEqual, vipTime)
  78. So(info.Svip, ShouldEqual, 0)
  79. So(info.SvipTime, ShouldEqual, svipTime)
  80. So(err, ShouldBeNil)
  81. })
  82. }))
  83. }
  84. func TestDao_SetVipCache(t *testing.T) {
  85. initd()
  86. Convey("test set vip cache", t, testWithTestUser(func(u *TestUser) {
  87. var (
  88. ctx = context.Background()
  89. info *model.VipInfo
  90. err error
  91. conn = d.redis.Get(ctx)
  92. key = getUserCacheKey(u.Uid)
  93. )
  94. log.Info("TestDao_GetVipFromCache uid(%d), key(%s)", u.Uid, key)
  95. // delete key at begin
  96. conn.Do("DEL", key)
  97. // nil info
  98. Convey("nil info", func() {
  99. err = d.SetVipCache(ctx, u.Uid, nil)
  100. So(err, ShouldBeNil)
  101. info, err = d.GetVipFromCache(ctx, u.Uid)
  102. log.Info("TestDao_SetVipCache get info1(%v), err(%v)", info, err)
  103. So(err, ShouldBeNil)
  104. So(info.Vip, ShouldEqual, 0)
  105. So(info.VipTime, ShouldEqual, model.TimeEmpty)
  106. So(info.Svip, ShouldEqual, 0)
  107. So(info.SvipTime, ShouldEqual, model.TimeEmpty)
  108. })
  109. // set valid info
  110. Convey("set valid info", func() {
  111. info = &model.VipInfo{
  112. Vip: 1,
  113. VipTime: time.Now().Add(time.Hour * 12).Format(model.TimeNano),
  114. Svip: 1,
  115. SvipTime: time.Now().Add(time.Hour * 6).Format(model.TimeNano),
  116. }
  117. err = d.SetVipCache(ctx, u.Uid, info)
  118. So(err, ShouldBeNil)
  119. info2, err := d.GetVipFromCache(ctx, u.Uid)
  120. log.Info("TestDao_SetVipCache get info2(%v), err(%v)", info, err)
  121. So(err, ShouldBeNil)
  122. So(info2.Vip, ShouldEqual, info.Vip)
  123. So(info2.VipTime, ShouldEqual, info.VipTime)
  124. So(info2.Svip, ShouldEqual, info.Svip)
  125. So(info2.SvipTime, ShouldEqual, info.SvipTime)
  126. })
  127. }))
  128. }
  129. func TestDao_ClearCache(t *testing.T) {
  130. initd()
  131. Convey("test clear cache", t, testWithTestUser(func(u *TestUser) {
  132. var (
  133. ctx = context.Background()
  134. err error
  135. conn = d.redis.Get(ctx)
  136. key = getUserCacheKey(u.Uid)
  137. )
  138. log.Info("TestDao_ClearCache uid(%d), key(%s)", u.Uid, key)
  139. // delete key at begin
  140. conn.Do("DEL", key)
  141. // del already deleted key
  142. Convey("del already deleted key", func() {
  143. err = d.ClearCache(ctx, u.Uid)
  144. So(err, ShouldBeNil)
  145. })
  146. // set valid info
  147. Convey("set valid info", func() {
  148. err = d.SetVipCache(ctx, u.Uid, nil)
  149. So(err, ShouldBeNil)
  150. err = d.ClearCache(ctx, u.Uid)
  151. So(err, ShouldBeNil)
  152. })
  153. }))
  154. }