使用miniredis模拟redis服务器失败

xfyts7mz  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(585)

哥朗新手:)
我有一个redis客户端 Package 器在go 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()
}

我做错什么了?

qyyhg6bp

qyyhg6bp1#

很抱歉,这么久才回复。我已经在本地运行了你的代码,没有出现错误。
普通文件

package redis

import (
    "errors"
    "fmt"
    "os"
    "strconv"

    "github.com/go-redis/redis"
    "go.uber.org/zap"
)

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()
    if err == redis.Nil {
        return "", errors.New("Key Not Found")
    } else if err != nil {
        return "", err
    } else {
        return res, nil
    }

}

测试文件

package redis

import (
    "fmt"
    "os"
    "testing"

    "github.com/alicebob/miniredis/v2"
)

var mr *miniredis.Miniredis

func TestGet(t *testing.T) {
    var err error

    mr, err = miniredis.Run()
    fmt.Printf("Run err: %v\n", err)
    fmt.Printf("mr.Addr(): %v\n", mr.Addr())

    err = mr.Set("test1", "some")
    fmt.Printf("Set err: %v\n", err)

    err = os.Setenv("REDIS_URL", mr.Addr())
    fmt.Printf("Setenv err: %v\n", err)

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

    fmt.Printf("Get err: %v\n", err)
    fmt.Printf("r: %v\n", r)

    mr.Close()
}

结果

go test
Run err: <nil>
mr.Addr(): 127.0.0.1:54966
Set err: <nil>
Setenv err: <nil>
Redis client is ready
Get err: <nil>
r: some
PASS
ok      test/sof        0.536s

相关问题