mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-11-25 03:06:10 +01:00
54acfa8880
Summary: - Move existing test under a `testify` Suite as `baseRedisWithServerTestSuite` - Those tests require real redis server. - Add `go.uber.org/mock/mockgen@latest` as dependency - as a tool (Makefile). - in the `go.mod` file. - Mock redis client lives under a `mock` directory under the queue module. - That mock module has an extra hand-written mock in-memory redis-like struct. - Add tests using the mock redis client. - Changed the logic around queue provider creation. - Now the `getNewQueue` returns a Queue provider directly, not an init function to create it. The whole Queue module is close to impossible to test properly because everything is private, everything goes through a struct route. Because of that, we can't test for example what keys are used for given queue. To overcome this, as a first step I removed one step from that hard route by allowing custom calls to create new queue provider. To achieve this, I moved the creation logic into the `getNewQueue` (previously it was `getNewQueueFn`). That changes nothing on that side, everything goes as before, except the `newXXX` call happens directly in that function and not outside that. That made it possible to add extra provider specific parameters to those function (`newXXX`). For example a client on redis. Calling it through the `getNewQueue` function, it gets `nil`. - If the provided client is not `nil`, it will use that instead of the connection string. - If it's `nil` (default behaviour), it creates a new redis client as it did before, no changes to that. The rest of the provider code is unchanged. All these changes were required to make it possible to generate mock clients for providers and use them. For the tests, the existing two test cases are good with redis server, and they need some extra helpers, for example to start a new redis server if required, or waiting on a redis server to be ready to use. These helpers are only required for test cases using real redis server. For better isolation, moved existing test under a testify Suite, and moved them into a new test file called `base_redis_with_server_test.go` because, well they test the code with server. These tests do exactly the same as before, calling the same sub-tests the same way as before, the only change is the structure of the test (remove repetition, scope server related helper functions). Finally, we can create unit tests without redis server. The main focus of this group of tests are higher level overview of operations. With the mock redis client we can set up expectations about used queue names, received values, return value to simulate faulty state. These new unit test functions don't test all functionality, at least it's not aimed for it now. It's more about the possibility of doing that and add extra tests around parts we couldn't test before, for example key. What extra features can test the new unit test group: - What is the received key for given queue? For example using `prefix`, or if all the `SXxx` calls are expected to use `queue_unique` if it's a unique queue. - If it's not a unique queue, no `SXxx` functions are called, because those sets are used only to check if a value is unique or not. - `HasItem` return `false` always if it's a non-unique queue. - All functions are called exactly `N` times, and we don't have any unexpected calls to redis from the code. Signed-off-by: Victoria Nadasdi <victoria@efertone.me>
139 lines
3.4 KiB
Go
139 lines
3.4 KiB
Go
// 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.NewMockUniversalClient(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)
|
|
})
|
|
}
|
|
}
|