disposable-redis: github.com/EverythingMe/disposable-redis Index | Examples | Files

package disposable_redis

import "github.com/EverythingMe/disposable-redis"

A utility to create disposable instances of redis server on random ports.

This can be used for testing redis dependent code without having to make assumptions on if and where redis server is running, or fear of corrupting data. You create a redis server instance, run your code against it as if it were a mock, and then remove it without a trace.



Package Files



const (
    MaxRetries = 10

    //this is the amount of time we give the server to start itself up and start listening (or fail)
    LaunchWaitTimeout = 100 * time.Millisecond


var RedisCommand = "redis-server"

The redis executable. This allows you to set it if you're using a custom one. Can be an absolute path, or an executable in your $PATH

type Server Uses

type Server struct {
    // contains filtered or unexported fields

A wrapper reperesenting a running disposable redis server


// create a new server on a random port
r, err := NewServerRandomPort()
if err != nil {
    panic("Could not create random server")

// we must remember to kill it at the end, or we'll have zombie redises
defer r.Stop()

// wait for our server to be ready for serving, for at least 50 ms.
// This gives redis time to initialize itself and listen
if err = r.WaitReady(50 * time.Millisecond); err != nil {
    panic("Couldn't connect to instance")

//now we can just connect and talk to it
conn, err := redigo.Dial("tcp", fmt.Sprintf("localhost:%d", r.Port()))
if err != nil {

fmt.Println(redigo.String(conn.Do("SET", "foo", "bar")))


OK <nil>

func NewServer Uses

func NewServer(port uint16) (*Server, error)

Create and run a new server on a given port. Return an error if the server cannot be started

func NewServerRandomPort Uses

func NewServerRandomPort() (*Server, error)

Create a new server on a random port. If the port is taken we retry (10 times). If we still couldn't start the process, we return an error

func (Server) Addr Uses

func (r Server) Addr() string

Addr returns the address of the server as a host:port string

func (Server) Info Uses

func (r Server) Info() (map[string]string, error)

Info returns the value of the server's INFO command parsed into a map of strings

func (Server) NewSlaveOf Uses

func (r Server) NewSlaveOf() (*Server, error)

NewSlaveOf creates a new server with a random port and makes it a slave of the current server.

func (Server) Port Uses

func (r Server) Port() uint16

Get the port of this server

func (*Server) Stop Uses

func (r *Server) Stop() error

Stop the running redis server

func (*Server) WaitReady Uses

func (r *Server) WaitReady(timeout time.Duration) error

Wait for the server to be ready, or until a timeout has elapsed. This just blocks and waits using sleep intervals of 5ms if it can't connect

Package disposable_redis imports 8 packages (graph) and is imported by 1 packages. Updated 2016-07-16. Refresh now. Tools for package owners.