Skip to content
Pure Go Redis server for Go unittests
Go Other
  1. Go 99.9%
  2. Other 0.1%
Branch: master
Clone or download
Latest commit bf9179b Jan 20, 2020
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
geohash improve geohash decoding Nov 13, 2019
integration Merge pull request #131 from kyeett/add-georadiusbymember Jan 20, 2020
server Lua .call() not via a socket (#126) Jan 4, 2020
.gitignore finish GEORADIUS, GEORADIUS_RO, GEOPOS, and GEOADD Sep 18, 2019
.travis.yml upgrade Go version to 1.12 Mar 23, 2019
CHANGELOG.md update CHANGELOG Dec 18, 2019
LICENSE Initial commit Sep 3, 2014
Makefile upgrade go version Aug 29, 2018
README.md GEODIST implemementation Nov 13, 2019
check.go SDIFF SDIFFSTORE Oct 6, 2014
cmd_connection.go Lua .call() not via a socket (#126) Jan 4, 2020
cmd_connection_test.go SWAPDB: more error checks and support transactions Apr 17, 2019
cmd_generic.go use time set by SetTime() for stream IDs, if any is set Nov 6, 2019
cmd_generic_test.go finish GEORADIUS, GEORADIUS_RO, GEOPOS, and GEOADD Sep 18, 2019
cmd_geo.go add command GEORADIUSBYMEMBER Jan 19, 2020
cmd_geo_test.go add command GEORADIUSBYMEMBER Jan 19, 2020
cmd_hash.go move HSET length check inside the transaction Dec 11, 2019
cmd_hash_test.go initial support for multiple files in hset Dec 1, 2019
cmd_list.go go modules: fix for v2 Apr 29, 2019
cmd_list_test.go shorter test sleep Dec 11, 2019
cmd_pubsub.go Lua .call() not via a socket (#126) Jan 4, 2020
cmd_pubsub_test.go work around disappearing connection Dec 28, 2019
cmd_scripting.go Lua .call() not via a socket (#126) Jan 4, 2020
cmd_scripting_test.go keep Lua floats as strings internally Dec 18, 2019
cmd_server.go use time set by SetTime() for stream IDs, if any is set Nov 6, 2019
cmd_server_test.go Support TIME command Nov 29, 2018
cmd_set.go finish GEORADIUS, GEORADIUS_RO, GEOPOS, and GEOADD Sep 18, 2019
cmd_set_test.go test SRANDMEMBER using Seed() Jul 24, 2019
cmd_sorted_set.go finish GEORADIUS, GEORADIUS_RO, GEOPOS, and GEOADD Sep 18, 2019
cmd_sorted_set_test.go finish GEORADIUS, GEORADIUS_RO, GEOPOS, and GEOADD Sep 18, 2019
cmd_stream.go implement XADD MAXLEN Nov 6, 2019
cmd_stream_test.go no fancy 1.13-isms Nov 6, 2019
cmd_string.go use 128 bit floats internally Nov 13, 2019
cmd_string_test.go Use the new redigo import path May 9, 2018
cmd_transactions.go Lua .call() not via a socket (#126) Jan 4, 2020
cmd_transactions_test.go Report Null on aborted transaction Jul 25, 2019
db.go initial support for multiple files in hset Dec 1, 2019
direct.go initial support for multiple files in hset Dec 1, 2019
example_test.go go modules: fix for v2 Apr 29, 2019
geo.go improve geohash decoding Nov 13, 2019
geo_test.go improve geohash decoding Nov 13, 2019
go.mod update gopher-lua dependency Dec 29, 2019
go.sum update gopher-lua dependency Dec 29, 2019
keys.go finish GEORADIUS, GEORADIUS_RO, GEOPOS, and GEOADD Sep 18, 2019
keys_test.go finish GEORADIUS, GEORADIUS_RO, GEOPOS, and GEOADD Sep 18, 2019
lua.go Lua .call() not via a socket (#126) Jan 4, 2020
miniredis.go Lua .call() not via a socket (#126) Jan 4, 2020
miniredis_test.go Lua .call() not via a socket (#126) Jan 4, 2020
pubsub.go use the existing pattern compiler Jul 20, 2019
redis.go Lua .call() not via a socket (#126) Jan 4, 2020
sorted_set.go finish GEORADIUS, GEORADIUS_RO, GEOPOS, and GEOADD Sep 18, 2019
sorted_set_test.go fix minor score bugs in tests Jun 2, 2015
stream.go no fancy 1.13-isms Nov 6, 2019
stream_test.go use a simpler XAdd() data structure Nov 6, 2019
test_test.go update some comments Mar 29, 2019

README.md

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
    • 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
    • XADD
    • XLEN
    • XRANGE
    • XREVRANGE
  • Scripting
    • EVAL
    • EVALSHA
    • SCRIPT LOAD
    • SCRIPT EXISTS
    • SCRIPT FLUSH
  • GEO
    • GEOADD
    • GEODIST
    • GEOHASH
    • GEOPOS
    • GEORADIUS
    • GEORADIUS_RO
    • GEORADIUSBYMEMBER
    • GEORADIUSBYMEMBER_RO

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 *
    • COMMAND *
    • CONFIG *
    • DEBUG *
    • INFO
    • LASTSAVE
    • MONITOR
    • ROLE
    • SAVE
    • SHUTDOWN
    • SLAVEOF
    • SLOWLOG
    • SYNC

&c.

Tests are run against Redis 5.0.3. 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.

Build Status GoDoc

You can’t perform that action at this time.