summaryrefslogtreecommitdiffstats
path: root/modules/queue/base_redis_test.go
blob: fa1700dc2e5a481a7e2f575f10f0dc7d01db1cf0 (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
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package queue

import (
	"context"
	"testing"

	"code.gitea.io/gitea/modules/queue/mock"
	"code.gitea.io/gitea/modules/setting"

	"github.com/redis/go-redis/v9"
	"github.com/stretchr/testify/suite"
	"go.uber.org/mock/gomock"
)

type baseRedisUnitTestSuite struct {
	suite.Suite

	mockController *gomock.Controller
}

func TestBaseRedis(t *testing.T) {
	suite.Run(t, &baseRedisUnitTestSuite{})
}

func (suite *baseRedisUnitTestSuite) SetupSuite() {
	suite.mockController = gomock.NewController(suite.T())
}

func (suite *baseRedisUnitTestSuite) TestBasic() {
	queueName := "test-queue"
	testCases := []struct {
		Name             string
		ConnectionString string
		QueueName        string
		Unique           bool
	}{
		{
			Name:             "unique",
			ConnectionString: "redis://127.0.0.1/0",
			QueueName:        queueName,
			Unique:           true,
		},
		{
			Name:             "non-unique",
			ConnectionString: "redis://127.0.0.1/0",
			QueueName:        queueName,
			Unique:           false,
		},
		{
			Name:             "unique with prefix",
			ConnectionString: "redis://127.0.0.1/0?prefix=forgejo:queue:",
			QueueName:        "forgejo:queue:" + queueName,
			Unique:           true,
		},
		{
			Name:             "non-unique with prefix",
			ConnectionString: "redis://127.0.0.1/0?prefix=forgejo:queue:",
			QueueName:        "forgejo:queue:" + queueName,
			Unique:           false,
		},
	}

	for _, testCase := range testCases {
		suite.Run(testCase.Name, func() {
			queueSettings := setting.QueueSettings{
				Length:  10,
				ConnStr: testCase.ConnectionString,
			}

			// Configure expectations.
			mockRedisStore := mock.NewInMemoryMockRedis()
			redisClient := mock.NewMockRedisClient(suite.mockController)

			redisClient.EXPECT().
				Ping(gomock.Any()).
				Times(1).
				Return(&redis.StatusCmd{})
			redisClient.EXPECT().
				LLen(gomock.Any(), testCase.QueueName).
				Times(1).
				DoAndReturn(mockRedisStore.LLen)
			redisClient.EXPECT().
				LPop(gomock.Any(), testCase.QueueName).
				Times(1).
				DoAndReturn(mockRedisStore.LPop)
			redisClient.EXPECT().
				RPush(gomock.Any(), testCase.QueueName, gomock.Any()).
				Times(1).
				DoAndReturn(mockRedisStore.RPush)

			if testCase.Unique {
				redisClient.EXPECT().
					SAdd(gomock.Any(), testCase.QueueName+"_unique", gomock.Any()).
					Times(1).
					DoAndReturn(mockRedisStore.SAdd)
				redisClient.EXPECT().
					SRem(gomock.Any(), testCase.QueueName+"_unique", gomock.Any()).
					Times(1).
					DoAndReturn(mockRedisStore.SRem)
				redisClient.EXPECT().
					SIsMember(gomock.Any(), testCase.QueueName+"_unique", gomock.Any()).
					Times(2).
					DoAndReturn(mockRedisStore.SIsMember)
			}

			client, err := newBaseRedisGeneric(
				toBaseConfig(queueName, queueSettings),
				testCase.Unique,
				redisClient,
			)
			suite.Require().NoError(err)

			ctx := context.Background()
			expectedContent := []byte("test")

			suite.Require().NoError(client.PushItem(ctx, expectedContent))

			found, err := client.HasItem(ctx, expectedContent)
			suite.Require().NoError(err)
			if testCase.Unique {
				suite.True(found)
			} else {
				suite.False(found)
			}

			found, err = client.HasItem(ctx, []byte("not found content"))
			suite.Require().NoError(err)
			suite.False(found)

			content, err := client.PopItem(ctx)
			suite.Require().NoError(err)
			suite.Equal(expectedContent, content)
		})
	}
}