email_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package email
  2. import (
  3. "context"
  4. "github.com/smartystreets/goconvey/convey"
  5. "testing"
  6. "time"
  7. "go-common/app/job/main/videoup-report/model/email"
  8. "go-common/library/cache/redis"
  9. "sync"
  10. )
  11. var tplTest = &email.Template{
  12. Headers: map[string][]string{
  13. email.TO: {"chenxi01@bilibili.com"},
  14. email.SUBJECT: {"nothing at all"},
  15. },
  16. Body: "testhahaha",
  17. ContentType: "text/plain",
  18. }
  19. func TestEmailSendMail(t *testing.T) {
  20. convey.Convey("SendMail", t, func(ctx convey.C) {
  21. tplTest.Headers[email.FROM] = []string{d.email.Username}
  22. d.SendMail(tplTest)
  23. ctx.Convey("No return values", func(ctx convey.C) {
  24. })
  25. })
  26. }
  27. func TestEmailsendEmailLog(t *testing.T) {
  28. var (
  29. to = []string{"chenxi01@bilibili.com"}
  30. cc = []string{""}
  31. result = "成功"
  32. )
  33. convey.Convey("sendEmailLog", t, func(ctx convey.C) {
  34. d.sendEmailLog(tplTest, to, cc, result)
  35. ctx.Convey("No return values", func(ctx convey.C) {
  36. })
  37. })
  38. }
  39. //分片
  40. func batch(tk []int64, length int) (path [][]int64) {
  41. ll := len(tk) / length
  42. if len(tk)%length > 0 {
  43. ll++
  44. }
  45. path = [][]int64{}
  46. item := []int64{}
  47. for i := 0; i < len(tk); i++ {
  48. if i > 0 && i%length == 0 {
  49. path = append(path, item)
  50. item = []int64{}
  51. }
  52. item = append(item, tk[i])
  53. }
  54. if len(item) > 0 {
  55. path = append(path, item)
  56. }
  57. return
  58. }
  59. func TestEmailPushToRedis(t *testing.T) {
  60. uid := int64(123)
  61. uids := []int64{}
  62. speedThreshold := d.c.Mail.SpeedThreshold
  63. overlimit := speedThreshold * d.c.Mail.OverspeedThreshold
  64. for i := 0; i < overlimit*2; i++ {
  65. uids = append(uids, uid)
  66. }
  67. tplTest.UID = uid
  68. path := batch(uids, speedThreshold)
  69. len1 := len(uids)
  70. path = append(path, batch(uids, speedThreshold-1)...)
  71. len2 := 2 * len1
  72. path = append(path, batch(uids, speedThreshold)...)
  73. cnt := 0
  74. convey.Convey("连续发送邮件,间隔出现超限名额", t, func(ctx convey.C) {
  75. tplTest.Headers[email.FROM] = []string{d.email.Username}
  76. for index, task := range path {
  77. now := time.Now().UnixNano()
  78. for i := range task {
  79. cnt++
  80. isfast, key, err := d.PushToRedis(context.TODO(), tplTest)
  81. //_, _, err := d.PushToRedis(context.email.TODO(), tplTest)
  82. convey.So(err, convey.ShouldBeNil)
  83. t.Logf("cnt=%d,index=%d, i=%d, detector=%+v", cnt, index, i, d.detector)
  84. if cnt < overlimit+speedThreshold { //快速,探查阶段
  85. convey.So(isfast, convey.ShouldEqual, false)
  86. convey.So(key, convey.ShouldEqual, email.MailKey)
  87. } else if cnt == overlimit+speedThreshold { //快速,确认为超限,提供超限名单
  88. convey.So(isfast, convey.ShouldEqual, true)
  89. convey.So(key, convey.ShouldEqual, email.MailFastKey)
  90. } else if cnt < len1+speedThreshold { //快速,探查阶段,保留上一次的超限名单
  91. convey.So(isfast, convey.ShouldEqual, false)
  92. convey.So(key, convey.ShouldEqual, email.MailFastKey)
  93. } else if cnt < len2+overlimit+speedThreshold { //慢速/快速探查阶段,第一次慢速时清空上一次的超限名单
  94. convey.So(isfast, convey.ShouldEqual, false)
  95. convey.So(key, convey.ShouldEqual, email.MailKey)
  96. } else if cnt == len2+overlimit+speedThreshold { //快速,确认为超限,提供超限名单
  97. convey.So(isfast, convey.ShouldEqual, true)
  98. convey.So(key, convey.ShouldEqual, email.MailFastKey)
  99. } else { //快速,探查阶段,保留上一次的超限名单
  100. convey.So(isfast, convey.ShouldEqual, false)
  101. convey.So(key, convey.ShouldEqual, email.MailFastKey)
  102. }
  103. }
  104. if diff := now + 1e9 - time.Now().UnixNano(); diff > 0 {
  105. time.Sleep(time.Duration(diff))
  106. }
  107. }
  108. })
  109. }
  110. func TestEmailStart(t *testing.T) {
  111. convey.Convey("email Start", t, func(ctx convey.C) {
  112. err := d.Start(email.MailKey)
  113. convey.So(err, convey.ShouldBeNil)
  114. err = d.Start(email.MailKey + "_1")
  115. convey.So(err, convey.ShouldEqual, redis.ErrNil)
  116. })
  117. }
  118. func TestEmailBatchStart(t *testing.T) {
  119. wg := sync.WaitGroup{}
  120. convey.Convey("email Start\r\n", t, func(ctx convey.C) {
  121. wg.Add(1)
  122. go func() {
  123. defer wg.Done()
  124. for {
  125. err := d.Start(email.MailKey)
  126. t.Logf("start to push normal email, time=%d\r\n", time.Now().Unix())
  127. if err == redis.ErrNil {
  128. t.Logf("normal email stopped\r\n")
  129. break
  130. }
  131. ctx.So(err, convey.ShouldBeNil)
  132. }
  133. }()
  134. go func() {
  135. defer wg.Done()
  136. for {
  137. err := d.Start(email.MailFastKey)
  138. t.Logf("start to push fast email, time=%d\r\n", time.Now().Unix())
  139. if err == redis.ErrNil {
  140. t.Logf("fast email stopped\r\n")
  141. break
  142. }
  143. ctx.So(err, convey.ShouldBeNil)
  144. }
  145. }()
  146. wg.Wait()
  147. t.Logf("end")
  148. })
  149. }