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_channel_test.go 2.0 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestChannelQueue(t *testing.T) {
  12. handleChan := make(chan *testData)
  13. handle := func(data ...Data) {
  14. for _, datum := range data {
  15. testDatum := datum.(*testData)
  16. handleChan <- testDatum
  17. }
  18. }
  19. nilFn := func(_ context.Context, _ func()) {}
  20. queue, err := NewChannelQueue(handle,
  21. ChannelQueueConfiguration{
  22. QueueLength: 20,
  23. Workers: 1,
  24. MaxWorkers: 10,
  25. BlockTimeout: 1 * time.Second,
  26. BoostTimeout: 5 * time.Minute,
  27. BoostWorkers: 5,
  28. }, &testData{})
  29. assert.NoError(t, err)
  30. go queue.Run(nilFn, nilFn)
  31. test1 := testData{"A", 1}
  32. go queue.Push(&test1)
  33. result1 := <-handleChan
  34. assert.Equal(t, test1.TestString, result1.TestString)
  35. assert.Equal(t, test1.TestInt, result1.TestInt)
  36. err = queue.Push(test1)
  37. assert.Error(t, err)
  38. }
  39. func TestChannelQueue_Batch(t *testing.T) {
  40. handleChan := make(chan *testData)
  41. handle := func(data ...Data) {
  42. assert.True(t, len(data) == 2)
  43. for _, datum := range data {
  44. testDatum := datum.(*testData)
  45. handleChan <- testDatum
  46. }
  47. }
  48. nilFn := func(_ context.Context, _ func()) {}
  49. queue, err := NewChannelQueue(handle,
  50. ChannelQueueConfiguration{
  51. QueueLength: 20,
  52. BatchLength: 2,
  53. Workers: 1,
  54. MaxWorkers: 10,
  55. BlockTimeout: 1 * time.Second,
  56. BoostTimeout: 5 * time.Minute,
  57. BoostWorkers: 5,
  58. }, &testData{})
  59. assert.NoError(t, err)
  60. go queue.Run(nilFn, nilFn)
  61. test1 := testData{"A", 1}
  62. test2 := testData{"B", 2}
  63. queue.Push(&test1)
  64. go queue.Push(&test2)
  65. result1 := <-handleChan
  66. assert.Equal(t, test1.TestString, result1.TestString)
  67. assert.Equal(t, test1.TestInt, result1.TestInt)
  68. result2 := <-handleChan
  69. assert.Equal(t, test2.TestString, result2.TestString)
  70. assert.Equal(t, test2.TestInt, result2.TestInt)
  71. err = queue.Push(test1)
  72. assert.Error(t, err)
  73. }