Alishan Ahmed
Alishan Ahmed

Reputation: 21

Mock Redis Cluster Server in Golang

How to mock redis cluster server in Golang ?

Simple redis server can be mocked using 'github.com/alicebob/miniredis' package but it does not support cluster mocking.

Received the following error: ERR unknown command cluster, with args beginning with: slots``

My program uses 'github.com/go-redis/redis' package for redis implementation.

For example - create mock server for the following client

redisCache := redis.NewClusterClient(redisConfig)
    _, err := redisCache.Ping().Result()
    if err != nil {
        log.Fatalf("fatal error Not able to connect using redis client: %s", err)
    }

Upvotes: 1

Views: 2452

Answers (1)

haton
haton

Reputation: 31

Maybe using "github.com/alicebob/miniredis/v2" instead of "github.com/alicebob/miniredis" would solve the problem.

Here's an example.

package redis_cluster_test

import (
    "log"
    "testing"
    "time"

    "github.com/alicebob/miniredis/v2"
    "github.com/go-redis/redis"
)

func TestRedisCluster(t *testing.T) {
    mock, err := miniredis.Run()
    if err != nil {
        panic(err)
    }

    redisConfig := redis.ClusterOptions{
        Addrs:        []string{mock.Addr()},
        ReadTimeout:  1 * time.Second,
        WriteTimeout: 1 * time.Second,
        PoolSize:     6500,
        PoolTimeout:  30 * time.Second}

    redisCache := redis.NewClusterClient(&redisConfig)
    _, err = redisCache.Ping().Result()
    if err != nil {
        t.Errorf("fatal error Not able to connect using redis client: %s", err)
        log.Fatalf("fatal error Not able to connect using redis client: %s", err)
    }
}

Upvotes: 2

Related Questions