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

queue_disk_test.go 3.1 kB

Graceful Queues: Issue Indexing and Tasks (#9363) * Queue: Add generic graceful queues with settings * Queue & Setting: Add worker pool implementation * Queue: Add worker settings * Queue: Make resizing worker pools * Queue: Add name variable to queues * Queue: Add monitoring * Queue: Improve logging * Issues: Gracefulise the issues indexer Remove the old now unused specific queues * Task: Move to generic queue and gracefulise * Issues: Standardise the issues indexer queue settings * Fix test * Queue: Allow Redis to connect to unix * Prevent deadlock during early shutdown of issue indexer * Add MaxWorker settings to queues * Merge branch 'master' into graceful-queues * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_disk.go * Update modules/queue/queue_disk_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Rename queue.Description to queue.ManagedQueue as per @guillep2k * Cancel pool workers when removed * Remove dependency on queue from setting * Update modules/queue/queue_redis.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * As per @guillep2k add mutex locks on shutdown/terminate * move unlocking out of setInternal * Add warning if number of workers < 0 * Small changes as per @guillep2k * No redis host specified not found * Clean up documentation for queues * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md * Update modules/indexer/issues/indexer_test.go * Ensure that persistable channel queue is added to manager * Rename QUEUE_NAME REDIS_QUEUE_NAME * Revert "Rename QUEUE_NAME REDIS_QUEUE_NAME" This reverts commit 1f83b4fc9b9dabda186257b38c265fe7012f90df. Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
5 years ago
Graceful Queues: Issue Indexing and Tasks (#9363) * Queue: Add generic graceful queues with settings * Queue & Setting: Add worker pool implementation * Queue: Add worker settings * Queue: Make resizing worker pools * Queue: Add name variable to queues * Queue: Add monitoring * Queue: Improve logging * Issues: Gracefulise the issues indexer Remove the old now unused specific queues * Task: Move to generic queue and gracefulise * Issues: Standardise the issues indexer queue settings * Fix test * Queue: Allow Redis to connect to unix * Prevent deadlock during early shutdown of issue indexer * Add MaxWorker settings to queues * Merge branch 'master' into graceful-queues * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_disk.go * Update modules/queue/queue_disk_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Rename queue.Description to queue.ManagedQueue as per @guillep2k * Cancel pool workers when removed * Remove dependency on queue from setting * Update modules/queue/queue_redis.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * As per @guillep2k add mutex locks on shutdown/terminate * move unlocking out of setInternal * Add warning if number of workers < 0 * Small changes as per @guillep2k * No redis host specified not found * Clean up documentation for queues * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md * Update modules/indexer/issues/indexer_test.go * Ensure that persistable channel queue is added to manager * Rename QUEUE_NAME REDIS_QUEUE_NAME * Revert "Rename QUEUE_NAME REDIS_QUEUE_NAME" This reverts commit 1f83b4fc9b9dabda186257b38c265fe7012f90df. Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
5 years ago
Graceful Queues: Issue Indexing and Tasks (#9363) * Queue: Add generic graceful queues with settings * Queue & Setting: Add worker pool implementation * Queue: Add worker settings * Queue: Make resizing worker pools * Queue: Add name variable to queues * Queue: Add monitoring * Queue: Improve logging * Issues: Gracefulise the issues indexer Remove the old now unused specific queues * Task: Move to generic queue and gracefulise * Issues: Standardise the issues indexer queue settings * Fix test * Queue: Allow Redis to connect to unix * Prevent deadlock during early shutdown of issue indexer * Add MaxWorker settings to queues * Merge branch 'master' into graceful-queues * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/indexer/issues/indexer.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Update modules/queue/queue_disk.go * Update modules/queue/queue_disk_channel.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Rename queue.Description to queue.ManagedQueue as per @guillep2k * Cancel pool workers when removed * Remove dependency on queue from setting * Update modules/queue/queue_redis.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * As per @guillep2k add mutex locks on shutdown/terminate * move unlocking out of setInternal * Add warning if number of workers < 0 * Small changes as per @guillep2k * No redis host specified not found * Clean up documentation for queues * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md * Update modules/indexer/issues/indexer_test.go * Ensure that persistable channel queue is added to manager * Rename QUEUE_NAME REDIS_QUEUE_NAME * Revert "Rename QUEUE_NAME REDIS_QUEUE_NAME" This reverts commit 1f83b4fc9b9dabda186257b38c265fe7012f90df. Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "context"
  7. "io/ioutil"
  8. "os"
  9. "testing"
  10. "time"
  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. queueShutdown := []func(){}
  23. queueTerminate := []func(){}
  24. tmpDir, err := ioutil.TempDir("", "level-queue-test-data")
  25. assert.NoError(t, err)
  26. defer os.RemoveAll(tmpDir)
  27. queue, err := NewLevelQueue(handle, LevelQueueConfiguration{
  28. WorkerPoolConfiguration: WorkerPoolConfiguration{
  29. QueueLength: 20,
  30. BatchLength: 2,
  31. BlockTimeout: 1 * time.Second,
  32. BoostTimeout: 5 * time.Minute,
  33. BoostWorkers: 5,
  34. MaxWorkers: 10,
  35. },
  36. DataDir: tmpDir,
  37. Workers: 1,
  38. }, &testData{})
  39. assert.NoError(t, err)
  40. go queue.Run(func(_ context.Context, shutdown func()) {
  41. queueShutdown = append(queueShutdown, shutdown)
  42. }, func(_ context.Context, terminate func()) {
  43. queueTerminate = append(queueTerminate, terminate)
  44. })
  45. test1 := testData{"A", 1}
  46. test2 := testData{"B", 2}
  47. err = queue.Push(&test1)
  48. assert.NoError(t, err)
  49. go func() {
  50. err = queue.Push(&test2)
  51. assert.NoError(t, err)
  52. }()
  53. result1 := <-handleChan
  54. assert.Equal(t, test1.TestString, result1.TestString)
  55. assert.Equal(t, test1.TestInt, result1.TestInt)
  56. result2 := <-handleChan
  57. assert.Equal(t, test2.TestString, result2.TestString)
  58. assert.Equal(t, test2.TestInt, result2.TestInt)
  59. err = queue.Push(test1)
  60. assert.Error(t, err)
  61. for _, callback := range queueShutdown {
  62. callback()
  63. }
  64. time.Sleep(200 * time.Millisecond)
  65. err = queue.Push(&test1)
  66. assert.NoError(t, err)
  67. err = queue.Push(&test2)
  68. assert.NoError(t, err)
  69. select {
  70. case <-handleChan:
  71. assert.Fail(t, "Handler processing should have stopped")
  72. default:
  73. }
  74. for _, callback := range queueTerminate {
  75. callback()
  76. }
  77. // Reopen queue
  78. queue, err = NewWrappedQueue(handle,
  79. WrappedQueueConfiguration{
  80. Underlying: LevelQueueType,
  81. Config: LevelQueueConfiguration{
  82. WorkerPoolConfiguration: WorkerPoolConfiguration{
  83. QueueLength: 20,
  84. BatchLength: 2,
  85. BlockTimeout: 1 * time.Second,
  86. BoostTimeout: 5 * time.Minute,
  87. BoostWorkers: 5,
  88. MaxWorkers: 10,
  89. },
  90. DataDir: tmpDir,
  91. Workers: 1,
  92. },
  93. }, &testData{})
  94. assert.NoError(t, err)
  95. go queue.Run(func(_ context.Context, shutdown func()) {
  96. queueShutdown = append(queueShutdown, shutdown)
  97. }, func(_ context.Context, terminate func()) {
  98. queueTerminate = append(queueTerminate, terminate)
  99. })
  100. result3 := <-handleChan
  101. assert.Equal(t, test1.TestString, result3.TestString)
  102. assert.Equal(t, test1.TestInt, result3.TestInt)
  103. result4 := <-handleChan
  104. assert.Equal(t, test2.TestString, result4.TestString)
  105. assert.Equal(t, test2.TestInt, result4.TestInt)
  106. for _, callback := range queueShutdown {
  107. callback()
  108. }
  109. for _, callback := range queueTerminate {
  110. callback()
  111. }
  112. }