summaryrefslogtreecommitdiffstats
path: root/modules/queue/mock/inmemorymockredis.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/queue/mock/inmemorymockredis.go')
-rw-r--r--modules/queue/mock/inmemorymockredis.go133
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
+}