Pure Go Redis server for Go unittests
Clone or download
alicebob simpler QUIT test
Somehow Jenkins can't make this one pass.
Latest commit 3657542 Sep 11, 2018
Permalink
Failed to load latest commit information.
integration simpler QUIT test Sep 11, 2018
server fix import path May 9, 2018
.travis.yml upgrade go version Aug 29, 2018
LICENSE Initial commit Sep 3, 2014
Makefile upgrade go version Aug 29, 2018
README.md improve the expiration mechanism Aug 30, 2018
check.go SDIFF SDIFFSTORE Oct 6, 2014
cmd_connection.go Prevent MITM attacks due to HTTP Sep 3, 2018
cmd_connection_test.go connection made in lua needs auth May 22, 2018
cmd_generic.go Prevent MITM attacks due to HTTP Sep 3, 2018
cmd_generic_test.go Use the new redigo import path May 9, 2018
cmd_hash.go Prevent MITM attacks due to HTTP Sep 3, 2018
cmd_hash_test.go Use the new redigo import path May 9, 2018
cmd_list.go Prevent MITM attacks due to HTTP Sep 3, 2018
cmd_list_test.go Use the new redigo import path May 9, 2018
cmd_scripting.go use gopher-json fork Jan 25, 2018
cmd_scripting_test.go ignore `redis.replicate_commands()` call Aug 29, 2018
cmd_server.go Prevent MITM attacks due to HTTP Sep 3, 2018
cmd_server_test.go Use the new redigo import path May 9, 2018
cmd_set.go Prevent MITM attacks due to HTTP Sep 3, 2018
cmd_set_test.go Use the new redigo import path May 9, 2018
cmd_sorted_set.go Prevent MITM attacks due to HTTP Sep 3, 2018
cmd_sorted_set_test.go fix direct.ZAdd() type check Jun 7, 2018
cmd_string.go Prevent MITM attacks due to HTTP Sep 3, 2018
cmd_string_test.go Use the new redigo import path May 9, 2018
cmd_transactions.go Prevent MITM attacks due to HTTP Sep 3, 2018
cmd_transactions_test.go Use the new redigo import path May 9, 2018
db.go deal with negative TTLs Apr 15, 2017
direct.go fix direct.ZAdd() type check Jun 7, 2018
example_test.go Use the new redigo import path May 9, 2018
keys.go use a local redis server Apr 30, 2017
keys_test.go move to function based tests Apr 19, 2018
lua.go ignore `redis.replicate_commands()` call Aug 29, 2018
miniredis.go connection made in lua needs auth May 22, 2018
miniredis_test.go Use the new redigo import path May 9, 2018
redis.go add support for `ZADD INCR` Feb 1, 2018
sorted_set.go use a local redis server Apr 30, 2017
sorted_set_test.go fix minor score bugs in tests Jun 2, 2015
test_test.go stop test on failure Jun 7, 2018

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.

Changelog

2.4.4

Fixed nil Lua return value (#43)

2.4.3

Fixed using Lua with authenticated redis.

2.4.2

Changed redigo import path.

2.4

Minor cleanups. Miniredis now requires Go >= 1.9 (only for the tests. If you don't run the tests you can use an older Go version).

2.3.1

Lua changes: added cjson library, and redis.sha1hex().

2.3

Added the EVAL, EVALSHA, and SCRIPT commands. Uses a pure Go Lua interpreter. Please open an issue if there are problems with any Lua code.

2.2

Introduced StartAddr().

2.1

Internal cleanups. No changes in functionality.

2.0

2.0.0 improves TTLs to be time.Duration values. .Expire() is removed and replaced by .TTL(), which returns the TTL as a time.Duration. This should be the change needed to upgrade:

1.0:

m.Expire() == 4

2.0:

m.TTL() == 4 * time.Second

Furthermore, .SetTime() is added to help with EXPIREAT commands, and .FastForward() is introduced to test keys expiration.

Commands

Implemented commands:

  • Connection (complete)
    • AUTH -- see RequireAuth()
    • ECHO
    • PING
    • SELECT
    • QUIT
  • Key
    • DEL
    • EXISTS
    • EXPIRE
    • EXPIREAT
    • KEYS
    • MOVE
    • PERSIST
    • PEXPIRE
    • PEXPIREAT
    • PTTL
    • RENAME
    • RENAMENX
    • RANDOMKEY -- call math.rand.Seed(...) once before using.
    • TTL
    • TYPE
    • SCAN
  • Transactions (complete)
    • DISCARD
    • EXEC
    • MULTI
    • UNWATCH
    • WATCH
  • Server
    • DBSIZE
    • FLUSHALL
    • FLUSHDB
  • 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
  • Set keys (complete)
    • SADD
    • SCARD
    • SDIFF
    • SDIFFSTORE
    • SINTER
    • SINTERSTORE
    • SISMEMBER
    • SMEMBERS
    • SMOVE
    • SPOP -- call math.rand.Seed(...) once before using.
    • SRANDMEMBER -- call math.rand.Seed(...) once before using.
    • SREM
    • SUNION
    • SUNIONSTORE
    • SSCAN
  • Sorted Set keys (complete)
    • ZADD
    • ZCARD
    • ZCOUNT
    • ZINCRBY
    • ZINTERSTORE
    • ZLEXCOUNT
    • ZRANGE
    • ZRANGEBYLEX
    • ZRANGEBYSCORE
    • ZRANK
    • ZREM
    • ZREMRANGEBYLEX
    • ZREMRANGEBYRANK
    • ZREMRANGEBYSCORE
    • ZREVRANGE
    • ZREVRANGEBYSCORE
    • ZREVRANK
    • ZSCORE
    • ZUNIONSTORE
    • ZSCAN
  • Scripting
    • EVAL
    • EVALSHA
    • SCRIPT LOAD
    • SCRIPT EXISTS
    • SCRIPT FLUSH

TTLs and key expiration

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.

Example

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
  • GEO (all) -- unless someone needs these
    • GEOADD
    • GEODIST
    • GEOHASH
    • GEOPOS
    • GEORADIUS
    • GEORADIUSBYMEMBER
  • HyperLogLog (all) -- unless someone needs these
    • PFADD
    • PFCOUNT
    • PFMERGE
  • Key
    • DUMP
    • MIGRATE
    • OBJECT
    • RESTORE
    • WAIT
  • Pub/Sub (all)
    • PSUBSCRIBE
    • PUBLISH
    • PUBSUB
    • PUNSUBSCRIBE
    • SUBSCRIBE
    • UNSUBSCRIBE
  • Scripting
    • SCRIPT DEBUG
    • SCRIPT KILL
  • Server
    • BGSAVE
    • BGWRITEAOF
    • CLIENT *
    • COMMAND *
    • CONFIG *
    • DEBUG *
    • INFO
    • LASTSAVE
    • MONITOR
    • ROLE
    • SAVE
    • SHUTDOWN
    • SLAVEOF
    • SLOWLOG
    • SYNC
    • TIME

&c.

Tests are run against Redis 4.0.6 (Debian). The ./integration subdir compares miniredis against a real redis instance.

Build Status GoDoc