Pure Go Redis server for Go unittests
Go to file
2024-05-29 15:39:52 +02:00
.github/workflows upgrade to actions/setup-go v5 2024-04-09 20:35:05 +02:00
fpconv fuzz is in 1.18, not 1.16 2024-03-27 19:54:56 +01:00
geohash
hyperloglog
integration Add integration test for SRANDMEMBER on nonexistent key 2024-05-27 22:42:08 +09:00
metro
proto
server Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
size Memory Usage (#341) 2023-09-04 10:16:52 +02:00
testdata
.gitignore Implement LPOS 2022-08-08 17:18:18 -07:00
CHANGELOG.md changelog for v2.33.0 2024-05-29 15:39:52 +02:00
check.go
cmd_client_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_client.go deal with tx and some more error checks 2023-09-27 12:23:40 +02:00
cmd_cluster_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_cluster.go random cleanup (#277) 2022-07-18 09:06:37 +02:00
cmd_command.go Bring back the COMMAND response fix introduced in #273 2023-04-17 14:02:40 -04:00
cmd_connection_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_connection.go redis 7.0 (#279) 2023-01-06 11:56:37 +01:00
cmd_generic_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_generic.go make go 1.17 the minimum version 2024-03-27 19:54:56 +01:00
cmd_geo_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_geo.go GEO* units are case insensitive since 7.0 2024-03-08 13:23:36 +01:00
cmd_hash_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_hash.go cleanup last commit 2023-12-13 12:09:29 +01:00
cmd_hll_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_hll.go random cleanup (#277) 2022-07-18 09:06:37 +02:00
cmd_info_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_info.go INFO_partially_support 2022-07-06 11:42:48 +02:00
cmd_list_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_list.go cleanup 2023-10-11 09:52:05 +02:00
cmd_object_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_object.go Add OBJECT IDLETIME command (#345) 2023-10-11 09:51:33 +02:00
cmd_pubsub_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_pubsub.go redis 7.0 (#279) 2023-01-06 11:56:37 +01:00
cmd_scripting_test.go Ignore 'redis.set_repl()' call 2024-04-02 02:48:51 +00:00
cmd_scripting.go Precompile lua functions to prevent wasted CPU cycles 2023-05-19 11:53:29 -07:00
cmd_server_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_server.go Memory Usage (#341) 2023-09-04 10:16:52 +02:00
cmd_set_test.go Return empty array for SRANDMEMBER on nonexistent key 2024-05-27 20:17:32 +09:00
cmd_set.go Return empty array for SRANDMEMBER on nonexistent key 2024-05-27 20:17:32 +09:00
cmd_sorted_set_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_sorted_set.go support ZINTER 2023-10-11 11:27:47 +02:00
cmd_stream_test.go but skip the invalid one 2024-05-10 22:27:01 +02:00
cmd_stream.go Support for the NOMKSTREAM option for XADD 2024-05-10 21:49:26 +02:00
cmd_string_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_string.go support SET NX GET special case 2023-03-08 18:27:49 +01:00
cmd_transactions_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
cmd_transactions.go redis 7.0 (#279) 2023-01-06 11:56:37 +01:00
db.go make go 1.17 the minimum version 2024-03-27 19:54:56 +01:00
direct.go Fix integer overflow 2024-03-23 08:42:17 +01:00
geo_test.go
geo.go run gofmt 2022-08-05 08:59:19 +02:00
go.mod make go 1.17 the minimum version 2024-03-27 19:54:56 +01:00
go.sum cleanup 2024-02-18 10:47:06 +01:00
hll.go
keys_test.go
keys.go
LICENSE
lua.go Ignore 'redis.set_repl()' call 2024-04-02 02:48:51 +00:00
Makefile Move .PHONY next to each target 2024-04-09 15:34:27 +02:00
miniredis_test.go Move create of client in tests to helper function 2024-03-27 19:55:20 +01:00
miniredis.go Add StartAddrTLS function 2024-04-29 11:01:31 +02:00
opts.go arg parse cleanup (#360) 2024-03-23 09:08:16 +01:00
pubsub.go
README.md upgrade to redis 7.2.4, which is the last released under BSD-3 license 2024-03-23 09:08:55 +01:00
redis_test.go Change Run() to RunT(t) in tests 2024-03-27 19:55:20 +01:00
redis.go Fix integer overflow 2024-03-23 08:42:17 +01:00
sorted_set_test.go
sorted_set.go
stream_test.go
stream.go test against 7.2.0 2023-09-27 12:03:07 +02:00
test_test.go

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.

Be sure to import v2:

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

Commands

Implemented commands:

  • Connection (complete)
    • AUTH -- see RequireAuth()
    • ECHO
    • HELLO -- see RequireUserAuth()
    • PING
    • SELECT
    • SWAPDB
    • QUIT
  • Key
    • COPY
    • DEL
    • EXISTS
    • EXPIRE
    • EXPIREAT
    • EXPIRETIME
    • KEYS
    • MOVE
    • PERSIST
    • PEXPIRE
    • PEXPIREAT
    • PEXPIRETIME
    • PTTL
    • RANDOMKEY -- see m.Seed(...)
    • RENAME
    • RENAMENX
    • 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()
    • COMMAND -- partly
    • INFO -- partly, returns only "clients" section with one field "connected_clients"
  • String keys (complete)
    • APPEND
    • BITCOUNT
    • BITOP
    • BITPOS
    • DECR
    • DECRBY
    • GET
    • GETBIT
    • GETRANGE
    • GETSET
    • GETDEL
    • GETEX
    • 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
    • HRANDFIELD
    • HSET
    • HSETNX
    • HSTRLEN
    • HVALS
    • HSCAN
  • List keys (complete)
    • BLPOP
    • BRPOP
    • BRPOPLPUSH
    • LINDEX
    • LINSERT
    • LLEN
    • LPOP
    • LPUSH
    • LPUSHX
    • LRANGE
    • LREM
    • LSET
    • LTRIM
    • RPOP
    • RPOPLPUSH
    • RPUSH
    • RPUSHX
    • LMOVE
    • BLMOVE
  • Pub/Sub (complete)
    • PSUBSCRIBE
    • PUBLISH
    • PUBSUB
    • PUNSUBSCRIBE
    • SUBSCRIBE
    • UNSUBSCRIBE
  • Set keys (complete)
    • SADD
    • SCARD
    • SDIFF
    • SDIFFSTORE
    • SINTER
    • SINTERSTORE
    • SINTERCARD
    • SISMEMBER
    • SMEMBERS
    • SMISMEMBER
    • SMOVE
    • SPOP -- see m.Seed(...)
    • SRANDMEMBER -- see m.Seed(...)
    • SREM
    • SSCAN
    • SUNION
    • SUNIONSTORE
  • Sorted Set keys (complete)
    • ZADD
    • ZCARD
    • ZCOUNT
    • ZINCRBY
    • ZINTER
    • ZINTERSTORE
    • ZLEXCOUNT
    • ZPOPMIN
    • ZPOPMAX
    • ZRANDMEMBER
    • ZRANGE
    • ZRANGEBYLEX
    • ZRANGEBYSCORE
    • ZRANK
    • ZREM
    • ZREMRANGEBYLEX
    • ZREMRANGEBYRANK
    • ZREMRANGEBYSCORE
    • ZREVRANGE
    • ZREVRANGEBYLEX
    • ZREVRANGEBYSCORE
    • ZREVRANK
    • ZSCORE
    • ZUNION
    • ZUNIONSTORE
    • ZSCAN
  • Stream keys
    • XACK
    • XADD
    • XAUTOCLAIM
    • XCLAIM
    • XDEL
    • XGROUP CREATE
    • XGROUP CREATECONSUMER
    • XGROUP DESTROY
    • XGROUP DELCONSUMER
    • XINFO STREAM -- partly
    • XINFO GROUPS
    • XINFO CONSUMERS -- partly
    • XLEN
    • XRANGE
    • XREAD
    • XREADGROUP
    • XREVRANGE
    • XPENDING
    • XTRIM
  • Scripting
    • EVAL
    • EVALSHA
    • SCRIPT LOAD
    • SCRIPT EXISTS
    • SCRIPT FLUSH
  • GEO
    • GEOADD
    • GEODIST
    • GEOHASH
    • GEOPOS
    • GEORADIUS
    • GEORADIUS_RO
    • GEORADIUSBYMEMBER
    • GEORADIUSBYMEMBER_RO
  • Cluster
    • CLUSTER SLOTS
    • CLUSTER KEYSLOT
    • CLUSTER NODES
  • HyperLogLog (complete)
    • PFADD
    • PFCOUNT
    • PFMERGE

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 := miniredis.RunT(t)

	// 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
  • Key
    • DUMP
    • MIGRATE
    • OBJECT
    • RESTORE
    • WAIT
  • Scripting
    • SCRIPT DEBUG
    • SCRIPT KILL
  • Server
    • BGSAVE
    • BGWRITEAOF
    • CLIENT *
    • CONFIG *
    • DEBUG *
    • LASTSAVE
    • MONITOR
    • ROLE
    • SAVE
    • SHUTDOWN
    • SLAVEOF
    • SLOWLOG
    • SYNC

&c.

Integration tests are run against Redis 7.2.4. The ./integration subdir compares miniredis against a real redis instance.

The Redis 6 RESP3 protocol is supported. If there are problems, please open an issue.

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

A changelog is kept at CHANGELOG.md.

Go Reference