代码拉取完成,页面将自动刷新
package redis
import (
"github.com/go-redis/redis"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
// redis server
var r = Default()
// redis client
var c = redis.NewClient(&redis.Options{
Addr: ":6379",
})
func init() {
go r.Run(":6379")
}
func TestPingCommand(t *testing.T) {
s, err := c.Ping().Result()
assert.Equal(t, "PONG", s)
assert.NoError(t, err)
pingCmd := redis.NewStringCmd("ping", "Hello,", "redis server!")
c.Process(pingCmd)
s, err = pingCmd.Result()
assert.Equal(t, "Hello, redis server!", s)
assert.NoError(t, err)
}
func TestSetCommand(t *testing.T) {
s, err := c.Set("k", "v", 0).Result()
assert.Equal(t, "OK", s)
assert.NoError(t, err)
s, err = c.Set("k2", nil, 0).Result()
assert.Equal(t, "OK", s)
assert.NoError(t, err)
s, err = c.Set("k3", "v", 1*time.Hour).Result()
assert.Equal(t, "OK", s)
assert.NoError(t, err)
}
func TestGetCommand(t *testing.T) {
s, err := c.Get("k").Result()
assert.Equal(t, "v", s)
assert.NoError(t, err)
}
func TestDelCommand(t *testing.T) {
i, err := c.Del("k", "k3").Result()
assert.Equal(t, i, int64(2))
assert.NoError(t, err)
i, err = c.Del("abc").Result()
assert.Zero(t, i)
assert.NoError(t, err)
}
func TestTtlCommand(t *testing.T) {
s, err := c.Set("aKey", "hey", 1*time.Minute).Result()
assert.Equal(t, "OK", s)
assert.NoError(t, err)
s, err = c.Set("bKey", "hallo", 0).Result()
assert.Equal(t, "OK", s)
assert.NoError(t, err)
ttl, err := c.TTL("aKey").Result()
assert.True(t, ttl.Seconds() > 55 && ttl.Seconds() < 61, "ttl: %d", ttl)
assert.NoError(t, err)
ttl, err = c.TTL("none").Result()
assert.Equal(t, time.Duration(-2000000000), ttl)
assert.NoError(t, err)
ttl, err = c.TTL("bKey").Result()
assert.NoError(t, err)
assert.Equal(t, time.Duration(-1000000000), ttl)
}
func TestExpiry(t *testing.T) {
s, err := c.Set("x", "val", 10*time.Millisecond).Result()
assert.NoError(t, err)
assert.Equal(t, "OK", s)
time.Sleep(10 * time.Millisecond)
s, err = c.Get("x").Result()
assert.Equal(t, "", s)
assert.Error(t, err)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。