redistest

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package redistest is a small Redis server for tests. It speaks enough RESP2 to answer the commands a client sends, including a pipeline and a MULTI/EXEC transaction. It is not a Redis implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(args []string) Value

Handler answers one user command. args[0] is the command name as the client sent it. Built-in commands (HELLO, AUTH, SELECT, PING, QUIT, MULTI, EXEC, DISCARD) are not passed to the handler.

type Kind

type Kind int

Kind is the RESP type of a Value.

const (
	// Simple is a status reply, such as OK or PONG.
	Simple Kind = iota
	// Error is a Redis error reply. Text is the message, including the code word.
	Error
	// Integer is a number reply.
	Integer
	// Bulk is a string reply.
	Bulk
	// Nil is a missing value.
	Nil
	// Array is a list of values.
	Array
)

type Server

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

Server is a running fake server.

func Start

func Start(t testing.TB, h Handler) *Server

Start listens on 127.0.0.1 and serves until the test ends.

func StartTLS

func StartTLS(t testing.TB, h Handler) *Server

StartTLS is Start behind a self-signed certificate for 127.0.0.1.

func StartTLSConfig added in v0.8.0

func StartTLSConfig(t testing.TB, h Handler, cfg *tls.Config) *Server

StartTLSConfig is Start behind the given TLS config. The URL is rediss://.

func StartWithPassword

func StartWithPassword(t testing.TB, h Handler, password string) *Server

StartWithPassword is Start, and HELLO or AUTH must present password. A wrong password is a Redis error that does not contain the password.

func (*Server) Addr

func (s *Server) Addr() string

Addr is host:port.

func (*Server) CloseAfterUserCommands

func (s *Server) CloseAfterUserCommands(n int)

CloseAfterUserCommands closes the connection after n user commands have been read, without writing a reply. Setup commands do not count. n is counted per connection.

func (*Server) CloseOnConnect added in v0.9.0

func (s *Server) CloseOnConnect()

CloseOnConnect closes each accepted connection without reading or writing.

func (*Server) Commands

func (s *Server) Commands() [][]string

Commands are the user commands received, in order, without HELLO, AUTH or SELECT. MULTI and EXEC are included. Each entry is the argument list.

func (*Server) ConnCount

func (s *Server) ConnCount() int

ConnCount is how many connections were accepted.

func (*Server) DisableHello

func (s *Server) DisableHello()

DisableHello makes HELLO answer with an unknown-command error, so a client falls back to AUTH and SELECT.

func (*Server) Setup

func (s *Server) Setup() [][]string

Setup is HELLO, AUTH and SELECT, in the order they were received.

func (*Server) URL

func (s *Server) URL() string

URL is a redis:// or rediss:// URL for this server.

type Value

type Value struct {
	Kind  Kind
	Text  string
	Int   int64
	Items []Value
}

Value is one RESP reply.

func ArrayOf

func ArrayOf(items ...Value) Value

ArrayOf is a list reply.

func BulkString

func BulkString(s string) Value

BulkString is a string reply.

func ErrorString

func ErrorString(s string) Value

ErrorString is an error reply. s should start with the code word, such as "ERR something".

func Int

func Int(n int64) Value

Int is an integer reply.

func NilValue

func NilValue() Value

NilValue is a missing value.

func SimpleString

func SimpleString(s string) Value

SimpleString is a status reply.

Jump to

Keyboard shortcuts

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