mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-23 14:53:34 +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>
133 lines
2.4 KiB
Go
133 lines
2.4 KiB
Go
package mock
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
redis "github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// InMemoryMockRedis is a very primitive in-memory redis-like feature. The main
|
|
// purpose of this struct is to give some backend to mocked unit tests.
|
|
type InMemoryMockRedis struct {
|
|
queues map[string][][]byte
|
|
}
|
|
|
|
func NewInMemoryMockRedis() InMemoryMockRedis {
|
|
return InMemoryMockRedis{
|
|
queues: map[string][][]byte{},
|
|
}
|
|
}
|
|
|
|
func (r *InMemoryMockRedis) LLen(ctx context.Context, key string) *redis.IntCmd {
|
|
cmd := redis.NewIntCmd(ctx)
|
|
cmd.SetVal(int64(len(r.queues[key])))
|
|
return cmd
|
|
}
|
|
|
|
func (r *InMemoryMockRedis) SAdd(ctx context.Context, key string, content []byte) *redis.IntCmd {
|
|
cmd := redis.NewIntCmd(ctx)
|
|
|
|
for _, value := range r.queues[key] {
|
|
if string(value) == string(content) {
|
|
cmd.SetVal(0)
|
|
|
|
return cmd
|
|
}
|
|
}
|
|
|
|
r.queues[key] = append(r.queues[key], content)
|
|
|
|
cmd.SetVal(1)
|
|
|
|
return cmd
|
|
}
|
|
|
|
func (r *InMemoryMockRedis) RPush(ctx context.Context, key string, content []byte) *redis.IntCmd {
|
|
cmd := redis.NewIntCmd(ctx)
|
|
|
|
r.queues[key] = append(r.queues[key], content)
|
|
|
|
cmd.SetVal(1)
|
|
|
|
return cmd
|
|
}
|
|
|
|
func (r *InMemoryMockRedis) LPop(ctx context.Context, key string) *redis.StringCmd {
|
|
cmd := redis.NewStringCmd(ctx)
|
|
|
|
queue, found := r.queues[key]
|
|
if !found {
|
|
cmd.SetErr(errors.New("queue not found"))
|
|
|
|
return cmd
|
|
}
|
|
|
|
if len(queue) < 1 {
|
|
cmd.SetErr(errors.New("queue is empty"))
|
|
|
|
return cmd
|
|
}
|
|
|
|
value, rest := queue[0], queue[1:]
|
|
|
|
r.queues[key] = rest
|
|
|
|
cmd.SetVal(string(value))
|
|
|
|
return cmd
|
|
}
|
|
|
|
func (r *InMemoryMockRedis) SRem(ctx context.Context, key string, content []byte) *redis.IntCmd {
|
|
cmd := redis.NewIntCmd(ctx)
|
|
|
|
queue, found := r.queues[key]
|
|
if !found {
|
|
cmd.SetErr(errors.New("queue not found"))
|
|
|
|
return cmd
|
|
}
|
|
|
|
if len(queue) < 1 {
|
|
cmd.SetErr(errors.New("queue is empty"))
|
|
|
|
return cmd
|
|
}
|
|
|
|
newList := [][]byte{}
|
|
|
|
for _, value := range queue {
|
|
if string(value) != string(content) {
|
|
newList = append(newList, value)
|
|
}
|
|
}
|
|
|
|
r.queues[key] = newList
|
|
|
|
cmd.SetVal(1)
|
|
|
|
return cmd
|
|
}
|
|
|
|
func (r *InMemoryMockRedis) SIsMember(ctx context.Context, key string, content []byte) *redis.BoolCmd {
|
|
cmd := redis.NewBoolCmd(ctx)
|
|
|
|
queue, found := r.queues[key]
|
|
if !found {
|
|
cmd.SetErr(errors.New("queue not found"))
|
|
|
|
return cmd
|
|
}
|
|
|
|
for _, value := range queue {
|
|
if string(value) == string(content) {
|
|
cmd.SetVal(true)
|
|
|
|
return cmd
|
|
}
|
|
}
|
|
|
|
cmd.SetVal(false)
|
|
|
|
return cmd
|
|
}
|