service_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package service
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. "go-common/app/interface/main/push-archive/conf"
  10. "go-common/app/interface/main/push-archive/model"
  11. time2 "go-common/library/time"
  12. "github.com/smartystreets/goconvey/convey"
  13. )
  14. var s *Service
  15. func initd() {
  16. dir, _ := filepath.Abs("../cmd/push-archive-test.toml")
  17. flag.Set("conf", dir)
  18. conf.Init()
  19. s = New(conf.Conf)
  20. }
  21. //普通关注的3个实验组比列均为5%,尾号分别为:00~04, 05~09, 10~14
  22. func initd2() {
  23. dir, _ := filepath.Abs("../cmd/push-archive-test.toml")
  24. flag.Set("conf", dir)
  25. conf.Init()
  26. conf.Conf.ArcPush.UpperLimitExpire = time2.Duration(2 * time.Second)
  27. ps := []conf.Proportion{}
  28. for i := 0; i < 5; i++ {
  29. p := conf.Proportion{
  30. Proportion: "0.05",
  31. ProportionStartFrom: fmt.Sprintf("%d", i*5),
  32. }
  33. ps = append(ps, p)
  34. }
  35. conf.Conf.ArcPush.Proportions = ps
  36. s = New(conf.Conf)
  37. }
  38. func Test_todaytime(t *testing.T) {
  39. initd()
  40. todayTime, err := s.getTodayTime("06:10:00")
  41. convey.Convey("当日定时时间", t, func() {
  42. convey.So(err, convey.ShouldBeNil)
  43. })
  44. t.Logf("todaytime(%s) unix(%d)", todayTime.Format("2006-01-02 15:04:05"), todayTime.Unix())
  45. }
  46. func Test_forbidTime(t *testing.T) {
  47. initd()
  48. t.Logf("the forbidtimes(%v)\n", s.ForbidTimes)
  49. forbid, err := s.isForbidTime()
  50. convey.Convey("禁止时间判断", t, func() {
  51. convey.So(err, convey.ShouldBeNil)
  52. convey.So(forbid, convey.ShouldEqual, false)
  53. })
  54. }
  55. func Test_deadline(t *testing.T) {
  56. initd()
  57. deadline, err := s.getDeadline()
  58. convey.Convey("最近第3天的凌晨", t, func() {
  59. convey.So(err, convey.ShouldBeNil)
  60. })
  61. t.Logf("the deadline(%s) unix(%d)", deadline.Format("2006-01-02 15:04:05"), deadline.Unix())
  62. }
  63. func Test_proportion(t *testing.T) {
  64. initd2()
  65. fans := map[int64]int{
  66. 100014: model.RelationAttention,
  67. 100015: model.RelationAttention,
  68. 100016: model.RelationAttention,
  69. 100017: model.RelationAttention,
  70. 100038: model.RelationAttention,
  71. 100029: model.RelationAttention,
  72. 100070: model.RelationAttention,
  73. 100071: model.RelationAttention,
  74. 100072: model.RelationAttention,
  75. 100073: model.RelationSpecial,
  76. 10034: model.RelationSpecial,
  77. 10038: model.RelationSpecial,
  78. }
  79. expected := map[int]int{
  80. 1: 4,
  81. 2: 3,
  82. }
  83. attentions, specials := s.dao.FansByProportion(121231, fans)
  84. convey.Convey("根据比列过滤各组", t, func() {
  85. convey.So(len(attentions), convey.ShouldEqual, expected[1])
  86. convey.So(len(specials), convey.ShouldEqual, expected[2])
  87. })
  88. }
  89. func Test_Group(t *testing.T) {
  90. initd2()
  91. upper := int64(27515256)
  92. //upper主所有粉丝
  93. fans, err := s.dao.Fans(context.TODO(), upper, false)
  94. convey.Convey("获取所有粉丝", t, func() {
  95. convey.So(err, convey.ShouldBeNil)
  96. convey.So(len(fans), convey.ShouldEqual, 37)
  97. })
  98. attentions, specials := s.dao.FansByProportion(upper, fans)
  99. t.Logf("attentions(%d), specials(%d)", len(attentions), len(specials))
  100. convey.Convey("没有hitby,则没有命中任何分组", t, func() {
  101. for _, g := range s.dao.FanGroups {
  102. g.Hitby = ""
  103. }
  104. list := s.group(upper, fans)
  105. convey.So(len(list), convey.ShouldEqual, 0)
  106. })
  107. convey.Convey("hitby=default,只命中2组", t, func() {
  108. s.dao.GroupOrder = []string{"1#attention", "1#ai:pushlist_play_recent", "2#special"}
  109. for _, g := range s.dao.FanGroups {
  110. g.Hitby = model.GroupDataTypeDefault
  111. }
  112. list := s.group(upper, fans)
  113. convey.So(len(list), convey.ShouldEqual, 3)
  114. convey.So(len(list["1#attention"]), convey.ShouldEqual, len(attentions))
  115. convey.So(len(list["1#ai:pushlist_play_recent"]), convey.ShouldEqual, 0)
  116. convey.So(len(list["2#special"]), convey.ShouldEqual, len(specials))
  117. })
  118. convey.Convey("hitby=hbase,根据表命中获取", t, func() {
  119. s.dao.GroupOrder = []string{"1#ai:pushlist_play_recent", "1#ai:pushlist_offline_up", "1#ai:pushlist_follow_recent", "1#attention", "2#special"}
  120. for _, g := range s.dao.FanGroups {
  121. if g.RelationType == 1 {
  122. g.Hitby = "hbase"
  123. }
  124. }
  125. list := s.group(upper, fans)
  126. t.Logf("list(%+v)", list)
  127. })
  128. //map[1#ai:pushlist_follow_recent:[]
  129. // 2#special:[21231134 4235023 1232032 2089809 88889018]
  130. // 1#ai:pushlist_play_recent:[27515303 27515311 27515317 27515300 27515401 27515306]
  131. // 1#ai:pushlist_offline_up:[]]
  132. expect := map[string]int{
  133. "1#ai:pushlist_follow_recent": 1,
  134. "1#ai:pushlist_play_recent": 5,
  135. "1#ai:pushlist_offline_up": 1,
  136. "2#special": 5,
  137. }
  138. convey.Convey("实验组粉丝优先级分组", t, func() {
  139. hit := s.group(upper, fans)
  140. t.Logf("the hits(%v)", hit)
  141. for gkey, f := range hit {
  142. convey.So(len(f), convey.ShouldEqual, expect[gkey])
  143. }
  144. })
  145. }
  146. func Test_manytimes(t *testing.T) {
  147. initd2()
  148. upper := int64(27515256)
  149. mids1, len1, err1 := s.fans(upper, 1020, false)
  150. time.Sleep(time.Second * 2)
  151. mids2, len2, err2 := s.fans(upper, 1030, false)
  152. convey.Convey(" 同一up主多次获取过滤后的粉丝,第二次比第一次少", t, func() {
  153. convey.So(err1, convey.ShouldBeNil)
  154. convey.So(err2, convey.ShouldBeNil)
  155. convey.So(len1, convey.ShouldBeGreaterThan, 0)
  156. convey.So(len2, convey.ShouldBeGreaterThan, 0)
  157. convey.So(len2, convey.ShouldBeLessThanOrEqualTo, len1)
  158. })
  159. t.Logf("the mids1(%v)\n the mids2(%v)\n", mids1, mids2)
  160. }