diff options
author | Daniel Baumann <daniel@debian.org> | 2024-10-18 20:33:49 +0200 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-12-12 23:57:56 +0100 |
commit | e68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch) | |
tree | 97775d6c13b0f416af55314eb6a89ef792474615 /modules/queue/base_redis_test.go | |
parent | Initial commit. (diff) | |
download | forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip |
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'modules/queue/base_redis_test.go')
-rw-r--r-- | modules/queue/base_redis_test.go | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/modules/queue/base_redis_test.go b/modules/queue/base_redis_test.go new file mode 100644 index 0000000..fa1700d --- /dev/null +++ b/modules/queue/base_redis_test.go @@ -0,0 +1,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) + }) + } +} |