summaryrefslogtreecommitdiffstats
path: root/modules/queue/base_test.go
blob: a5600fea638fc31e89f4813e3fe77b8b776e2936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package queue

import (
	"context"
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func testQueueBasic(t *testing.T, newFn func(cfg *BaseConfig) (baseQueue, error), cfg *BaseConfig, isUnique bool) {
	t.Run(fmt.Sprintf("testQueueBasic-%s-unique:%v", cfg.ManagedName, isUnique), func(t *testing.T) {
		q, err := newFn(cfg)
		require.NoError(t, err)

		ctx := context.Background()
		_ = q.RemoveAll(ctx)
		cnt, err := q.Len(ctx)
		require.NoError(t, err)
		assert.EqualValues(t, 0, cnt)

		// push the first item
		err = q.PushItem(ctx, []byte("foo"))
		require.NoError(t, err)

		cnt, err = q.Len(ctx)
		require.NoError(t, err)
		assert.EqualValues(t, 1, cnt)

		// push a duplicate item
		err = q.PushItem(ctx, []byte("foo"))
		if !isUnique {
			require.NoError(t, err)
		} else {
			require.ErrorIs(t, err, ErrAlreadyInQueue)
		}

		// check the duplicate item
		cnt, err = q.Len(ctx)
		require.NoError(t, err)
		has, err := q.HasItem(ctx, []byte("foo"))
		require.NoError(t, err)
		if !isUnique {
			assert.EqualValues(t, 2, cnt)
			assert.False(t, has) // non-unique queues don't check for duplicates
		} else {
			assert.EqualValues(t, 1, cnt)
			assert.True(t, has)
		}

		// push another item
		err = q.PushItem(ctx, []byte("bar"))
		require.NoError(t, err)

		// pop the first item (and the duplicate if non-unique)
		it, err := q.PopItem(ctx)
		require.NoError(t, err)
		assert.EqualValues(t, "foo", string(it))

		if !isUnique {
			it, err = q.PopItem(ctx)
			require.NoError(t, err)
			assert.EqualValues(t, "foo", string(it))
		}

		// pop another item
		it, err = q.PopItem(ctx)
		require.NoError(t, err)
		assert.EqualValues(t, "bar", string(it))

		// pop an empty queue (timeout, cancel)
		ctxTimed, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
		it, err = q.PopItem(ctxTimed)
		require.ErrorIs(t, err, context.DeadlineExceeded)
		assert.Nil(t, it)
		cancel()

		ctxTimed, cancel = context.WithTimeout(ctx, 10*time.Millisecond)
		cancel()
		it, err = q.PopItem(ctxTimed)
		require.ErrorIs(t, err, context.Canceled)
		assert.Nil(t, it)

		// test blocking push if queue is full
		for i := 0; i < cfg.Length; i++ {
			err = q.PushItem(ctx, []byte(fmt.Sprintf("item-%d", i)))
			require.NoError(t, err)
		}
		ctxTimed, cancel = context.WithTimeout(ctx, 10*time.Millisecond)
		err = q.PushItem(ctxTimed, []byte("item-full"))
		require.ErrorIs(t, err, context.DeadlineExceeded)
		cancel()

		// test blocking push if queue is full (with custom pushBlockTime)
		oldPushBlockTime := pushBlockTime
		timeStart := time.Now()
		pushBlockTime = 30 * time.Millisecond
		err = q.PushItem(ctx, []byte("item-full"))
		require.ErrorIs(t, err, context.DeadlineExceeded)
		assert.GreaterOrEqual(t, time.Since(timeStart), pushBlockTime*2/3)
		pushBlockTime = oldPushBlockTime

		// remove all
		cnt, err = q.Len(ctx)
		require.NoError(t, err)
		assert.EqualValues(t, cfg.Length, cnt)

		_ = q.RemoveAll(ctx)

		cnt, err = q.Len(ctx)
		require.NoError(t, err)
		assert.EqualValues(t, 0, cnt)
	})
}

func TestBaseDummy(t *testing.T) {
	q, err := newBaseDummy(&BaseConfig{}, true)
	require.NoError(t, err)

	ctx := context.Background()
	require.NoError(t, q.PushItem(ctx, []byte("foo")))

	cnt, err := q.Len(ctx)
	require.NoError(t, err)
	assert.EqualValues(t, 0, cnt)

	has, err := q.HasItem(ctx, []byte("foo"))
	require.NoError(t, err)
	assert.False(t, has)

	it, err := q.PopItem(ctx)
	require.NoError(t, err)
	assert.Nil(t, it)

	require.NoError(t, q.RemoveAll(ctx))
}