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.

base_test.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package queue
  4. import (
  5. "context"
  6. "fmt"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func testQueueBasic(t *testing.T, newFn func(cfg *BaseConfig) (baseQueue, error), cfg *BaseConfig, isUnique bool) {
  12. t.Run(fmt.Sprintf("testQueueBasic-%s-unique:%v", cfg.ManagedName, isUnique), func(t *testing.T) {
  13. q, err := newFn(cfg)
  14. assert.NoError(t, err)
  15. ctx := context.Background()
  16. _ = q.RemoveAll(ctx)
  17. cnt, err := q.Len(ctx)
  18. assert.NoError(t, err)
  19. assert.EqualValues(t, 0, cnt)
  20. // push the first item
  21. err = q.PushItem(ctx, []byte("foo"))
  22. assert.NoError(t, err)
  23. cnt, err = q.Len(ctx)
  24. assert.NoError(t, err)
  25. assert.EqualValues(t, 1, cnt)
  26. // push a duplicate item
  27. err = q.PushItem(ctx, []byte("foo"))
  28. if !isUnique {
  29. assert.NoError(t, err)
  30. } else {
  31. assert.ErrorIs(t, err, ErrAlreadyInQueue)
  32. }
  33. // check the duplicate item
  34. cnt, err = q.Len(ctx)
  35. assert.NoError(t, err)
  36. has, err := q.HasItem(ctx, []byte("foo"))
  37. assert.NoError(t, err)
  38. if !isUnique {
  39. assert.EqualValues(t, 2, cnt)
  40. assert.EqualValues(t, false, has) // non-unique queues don't check for duplicates
  41. } else {
  42. assert.EqualValues(t, 1, cnt)
  43. assert.EqualValues(t, true, has)
  44. }
  45. // push another item
  46. err = q.PushItem(ctx, []byte("bar"))
  47. assert.NoError(t, err)
  48. // pop the first item (and the duplicate if non-unique)
  49. it, err := q.PopItem(ctx)
  50. assert.NoError(t, err)
  51. assert.EqualValues(t, "foo", string(it))
  52. if !isUnique {
  53. it, err = q.PopItem(ctx)
  54. assert.NoError(t, err)
  55. assert.EqualValues(t, "foo", string(it))
  56. }
  57. // pop another item
  58. it, err = q.PopItem(ctx)
  59. assert.NoError(t, err)
  60. assert.EqualValues(t, "bar", string(it))
  61. // pop an empty queue (timeout, cancel)
  62. ctxTimed, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
  63. it, err = q.PopItem(ctxTimed)
  64. assert.ErrorIs(t, err, context.DeadlineExceeded)
  65. assert.Nil(t, it)
  66. cancel()
  67. ctxTimed, cancel = context.WithTimeout(ctx, 10*time.Millisecond)
  68. cancel()
  69. it, err = q.PopItem(ctxTimed)
  70. assert.ErrorIs(t, err, context.Canceled)
  71. assert.Nil(t, it)
  72. // test blocking push if queue is full
  73. for i := 0; i < cfg.Length; i++ {
  74. err = q.PushItem(ctx, []byte(fmt.Sprintf("item-%d", i)))
  75. assert.NoError(t, err)
  76. }
  77. ctxTimed, cancel = context.WithTimeout(ctx, 10*time.Millisecond)
  78. err = q.PushItem(ctxTimed, []byte("item-full"))
  79. assert.ErrorIs(t, err, context.DeadlineExceeded)
  80. cancel()
  81. // test blocking push if queue is full (with custom pushBlockTime)
  82. oldPushBlockTime := pushBlockTime
  83. timeStart := time.Now()
  84. pushBlockTime = 30 * time.Millisecond
  85. err = q.PushItem(ctx, []byte("item-full"))
  86. assert.ErrorIs(t, err, context.DeadlineExceeded)
  87. assert.True(t, time.Since(timeStart) >= pushBlockTime*2/3)
  88. pushBlockTime = oldPushBlockTime
  89. // remove all
  90. cnt, err = q.Len(ctx)
  91. assert.NoError(t, err)
  92. assert.EqualValues(t, cfg.Length, cnt)
  93. _ = q.RemoveAll(ctx)
  94. cnt, err = q.Len(ctx)
  95. assert.NoError(t, err)
  96. assert.EqualValues(t, 0, cnt)
  97. })
  98. }
  99. func TestBaseDummy(t *testing.T) {
  100. q, err := newBaseDummy(&BaseConfig{}, true)
  101. assert.NoError(t, err)
  102. ctx := context.Background()
  103. assert.NoError(t, q.PushItem(ctx, []byte("foo")))
  104. cnt, err := q.Len(ctx)
  105. assert.NoError(t, err)
  106. assert.EqualValues(t, 0, cnt)
  107. has, err := q.HasItem(ctx, []byte("foo"))
  108. assert.NoError(t, err)
  109. assert.False(t, has)
  110. it, err := q.PopItem(ctx)
  111. assert.NoError(t, err)
  112. assert.Nil(t, it)
  113. assert.NoError(t, q.RemoveAll(ctx))
  114. }