common_message_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package service
  2. import (
  3. . "github.com/smartystreets/goconvey/convey"
  4. "go-common/app/interface/live/push-live/dao"
  5. "go-common/app/interface/live/push-live/model"
  6. "go-common/library/cache/redis"
  7. "math/rand"
  8. "strconv"
  9. "testing"
  10. )
  11. func makeTestCommonPushTask(title, body, linkValue, group string, business, expireTime int) (task *model.ApPushTask) {
  12. m := &model.LiveCommonMessage{}
  13. m.MsgContent = model.LiveCommonMessageContent{
  14. Business: business,
  15. Group: group,
  16. Mids: "",
  17. AlertTitle: title,
  18. AlertBody: body,
  19. LinkValue: linkValue,
  20. ExpireTime: expireTime,
  21. }
  22. task = s.InitCommonTask(m)
  23. return
  24. }
  25. func TestService_InitCommonTask(t *testing.T) {
  26. initd()
  27. Convey("should return init struct", t, func() {
  28. title := "room_title"
  29. body := "测试"
  30. group := "group"
  31. linkValue := strconv.Itoa(rand.Intn(9999))
  32. expireTime := rand.Intn(10000) + 1
  33. business := rand.Intn(9999)
  34. task := makeTestCommonPushTask(title, body, linkValue, group, business, expireTime)
  35. So(task.AlertTitle, ShouldResemble, title)
  36. So(task.AlertBody, ShouldResemble, body)
  37. So(task.ExpireTime, ShouldResemble, expireTime)
  38. So(task.LinkValue, ShouldResemble, linkValue)
  39. So(task.MidSource, ShouldEqual, business)
  40. So(task.Group, ShouldEqual, group)
  41. })
  42. }
  43. func TestService_setPushInterval(t *testing.T) {
  44. initd()
  45. Convey("test setPushInterval", t, func() {
  46. var (
  47. resTotal int
  48. total int
  49. business int
  50. task *model.ApPushTask
  51. mids []int64
  52. err error
  53. )
  54. Convey("test business will not exec logic", func() {
  55. business = rand.Intn(100)
  56. task = &model.ApPushTask{}
  57. total = 10
  58. mids = makeMids(total)
  59. resTotal, err = s.setPushInterval(business, rand.Int31(), mids, task)
  60. So(err, ShouldBeNil)
  61. So(resTotal, ShouldEqual, 0)
  62. })
  63. Convey("test business will exec logic", func() {
  64. var conn redis.Conn
  65. business = 111
  66. task = &model.ApPushTask{
  67. LinkValue: "test",
  68. }
  69. total = 10
  70. mids = makeMids(total)
  71. resTotal, err = s.setPushInterval(business, 300, mids, task)
  72. So(err, ShouldBeNil)
  73. So(resTotal, ShouldEqual, total)
  74. // clean
  75. conn, err = redis.Dial(s.c.Redis.PushInterval.Proto, s.c.Redis.PushInterval.Addr, s.dao.RedisOption()...)
  76. So(err, ShouldBeNil)
  77. for _, mid := range mids {
  78. key := dao.GetIntervalKey(mid)
  79. conn.Do("DEL", key)
  80. }
  81. conn.Close()
  82. })
  83. })
  84. }
  85. func makeMids(total int) []int64 {
  86. mids := make([]int64, 0, total)
  87. for i := 0; i < total; i++ {
  88. mids = append(mids, rand.Int63())
  89. }
  90. return mids
  91. }