rollingwindow_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package collection
  2. import (
  3. "math/rand"
  4. "testing"
  5. "time"
  6. "git.i2edu.net/i2/go-zero/core/stringx"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. const duration = time.Millisecond * 50
  10. func TestNewRollingWindow(t *testing.T) {
  11. assert.NotNil(t, NewRollingWindow(10, time.Second))
  12. assert.Panics(t, func() {
  13. NewRollingWindow(0, time.Second)
  14. })
  15. }
  16. func TestRollingWindowAdd(t *testing.T) {
  17. const size = 3
  18. r := NewRollingWindow(size, duration)
  19. listBuckets := func() []float64 {
  20. var buckets []float64
  21. r.Reduce(func(b *Bucket) {
  22. buckets = append(buckets, b.Sum)
  23. })
  24. return buckets
  25. }
  26. assert.Equal(t, []float64{0, 0, 0}, listBuckets())
  27. r.Add(1)
  28. assert.Equal(t, []float64{0, 0, 1}, listBuckets())
  29. elapse()
  30. r.Add(2)
  31. r.Add(3)
  32. assert.Equal(t, []float64{0, 1, 5}, listBuckets())
  33. elapse()
  34. r.Add(4)
  35. r.Add(5)
  36. r.Add(6)
  37. assert.Equal(t, []float64{1, 5, 15}, listBuckets())
  38. elapse()
  39. r.Add(7)
  40. assert.Equal(t, []float64{5, 15, 7}, listBuckets())
  41. }
  42. func TestRollingWindowReset(t *testing.T) {
  43. const size = 3
  44. r := NewRollingWindow(size, duration, IgnoreCurrentBucket())
  45. listBuckets := func() []float64 {
  46. var buckets []float64
  47. r.Reduce(func(b *Bucket) {
  48. buckets = append(buckets, b.Sum)
  49. })
  50. return buckets
  51. }
  52. r.Add(1)
  53. elapse()
  54. assert.Equal(t, []float64{0, 1}, listBuckets())
  55. elapse()
  56. assert.Equal(t, []float64{1}, listBuckets())
  57. elapse()
  58. assert.Nil(t, listBuckets())
  59. // cross window
  60. r.Add(1)
  61. time.Sleep(duration * 10)
  62. assert.Nil(t, listBuckets())
  63. }
  64. func TestRollingWindowReduce(t *testing.T) {
  65. const size = 4
  66. tests := []struct {
  67. win *RollingWindow
  68. expect float64
  69. }{
  70. {
  71. win: NewRollingWindow(size, duration),
  72. expect: 10,
  73. },
  74. {
  75. win: NewRollingWindow(size, duration, IgnoreCurrentBucket()),
  76. expect: 4,
  77. },
  78. }
  79. for _, test := range tests {
  80. t.Run(stringx.Rand(), func(t *testing.T) {
  81. r := test.win
  82. for x := 0; x < size; x++ {
  83. for i := 0; i <= x; i++ {
  84. r.Add(float64(i))
  85. }
  86. if x < size-1 {
  87. elapse()
  88. }
  89. }
  90. var result float64
  91. r.Reduce(func(b *Bucket) {
  92. result += b.Sum
  93. })
  94. assert.Equal(t, test.expect, result)
  95. })
  96. }
  97. }
  98. func TestRollingWindowBucketTimeBoundary(t *testing.T) {
  99. const size = 3
  100. interval := time.Millisecond * 30
  101. r := NewRollingWindow(size, interval)
  102. listBuckets := func() []float64 {
  103. var buckets []float64
  104. r.Reduce(func(b *Bucket) {
  105. buckets = append(buckets, b.Sum)
  106. })
  107. return buckets
  108. }
  109. assert.Equal(t, []float64{0, 0, 0}, listBuckets())
  110. r.Add(1)
  111. assert.Equal(t, []float64{0, 0, 1}, listBuckets())
  112. time.Sleep(time.Millisecond * 45)
  113. r.Add(2)
  114. r.Add(3)
  115. assert.Equal(t, []float64{0, 1, 5}, listBuckets())
  116. // sleep time should be less than interval, and make the bucket change happen
  117. time.Sleep(time.Millisecond * 20)
  118. r.Add(4)
  119. r.Add(5)
  120. r.Add(6)
  121. assert.Equal(t, []float64{1, 5, 15}, listBuckets())
  122. time.Sleep(time.Millisecond * 100)
  123. r.Add(7)
  124. r.Add(8)
  125. r.Add(9)
  126. assert.Equal(t, []float64{0, 0, 24}, listBuckets())
  127. }
  128. func TestRollingWindowDataRace(t *testing.T) {
  129. const size = 3
  130. r := NewRollingWindow(size, duration)
  131. stop := make(chan bool)
  132. go func() {
  133. for {
  134. select {
  135. case <-stop:
  136. return
  137. default:
  138. r.Add(float64(rand.Int63()))
  139. time.Sleep(duration / 2)
  140. }
  141. }
  142. }()
  143. go func() {
  144. for {
  145. select {
  146. case <-stop:
  147. return
  148. default:
  149. r.Reduce(func(b *Bucket) {})
  150. }
  151. }
  152. }()
  153. time.Sleep(duration * 5)
  154. close(stop)
  155. }
  156. func elapse() {
  157. time.Sleep(duration)
  158. }