periodicalexecutor_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package executors
  2. import (
  3. "runtime"
  4. "sync"
  5. "sync/atomic"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zeromicro/go-zero/core/proc"
  10. "github.com/zeromicro/go-zero/core/timex"
  11. )
  12. const threshold = 10
  13. type container struct {
  14. interval time.Duration
  15. tasks []int
  16. execute func(tasks any)
  17. }
  18. func newContainer(interval time.Duration, execute func(tasks any)) *container {
  19. return &container{
  20. interval: interval,
  21. execute: execute,
  22. }
  23. }
  24. func (c *container) AddTask(task any) bool {
  25. c.tasks = append(c.tasks, task.(int))
  26. return len(c.tasks) > threshold
  27. }
  28. func (c *container) Execute(tasks any) {
  29. if c.execute != nil {
  30. c.execute(tasks)
  31. } else {
  32. time.Sleep(c.interval)
  33. }
  34. }
  35. func (c *container) RemoveAll() any {
  36. tasks := c.tasks
  37. c.tasks = nil
  38. return tasks
  39. }
  40. func TestPeriodicalExecutor_Sync(t *testing.T) {
  41. var done int32
  42. exec := NewPeriodicalExecutor(time.Second, newContainer(time.Millisecond*500, nil))
  43. exec.Sync(func() {
  44. atomic.AddInt32(&done, 1)
  45. })
  46. assert.Equal(t, int32(1), atomic.LoadInt32(&done))
  47. }
  48. func TestPeriodicalExecutor_QuitGoroutine(t *testing.T) {
  49. ticker := timex.NewFakeTicker()
  50. exec := NewPeriodicalExecutor(time.Millisecond, newContainer(time.Millisecond, nil))
  51. exec.newTicker = func(d time.Duration) timex.Ticker {
  52. return ticker
  53. }
  54. routines := runtime.NumGoroutine()
  55. exec.Add(1)
  56. ticker.Tick()
  57. ticker.Wait(time.Millisecond * idleRound * 2)
  58. ticker.Tick()
  59. ticker.Wait(time.Millisecond * idleRound)
  60. assert.Equal(t, routines, runtime.NumGoroutine())
  61. proc.Shutdown()
  62. }
  63. func TestPeriodicalExecutor_Bulk(t *testing.T) {
  64. ticker := timex.NewFakeTicker()
  65. var vals []int
  66. // avoid data race
  67. var lock sync.Mutex
  68. exec := NewPeriodicalExecutor(time.Millisecond, newContainer(time.Millisecond, func(tasks any) {
  69. t := tasks.([]int)
  70. for _, each := range t {
  71. lock.Lock()
  72. vals = append(vals, each)
  73. lock.Unlock()
  74. }
  75. }))
  76. exec.newTicker = func(d time.Duration) timex.Ticker {
  77. return ticker
  78. }
  79. for i := 0; i < threshold*10; i++ {
  80. if i%threshold == 5 {
  81. time.Sleep(time.Millisecond * idleRound * 2)
  82. }
  83. exec.Add(i)
  84. }
  85. ticker.Tick()
  86. ticker.Wait(time.Millisecond * idleRound * 2)
  87. ticker.Tick()
  88. ticker.Tick()
  89. ticker.Wait(time.Millisecond * idleRound)
  90. var expect []int
  91. for i := 0; i < threshold*10; i++ {
  92. expect = append(expect, i)
  93. }
  94. lock.Lock()
  95. assert.EqualValues(t, expect, vals)
  96. lock.Unlock()
  97. }
  98. func TestPeriodicalExecutor_Wait(t *testing.T) {
  99. var lock sync.Mutex
  100. executer := NewBulkExecutor(func(tasks []any) {
  101. lock.Lock()
  102. defer lock.Unlock()
  103. time.Sleep(10 * time.Millisecond)
  104. }, WithBulkTasks(1), WithBulkInterval(time.Second))
  105. for i := 0; i < 10; i++ {
  106. executer.Add(1)
  107. }
  108. executer.Flush()
  109. executer.Wait()
  110. }
  111. func TestPeriodicalExecutor_WaitFast(t *testing.T) {
  112. const total = 3
  113. var cnt int
  114. var lock sync.Mutex
  115. executer := NewBulkExecutor(func(tasks []any) {
  116. defer func() {
  117. cnt++
  118. }()
  119. lock.Lock()
  120. defer lock.Unlock()
  121. time.Sleep(10 * time.Millisecond)
  122. }, WithBulkTasks(1), WithBulkInterval(10*time.Millisecond))
  123. for i := 0; i < total; i++ {
  124. executer.Add(2)
  125. }
  126. executer.Flush()
  127. executer.Wait()
  128. assert.Equal(t, total, cnt)
  129. }
  130. func TestPeriodicalExecutor_Deadlock(t *testing.T) {
  131. executor := NewBulkExecutor(func(tasks []any) {
  132. }, WithBulkTasks(1), WithBulkInterval(time.Millisecond))
  133. for i := 0; i < 1e5; i++ {
  134. executor.Add(1)
  135. }
  136. }
  137. func TestPeriodicalExecutor_hasTasks(t *testing.T) {
  138. ticker := timex.NewFakeTicker()
  139. defer ticker.Stop()
  140. exec := NewPeriodicalExecutor(time.Millisecond, newContainer(time.Millisecond, nil))
  141. exec.newTicker = func(d time.Duration) timex.Ticker {
  142. return ticker
  143. }
  144. assert.False(t, exec.hasTasks(nil))
  145. assert.True(t, exec.hasTasks(1))
  146. }
  147. // go test -benchtime 10s -bench .
  148. func BenchmarkExecutor(b *testing.B) {
  149. b.ReportAllocs()
  150. executor := NewPeriodicalExecutor(time.Second, newContainer(time.Millisecond*500, nil))
  151. for i := 0; i < b.N; i++ {
  152. executor.Add(1)
  153. }
  154. }