使用 miniredis 模拟 redis 服务器失败

Golang 新手在这里 :)

我在 go-redis 上有一个 redis 客户端包装器,我想测试它是否正常工作。我曾尝试使用 miniredis 来模拟我将要连接的 redis 服务器,但我不断收到错误消息。


当我将所有东西都放在同一个函数中时,它就可以工作了——我可以针对 miniredis 服务运行 go-redis。但是,当我尝试使用我的客户端时,它失败了。

我试过查看 go-redis 和 miniredis 的文档,但找不到任何答案。我也尝试过将 miniredis 服务器定义为全局变量,但这也无济于事。


客户端实现:


package redis


import (

    "errors"

    "fmt"

    "github.com/go-redis/redis"

    "go.uber.org/zap"

    "os"

    "strconv"

    "time"

)


var redisClient *redis.Client

var redisCert string


var redisURL = "localhost:6379"

var redisPass = ""

var redisDB = 0


type Client struct {

    redisClient *redis.Client

}

func newRedisClient() *Client {

    if os.Getenv("REDIS_URL") != "" {

        redisURL = os.Getenv("REDIS_URL")

    }

    if os.Getenv("REDIS_PASS") != "" {

        redisPass = os.Getenv("REDIS_PASS")

    }

    if os.Getenv("REDIS_DB") != "" {

        redisDB, _ = strconv.Atoi(os.Getenv("REDIS_DB"))

    }


    client := redis.NewClient(&redis.Options{

        Addr:      redisURL,

        Password:  redisPass,

        DB:        redisDB,

    })

    redisClient = client

    _, err := redisClient.Ping().Result()

    if err != nil {

        fmt.Println("Failed to connect", zap.Error(err))

        return nil

    }

    fmt.Println("Redis client is ready")

    return &Client{redisClient: client}

}


func (r *Client) Get(key string) (string, error) {

    res, err := redisClient.Get(key).Result()

    fmt.Println(res, err)

    if err == redis.Nil {

        return "", errors.New("Key Not Found")

    } else if err != nil {

        return "", err

    } else {

        return res, nil

    }


}

测试实现:


package redis


import (

    "github.com/alicebob/miniredis/v2"

    "gopkg.in/go-playground/assert.v1"

    "os"

    "testing"

)


var mr *miniredis.Miniredis


func TestGet(t *testing.T) {

    mr, _ = miniredis.Run()

    mr.Set("test1", "some")

    os.Setenv("REDIS_URL", mr.Addr())

    c := newRedisClient()

    r, err := c.Get("test1")

    assert.Equal(t, r, "")

    assert.Equal(t, err, "")

    mr.Close()

}

我究竟做错了什么?


蓝山帝景
浏览 268回答 2
2回答

FFIVE

通过 miniredis 模拟失败,您可以使用SetError:https ://github.com/alicebob/miniredis/issues/36#issuecomment-642009898func TestGet(t *testing.T) {    mr, _ = miniredis.Run()    // normal test    // ...    // error test    mr.SetError("mock error")    _, err = c.Get("test2")    assert.EqualError(t, err, "mock error")    mr.Close()}

湖上湖

我已在本地运行您的代码,但没有收到错误消息。普通文件package redisimport (&nbsp; &nbsp; "errors"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "github.com/go-redis/redis"&nbsp; &nbsp; "go.uber.org/zap")var redisClient *redis.Clientvar redisCert stringvar redisURL = "localhost:6379"var redisPass = ""var redisDB = 0type Client struct {&nbsp; &nbsp; redisClient *redis.Client}func newRedisClient() *Client {&nbsp; &nbsp; if os.Getenv("REDIS_URL") != "" {&nbsp; &nbsp; &nbsp; &nbsp; redisURL = os.Getenv("REDIS_URL")&nbsp; &nbsp; }&nbsp; &nbsp; if os.Getenv("REDIS_PASS") != "" {&nbsp; &nbsp; &nbsp; &nbsp; redisPass = os.Getenv("REDIS_PASS")&nbsp; &nbsp; }&nbsp; &nbsp; if os.Getenv("REDIS_DB") != "" {&nbsp; &nbsp; &nbsp; &nbsp; redisDB, _ = strconv.Atoi(os.Getenv("REDIS_DB"))&nbsp; &nbsp; }&nbsp; &nbsp; client := redis.NewClient(&redis.Options{&nbsp; &nbsp; &nbsp; &nbsp; Addr:&nbsp; &nbsp; &nbsp;redisURL,&nbsp; &nbsp; &nbsp; &nbsp; Password: redisPass,&nbsp; &nbsp; &nbsp; &nbsp; DB:&nbsp; &nbsp; &nbsp; &nbsp;redisDB,&nbsp; &nbsp; })&nbsp; &nbsp; redisClient = client&nbsp; &nbsp; _, err := redisClient.Ping().Result()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Failed to connect", zap.Error(err))&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println("Redis client is ready")&nbsp; &nbsp; return &Client{redisClient: client}}func (r *Client) Get(key string) (string, error) {&nbsp; &nbsp; res, err := redisClient.Get(key).Result()&nbsp; &nbsp; if err == redis.Nil {&nbsp; &nbsp; &nbsp; &nbsp; return "", errors.New("Key Not Found")&nbsp; &nbsp; } else if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return "", err&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return res, nil&nbsp; &nbsp; }}测试文件package redisimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "testing"&nbsp; &nbsp; "github.com/alicebob/miniredis/v2")var mr *miniredis.Miniredisfunc TestGet(t *testing.T) {&nbsp; &nbsp; var err error&nbsp; &nbsp; mr, err = miniredis.Run()&nbsp; &nbsp; fmt.Printf("Run err: %v\n", err)&nbsp; &nbsp; fmt.Printf("mr.Addr(): %v\n", mr.Addr())&nbsp; &nbsp; err = mr.Set("test1", "some")&nbsp; &nbsp; fmt.Printf("Set err: %v\n", err)&nbsp; &nbsp; err = os.Setenv("REDIS_URL", mr.Addr())&nbsp; &nbsp; fmt.Printf("Setenv err: %v\n", err)&nbsp; &nbsp; c := newRedisClient()&nbsp; &nbsp; r, err := c.Get("test1")&nbsp; &nbsp; fmt.Printf("Get err: %v\n", err)&nbsp; &nbsp; fmt.Printf("r: %v\n", r)&nbsp; &nbsp; mr.Close()}结果go testRun err: <nil>mr.Addr(): 127.0.0.1:54966Set err: <nil>Setenv err: <nil>Redis client is readyGet err: <nil>r: somePASSok&nbsp; &nbsp; &nbsp; test/sof&nbsp; &nbsp; &nbsp; &nbsp; 0.536s
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go