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.2KB

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