You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

queue_disk_test.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package queue
  5. import (
  6. "os"
  7. "sync"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/modules/util"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestLevelQueue(t *testing.T) {
  14. handleChan := make(chan *testData)
  15. handle := func(data ...Data) {
  16. assert.True(t, len(data) == 2)
  17. for _, datum := range data {
  18. testDatum := datum.(*testData)
  19. handleChan <- testDatum
  20. }
  21. }
  22. var lock sync.Mutex
  23. queueShutdown := []func(){}
  24. queueTerminate := []func(){}
  25. tmpDir, err := os.MkdirTemp("", "level-queue-test-data")
  26. assert.NoError(t, err)
  27. defer util.RemoveAll(tmpDir)
  28. queue, err := NewLevelQueue(handle, LevelQueueConfiguration{
  29. ByteFIFOQueueConfiguration: ByteFIFOQueueConfiguration{
  30. WorkerPoolConfiguration: WorkerPoolConfiguration{
  31. QueueLength: 20,
  32. BatchLength: 2,
  33. BlockTimeout: 1 * time.Second,
  34. BoostTimeout: 5 * time.Minute,
  35. BoostWorkers: 5,
  36. MaxWorkers: 10,
  37. },
  38. Workers: 1,
  39. },
  40. DataDir: tmpDir,
  41. }, &testData{})
  42. assert.NoError(t, err)
  43. go queue.Run(func(shutdown func()) {
  44. lock.Lock()
  45. queueShutdown = append(queueShutdown, shutdown)
  46. lock.Unlock()
  47. }, func(terminate func()) {
  48. lock.Lock()
  49. queueTerminate = append(queueTerminate, terminate)
  50. lock.Unlock()
  51. })
  52. test1 := testData{"A", 1}
  53. test2 := testData{"B", 2}
  54. err = queue.Push(&test1)
  55. assert.NoError(t, err)
  56. go func() {
  57. err := queue.Push(&test2)
  58. assert.NoError(t, err)
  59. }()
  60. result1 := <-handleChan
  61. assert.Equal(t, test1.TestString, result1.TestString)
  62. assert.Equal(t, test1.TestInt, result1.TestInt)
  63. result2 := <-handleChan
  64. assert.Equal(t, test2.TestString, result2.TestString)
  65. assert.Equal(t, test2.TestInt, result2.TestInt)
  66. err = queue.Push(test1)
  67. assert.Error(t, err)
  68. lock.Lock()
  69. for _, callback := range queueShutdown {
  70. callback()
  71. }
  72. lock.Unlock()
  73. time.Sleep(200 * time.Millisecond)
  74. err = queue.Push(&test1)
  75. assert.NoError(t, err)
  76. err = queue.Push(&test2)
  77. assert.NoError(t, err)
  78. select {
  79. case <-handleChan:
  80. assert.Fail(t, "Handler processing should have stopped")
  81. default:
  82. }
  83. lock.Lock()
  84. for _, callback := range queueTerminate {
  85. callback()
  86. }
  87. lock.Unlock()
  88. // Reopen queue
  89. queue, err = NewWrappedQueue(handle,
  90. WrappedQueueConfiguration{
  91. Underlying: LevelQueueType,
  92. Config: LevelQueueConfiguration{
  93. ByteFIFOQueueConfiguration: ByteFIFOQueueConfiguration{
  94. WorkerPoolConfiguration: WorkerPoolConfiguration{
  95. QueueLength: 20,
  96. BatchLength: 2,
  97. BlockTimeout: 1 * time.Second,
  98. BoostTimeout: 5 * time.Minute,
  99. BoostWorkers: 5,
  100. MaxWorkers: 10,
  101. },
  102. Workers: 1,
  103. },
  104. DataDir: tmpDir,
  105. },
  106. }, &testData{})
  107. assert.NoError(t, err)
  108. go queue.Run(func(shutdown func()) {
  109. lock.Lock()
  110. queueShutdown = append(queueShutdown, shutdown)
  111. lock.Unlock()
  112. }, func(terminate func()) {
  113. lock.Lock()
  114. queueTerminate = append(queueTerminate, terminate)
  115. lock.Unlock()
  116. })
  117. result3 := <-handleChan
  118. assert.Equal(t, test1.TestString, result3.TestString)
  119. assert.Equal(t, test1.TestInt, result3.TestInt)
  120. result4 := <-handleChan
  121. assert.Equal(t, test2.TestString, result4.TestString)
  122. assert.Equal(t, test2.TestInt, result4.TestInt)
  123. lock.Lock()
  124. for _, callback := range queueShutdown {
  125. callback()
  126. }
  127. for _, callback := range queueTerminate {
  128. callback()
  129. }
  130. lock.Unlock()
  131. }