detector_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package email
  2. import (
  3. "testing"
  4. "github.com/smartystreets/goconvey/convey"
  5. "time"
  6. )
  7. var fd *FastDetector
  8. var speedThreshold = 3
  9. var overspeedThreshold = 2
  10. var overlimit = speedThreshold * overspeedThreshold
  11. func TestEmailnewFastDetector(t *testing.T) {
  12. convey.Convey("newFastDetector", t, func(ctx convey.C) {
  13. fd = NewFastDetector(speedThreshold, overspeedThreshold)
  14. ctx.Convey("Then fd should not be nil.", func(ctx convey.C) {
  15. ctx.So(fd, convey.ShouldNotBeNil)
  16. })
  17. })
  18. }
  19. //一秒多少个
  20. func limit(t *testing.T, ids []int64) {
  21. now := time.Now().UnixNano()
  22. limit := len(ids)
  23. for i := 0; i < limit; i++ {
  24. unique := ids[i]
  25. res := fd.Detect(unique)
  26. hit := fd.IsFastUnique(unique)
  27. t.Logf("index=%d, unique=%d, res=%v, hit=%v, detector(%+v)", i, unique, res, hit, fd)
  28. }
  29. if diff := now + 1e9 - time.Now().UnixNano(); diff > 0 {
  30. time.Sleep(time.Duration(diff))
  31. }
  32. return
  33. }
  34. //分片
  35. func batch(tk []int64, length int) (path [][]int64) {
  36. ll := len(tk) / length
  37. if len(tk)%length > 0 {
  38. ll++
  39. }
  40. path = [][]int64{}
  41. item := []int64{}
  42. for i := 0; i < len(tk); i++ {
  43. if i > 0 && i%length == 0 {
  44. path = append(path, item)
  45. item = []int64{}
  46. }
  47. item = append(item, tk[i])
  48. }
  49. if len(item) > 0 {
  50. path = append(path, item)
  51. }
  52. return
  53. }
  54. func TestEmaildetect(t *testing.T) {
  55. TestEmailnewFastDetector(t)
  56. ids := []int64{222, 333}
  57. //convey.Convey("慢速,无超限名额", t, func() {
  58. // tk := []int64{}
  59. // for i := 1; i <= overlimit*2; i++ {
  60. // tk = append(tk, ids[0])
  61. // }
  62. // path := batch(tk, speedThreshold-1)
  63. // for _, tk := range path {
  64. // limit(t, tk)
  65. // }
  66. //})
  67. //return
  68. //convey.Convey("部分超限,但未超次数,无超限名额", t, func() {
  69. // tk := []int64{}
  70. // for i := 1; i < overlimit; i++ {
  71. // tk = append(tk, ids[0])
  72. // }
  73. // path := [][]int64{tk}
  74. // path = append(path, batch(tk, speedThreshold-1)...)
  75. // for _, tk := range path {
  76. // limit(t, tk)
  77. // }
  78. //})
  79. //return
  80. convey.Convey("部分超限且超次数,没有回落,有超限名额且被替代, 几秒后再超限,但没有超限名单", t, func() {
  81. tk := []int64{}
  82. tk2 := []int64{}
  83. for i := 1; i < overlimit*2; i++ {
  84. tk = append(tk, ids[0])
  85. tk2 = append(tk2, ids[1])
  86. }
  87. path := [][]int64{tk}
  88. path = append(path, batch(tk, speedThreshold+1)...)
  89. path = append(path, batch(tk2, speedThreshold+1)...)
  90. for _, tk := range path {
  91. limit(t, tk)
  92. }
  93. limit(t, path[len(path)-1])
  94. sl := int64(5)
  95. time.Sleep(time.Duration(sl) * time.Second)
  96. t.Logf("after %ds sleep", sl)
  97. limit(t, path[len(path)-1])
  98. })
  99. convey.Convey("部分超限且超次数,有超限名额,但有回落,没有超限名额", t, func(ctx convey.C) {
  100. tk := []int64{}
  101. for i := 1; i < overlimit*2; i++ {
  102. tk = append(tk, ids[0])
  103. }
  104. path := batch(tk, speedThreshold+1)
  105. path = append(path, batch(tk, speedThreshold-1)...)
  106. for _, tk := range path {
  107. limit(t, tk)
  108. }
  109. })
  110. }