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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is one running throwaway redis-server.
func StartServer ¶
StartServer is Start with the server in hand, for a test that stops it before the test ends.