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/mock/inmemorymockredis.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 '')
-rw-r--r-- | modules/queue/mock/inmemorymockredis.go | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/modules/queue/mock/inmemorymockredis.go b/modules/queue/mock/inmemorymockredis.go new file mode 100644 index 0000000..de8bd8a --- /dev/null +++ b/modules/queue/mock/inmemorymockredis.go @@ -0,0 +1,133 @@ +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 +} |