testredis

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package testredis is a throwaway redis-server for one test.

Start runs the redis-server on PATH as a child of the test binary and returns its address. What a test can rely on:

WHAT IT LISTENS ON. One TCP listener on 127.0.0.1, the IPv4 loopback, on a port the kernel chose. No other interface, no IPv6, no TLS port and no Unix socket. The address is host:port and not a socket path because every caller hands it to a Redis client's Addr or to a tool's --redis flag, and both dial TCP. An extra argument that would move the listener is refused.

IT IS THIS TEST'S ALONE. The port is taken from the kernel, closed and handed to redis-server, so another process can bind it in between. A server is therefore ready only when its OWN output says so, never because something answered on the port; one that lost its port exits and another port is taken. Many tests may call Start at once.

NOTHING IS KEPT. --save "" and no append-only file, in the test's own t.TempDir(). A test that wants persistence asks for it in the extra arguments, and the files still land in that directory.

IT DOES NOT OUTLIVE ITS TEST. The test's cleanup kills the server and waits until the process is gone, then the testing package removes the directory: after a pass, a failure or a panic in the test. When the test binary itself dies without its cleanups (a timeout, os.Exit, a kill) the sentry kills the server: see sentry.go. A server of a test binary that was killed leaves its directory, as every t.TempDir() of that binary does.

A MISSING redis-server skips the test on a laptop and fails it under NOVA_CI=1, which the CI workflows set: a skip there would let a green run be a run that never executed the store. A redis-server that is found and does not come up fails the test anywhere.

Index

Constants

View Source
const CIEnv = "NOVA_CI"

CIEnv is set to "1" by the CI workflows. A missing redis-server fails the test in that environment. Anywhere else it skips, so a laptop without the binary can still run the rest of the suite.

Variables

This section is empty.

Functions

func Absent

func Absent(t testing.TB, cause error)

Absent is Start's answer when redis-server is not on PATH. CI fails closed. A skip here would let a green run be a run that never executed the store.

func CommandCounter

func CommandCounter(t testing.TB) (addr string, count func() int64)

CommandCounter listens on 127.0.0.1, on a port the kernel chose, and counts every command a Redis client sends it, handshake included (#3277). It is not a redis-server and starts no process. It answers just enough RESP2 for a go-redis client to finish a command: HELLO is refused so the client falls back to RESP2, PING is PONG and anything else is OK. A test calls a package's Open or Dial against the address and asserts the count is still zero: the caller's first batch, not the open, is the first command.

func FreePort

func FreePort(t testing.TB) string

FreePort is a loopback port that was free a moment ago, as text. The kernel chose it. Redis treats --port 0 as "do not listen", so a test that starts a server itself takes the port here and hands it over.

func Program

func Program(t testing.TB) string

Program is the redis-server on PATH, or Absent's answer when there is none. A test that launches the server through its own production path (nova-redis serve) takes the program from here, so the missing-binary rule stays one.

func Start

func Start(t testing.TB, extra ...string) string

Start runs a throwaway redis-server and returns its address, 127.0.0.1 and a port, once the server has said it is ready and has answered a PING there (a NOAUTH answer is a live server: the default user may be off). It listens on that one loopback TCP port and nowhere else, keeps nothing, and is killed by the test's cleanup.

The extra arguments follow the fixed ones, so a test can add users (testredis.User), an ACL file, or persistence inside its own directory. Start refuses the ones that are its own: bind, port, tls-port and unixsocket (where it listens), dir and include (where its files are), logfile and loglevel (the output it reads), daemonize and supervised (the process it holds).

A server that does not come up fails the test with the binary, the arguments and the last of its output. Start waits thirty seconds for one server and takes five ports before it gives up.

func User

func User(name, password string) []string

User is the extra arguments of a server that requires a login: the default user off, and one named user with a password and every permission. A client that does not log in is answered NOAUTH.

Types

type Server

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

Server is one running throwaway redis-server.

func StartServer

func StartServer(t testing.TB, extra ...string) *Server

StartServer is Start with the server in hand, for a test that stops it before the test ends.

func (*Server) Addr

func (s *Server) Addr() string

Addr is the server's address, 127.0.0.1 and its port. It stays the same after Stop, when nothing listens there any more.

func (*Server) PID

func (s *Server) PID() int

PID is the server's process id.

func (*Server) Stop

func (s *Server) Stop()

Stop kills the server and returns when the process is gone, so the next command to its address is refused at once: the unreachable store of a test. The test's cleanup calls it too; a second call only waits.

Jump to

Keyboard shortcuts

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