disposable_redis

package module
v0.0.0-...-b781276 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 29, 2016 License: BSD-2-Clause-Views Imports: 8 Imported by: 2

README

Disposable-Redis

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 just create a redis server instance, run your code against it as if it were a mock, and then remove it without a trace. The only assumption here is that you have redis-server available in your path.

For full documentation see http://godoc.org/github.com/EverythingMe/disposable-redis

Example:


import (
	"fmt"
	"time"
	disposable "github.com/EverythingMe/disposable-redis"
	redigo "github.com/garyburd/redigo/redis"
)

func ExampleServer() {

	// create a new server on a random port
	r, err := disposable.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", r.Addr())
	if err != nil {
		panic(err)
	}

	fmt.Println(redigo.String(conn.Do("SET", "foo", "bar")))
	//Output: OK <nil>

}

Documentation

Overview

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.

Index

Examples

Constants

View Source
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
)

Variables

View Source
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

Functions

This section is empty.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

A wrapper reperesenting a running disposable redis server

Example
// 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 {
	panic(err)
}

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

OK <nil>

func NewServer

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

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

func (r Server) Addr() string

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

func (Server) Info

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

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

func (r Server) Port() uint16

Get the port of this server

func (*Server) Stop

func (r *Server) Stop() error

Stop the running redis server

func (*Server) WaitReady

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL