lazyqueue_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package prque
  17. import (
  18. "math/rand"
  19. "sync"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common/mclock"
  23. )
  24. const (
  25. testItems = 1000
  26. testPriorityStep = 100
  27. testSteps = 1000000
  28. testStepPeriod = time.Millisecond
  29. testQueueRefresh = time.Second
  30. testAvgRate = float64(testPriorityStep) / float64(testItems) / float64(testStepPeriod)
  31. )
  32. type lazyItem struct {
  33. p, maxp int64
  34. last mclock.AbsTime
  35. index int
  36. }
  37. func testPriority(a interface{}) int64 {
  38. return a.(*lazyItem).p
  39. }
  40. func testMaxPriority(a interface{}, until mclock.AbsTime) int64 {
  41. i := a.(*lazyItem)
  42. dt := until - i.last
  43. i.maxp = i.p + int64(float64(dt)*testAvgRate)
  44. return i.maxp
  45. }
  46. func testSetIndex(a interface{}, i int) {
  47. a.(*lazyItem).index = i
  48. }
  49. func TestLazyQueue(t *testing.T) {
  50. rand.Seed(time.Now().UnixNano())
  51. clock := &mclock.Simulated{}
  52. q := NewLazyQueue(testSetIndex, testPriority, testMaxPriority, clock, testQueueRefresh)
  53. var (
  54. items [testItems]lazyItem
  55. maxPri int64
  56. )
  57. for i := range items[:] {
  58. items[i].p = rand.Int63n(testPriorityStep * 10)
  59. if items[i].p > maxPri {
  60. maxPri = items[i].p
  61. }
  62. items[i].index = -1
  63. q.Push(&items[i])
  64. }
  65. var (
  66. lock sync.Mutex
  67. wg sync.WaitGroup
  68. stopCh = make(chan chan struct{})
  69. )
  70. defer wg.Wait()
  71. wg.Add(1)
  72. go func() {
  73. defer wg.Done()
  74. for {
  75. select {
  76. case <-clock.After(testQueueRefresh):
  77. lock.Lock()
  78. q.Refresh()
  79. lock.Unlock()
  80. case <-stopCh:
  81. return
  82. }
  83. }
  84. }()
  85. for c := 0; c < testSteps; c++ {
  86. i := rand.Intn(testItems)
  87. lock.Lock()
  88. items[i].p += rand.Int63n(testPriorityStep*2-1) + 1
  89. if items[i].p > maxPri {
  90. maxPri = items[i].p
  91. }
  92. items[i].last = clock.Now()
  93. if items[i].p > items[i].maxp {
  94. q.Update(items[i].index)
  95. }
  96. if rand.Intn(100) == 0 {
  97. p := q.PopItem().(*lazyItem)
  98. if p.p != maxPri {
  99. lock.Unlock()
  100. close(stopCh)
  101. t.Fatalf("incorrect item (best known priority %d, popped %d)", maxPri, p.p)
  102. }
  103. q.Push(p)
  104. }
  105. lock.Unlock()
  106. clock.Run(testStepPeriod)
  107. clock.WaitForTimers(1)
  108. }
  109. close(stopCh)
  110. }