android单体测试框架ro,Miniredis - 用于Go单元测试的纯Go Redis服务器

Miniredis

Pure Go Redis test server, used in Go unittests.

Sometimes you want to test code which uses Redis, without making it a full-blown integration test. Miniredis implements (parts of) the Redis server, to be used in unittests. It enables a simple, cheap, in-memory, Redis replacement, with a real TCP interface. Think of it as the Redis version of net/http/httptest.

It saves you from using mock code, and since the redis server lives in the test process you can query for values directly, without going through the server stack.

There are no dependencies on external binaries, so you can easily integrate it in automated build processes.

Commands

Implemented commands:

Connection (complete)

AUTH -- see RequireAuth()

ECHO

PING

SELECT

SWAPDB

QUIT

Key

DEL

EXISTS

EXPIRE

EXPIREAT

KEYS

MOVE

PERSIST

PEXPIRE

PEXPIREAT

PTTL

RENAME

RENAMENX

RANDOMKEY -- see m.Seed(...)

SCAN

TOUCH

TTL

TYPE

UNLINK

Transactions (complete)

DISCARD

EXEC

MULTI

UNWATCH

WATCH

Server

DBSIZE

FLUSHALL

FLUSHDB

TIME -- returns time.Now() or value set by SetTime()

String keys (complete)

APPEND

BITCOUNT

BITOP

BITPOS

DECR

DECRBY

GET

GETBIT

GETRANGE

GETSET

INCR

INCRBY

INCRBYFLOAT

MGET

MSET

MSETNX

PSETEX

SET

SETBIT

SETEX

SETNX

SETRANGE

STRLEN

Hash keys (complete)

HDEL

HEXISTS

HGET

HGETALL

HINCRBY

HINCRBYFLOAT

HKEYS

HLEN

HMGET

HMSET

HSET

HSETNX

HVALS

HSCAN

List keys (complete)

BLPOP

BRPOP

BRPOPLPUSH

LINDEX

LINSERT

LLEN

LPOP

LPUSH

LPUSHX

LRANGE

LREM

LSET

LTRIM

RPOP

RPOPLPUSH

RPUSH

RPUSHX

Pub/Sub (complete)

PSUBSCRIBE

PUBLISH

PUBSUB

PUNSUBSCRIBE

SUBSCRIBE

UNSUBSCRIBE

Set keys (complete)

SADD

SCARD

SDIFF

SDIFFSTORE

SINTER

SINTERSTORE

SISMEMBER

SMEMBERS

SMOVE

SPOP -- see m.Seed(...)

SRANDMEMBER -- see m.Seed(...)

SREM

SUNION

SUNIONSTORE

SSCAN

Sorted Set keys (complete)

ZADD

ZCARD

ZCOUNT

ZINCRBY

ZINTERSTORE

ZLEXCOUNT

ZPOPMIN

ZPOPMAX

ZRANGE

ZRANGEBYLEX

ZRANGEBYSCORE

ZRANK

ZREM

ZREMRANGEBYLEX

ZREMRANGEBYRANK

ZREMRANGEBYSCORE

ZREVRANGE

ZREVRANGEBYLEX

ZREVRANGEBYSCORE

ZREVRANK

ZSCORE

ZUNIONSTORE

ZSCAN

Stream keys

XACK

XADD

XDEL

XGROUP CREATE

XINFO STREAM -- partly

XLEN

XRANGE

XREADGROUP -- partly

XREVRANGE

Scripting

EVAL

EVALSHA

SCRIPT LOAD

SCRIPT EXISTS

SCRIPT FLUSH

GEO

GEOADD

GEODIST

GEOHASH

GEOPOS

GEORADIUS

GEORADIUS_RO

GEORADIUSBYMEMBER

GEORADIUSBYMEMBER_RO

Server

COMMAND -- partly

Cluster

CLUSTER SLOTS

CLUSTER KEYSLOT

CLUSTER NODES

TTLs, key expiration, and time

Since miniredis is intended to be used in unittests TTLs don't decrease automatically. You can use TTL() to get the TTL (as a time.Duration) of a key. It will return 0 when no TTL is set.

m.FastForward(d) can be used to decrement all TTLs. All TTLs which become <= 0 will be removed.

EXPIREAT and PEXPIREAT values will be converted to a duration. For that you can either set m.SetTime(t) to use that time as the base for the (P)EXPIREAT conversion, or don't call SetTime(), in which case time.Now() will be used.

SetTime() also sets the value returned by TIME, which defaults to time.Now(). It is not updated by FastForward, only by SetTime.

Randomness and Seed()

Miniredis will use math/rand's global RNG for randomness unless a seed is provided by calling m.Seed(...). If a seed is provided, then miniredis will use its own RNG based on that seed.

Commands which use randomness are: RANDOMKEY, SPOP, and SRANDMEMBER.

Example

import (

...

"github.com/alicebob/miniredis/v2"

...

)

func TestSomething(t *testing.T) {

s, err := miniredis.Run()

if err != nil {

panic(err)

}

defer s.Close()

// Optionally set some keys your code expects:

s.Set("foo", "bar")

s.HSet("some", "other", "key")

// Run your code and see if it behaves.

// An example using the redigo library from "github.com/gomodule/redigo/redis":

c, err := redis.Dial("tcp", s.Addr())

_, err = c.Do("SET", "foo", "bar")

// Optionally check values in redis...

if got, err := s.Get("foo"); err != nil || got != "bar" {

t.Error("'foo' has the wrong value")

}

// ... or use a helper for that:

s.CheckGet(t, "foo", "bar")

// TTL and expiration:

s.Set("foo", "bar")

s.SetTTL("foo", 10*time.Second)

s.FastForward(11 * time.Second)

if s.Exists("foo") {

t.Fatal("'foo' should not have existed anymore")

}

}

Not supported

Commands which will probably not be implemented:

CLUSTER (all)

CLUSTER *

READONLY

READWRITE

HyperLogLog (all) -- unless someone needs these

PFADD

PFCOUNT

PFMERGE

Key

DUMP

MIGRATE

OBJECT

RESTORE

WAIT

Scripting

SCRIPT DEBUG

SCRIPT KILL

Server

BGSAVE

BGWRITEAOF

CLIENT *

CONFIG *

DEBUG *

INFO

LASTSAVE

MONITOR

ROLE

SAVE

SHUTDOWN

SLAVEOF

SLOWLOG

SYNC

&c.

Tests are run against Redis 6.0.5. The ./integration subdir compares miniredis against a real redis instance.

If you want to test Redis Sentinel have a look at minisentinel.

A changelog is kept at CHANGELOG.md.

68747470733a2f2f7472617669732d63692e6f72672f616c696365626f622f6d696e6972656469732e7376673f6272616e63683d6d617374657268747470733a2f2f676f646f632e6f72672f6769746875622e636f6d2f616c696365626f622f6d696e6972656469733f7374617475732e737667

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值