redis_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "testing"
  6. "time"
  7. "go-common/app/interface/main/push-archive/model"
  8. "github.com/smartystreets/goconvey/convey"
  9. )
  10. func Test_upperlimit(t *testing.T) {
  11. upper := int64(998)
  12. d.UpperLimitExpire = 1 // 1s
  13. exist, err := d.ExistUpperLimitCache(context.TODO(), upper)
  14. convey.Convey("upper主推送频率限制,没存储过", t, func() {
  15. convey.So(err, convey.ShouldBeNil)
  16. convey.So(exist, convey.ShouldEqual, false)
  17. })
  18. err = d.AddUpperLimitCache(context.TODO(), upper)
  19. convey.Convey("upper主推送频率限制,添加推送1次,再次获取已存在, 失效后不存在", t, func() {
  20. convey.So(err, convey.ShouldBeNil)
  21. exist, err = d.ExistUpperLimitCache(context.TODO(), upper)
  22. convey.So(err, convey.ShouldBeNil)
  23. convey.So(exist, convey.ShouldEqual, true)
  24. time.Sleep(2 * time.Second)
  25. exist, err = d.ExistUpperLimitCache(context.TODO(), upper)
  26. convey.So(err, convey.ShouldBeNil)
  27. convey.So(exist, convey.ShouldEqual, false)
  28. })
  29. }
  30. func Test_statisticscache(t *testing.T) {
  31. ps, err := d.GetStatisticsCache(context.TODO())
  32. convey.Convey("从redis获取统计数据, 没有数据", t, func() {
  33. convey.So(err, convey.ShouldBeNil)
  34. convey.So(ps, convey.ShouldBeNil)
  35. })
  36. per := int64(1000)
  37. start := int64(1000000)
  38. var mids []int64
  39. for i := start; i < start+per; i++ {
  40. mids = append(mids, i)
  41. }
  42. midscount := len(mids)
  43. midsstr, _ := json.Marshal(mids)
  44. ps = &model.PushStatistic{
  45. Aid: int64(121321),
  46. Group: "ai:pushlist_offline_up",
  47. Type: 1,
  48. Mids: string(midsstr),
  49. MidsCounter: midscount,
  50. CTime: time.Now(),
  51. }
  52. err = d.AddStatisticsCache(context.TODO(), ps)
  53. convey.Convey("添加统计数据到redis", t, func() {
  54. convey.So(err, convey.ShouldBeNil)
  55. })
  56. }
  57. func Test_perupperlimit(t *testing.T) {
  58. upper := int64(10)
  59. fan := int64(20)
  60. err := d.AddPerUpperLimitCache(context.TODO(), fan, upper, 1, 1)
  61. convey.Convey("添加推送次数限制", t, func() {
  62. convey.So(err, convey.ShouldEqual, nil)
  63. })
  64. total, err := d.GetPerUpperLimitCache(context.TODO(), fan, upper)
  65. convey.Convey("获取推送次数限制, 失效后不存在", t, func() {
  66. convey.So(err, convey.ShouldEqual, nil)
  67. convey.So(total, convey.ShouldEqual, 1)
  68. time.Sleep(time.Second * 2)
  69. total, err = d.GetPerUpperLimitCache(context.TODO(), fan, upper)
  70. convey.So(err, convey.ShouldEqual, nil)
  71. convey.So(total, convey.ShouldEqual, 0)
  72. })
  73. }