README.md
Hub test helpers
These fixtures use no daemon, XDG state, or JSONL parsing. Register Endpoint.Close
with t.Cleanup immediately after construction. Close joins its goroutines and
removes its temporary socket directory. Tests own and must close client connections
and both ends of any net.Pipe they create.
Scripted Unix endpoint
NewEndpoint(EndpointOptions, scripts ...[]Step) assigns one script to each
connection in accept order. A script closes its connection when it finishes or
fails. Extra connections are counted and immediately closed.
EndpointOptions.SocketBase selects an existing root for private socket
subdirectories. On Unix the default is /tmp, independent of TMPDIR, because
AF_UNIX paths have a small platform-specific length limit. On Windows the default
is os.TempDir(); supply a short root if that path is too long. Close removes the
endpoint's subdirectory, never the caller's root. A missing or unusable root is
an error; the helper does not silently choose a different location.
Each Step selects exactly one operation:
Read: nconsumes exactly n inbound bytes, retaining a partial read on error.Write: []byte(...)sends those bytes unchanged. Separate writes can split a JSONL frame; ending the script after a prefix simulates a truncated response.Gate: chwaits until ch receives or closes. Closing a channel releases all steps sharing it. Endpoint timeout or Close also interrupts the wait.
For example, a reconnect test can supply two scripts:
| 1 | gate := make(chan struct{}) |
| 2 | e, err := testkit.NewEndpoint(testkit.EndpointOptions{}, |
| 3 | []testkit.Step{ |
| 4 | {Read: len(request)}, |
| 5 | {Write: []byte("{\"event\":")}, |
| 6 | }, |
| 7 | []testkit.Step{ |
| 8 | {Read: len(resumeRequest)}, |
| 9 | {Gate: gate}, |
| 10 | {Write: []byte("{\"event\":\"done\"}\n")}, |
| 11 | }, |
| 12 | ) |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | t.Cleanup(func() { e.Close() }) |
| 17 | // Dial e.Path with network "unix"; close gate when ready to release the response. |
Wait(ctx, index) waits for one script. Its returned error describes failure to
wait; Capture.Err describes script I/O or cancellation failure. Snapshot(index)
returns an independent copy without waiting. Capture.Bytes preserves whitespace,
CRLF, empty lines, and unfinished tails. Capture.Lines contains only complete
lines, including newline terminators. Capture updates after each Read step returns,
not after each underlying socket read. Unscripted input is not consumed or captured.
Connections() reports accepts, not dial attempts.
Default bounds are five seconds of total endpoint lifetime and one MiB of inbound capture per script. Negative bounds and scripts exceeding the capture limit are rejected before opening the listener. Set a longer timeout explicitly for tests that need it. Timeout closes the listener and active connections; still call Close to join goroutines and remove the directory. Concurrent accepted connections run independently. Coordinate reconnect order through completed scripts rather than assuming concurrent dials have a particular order. Socket writes do not guarantee client read boundaries; use gates or the stream wrapper when boundaries matter.
Controlled stream faults
NewFaultConn(conn, StreamOptions) wraps any net.Conn. Use net.Pipe for a
controlled in-memory stream. The wrapper starts no goroutines, buffers no payloads,
and forwards deadlines and Close to the underlying connection.
MaxReadandMaxWritecap bytes transferred per call. Zero disables the cap. MaxWrite deliberately permits short writes with nil errors to test retry loops.ReadFaultandWriteFaulteach accept&Fault{After: n, Err: err}. After n successful bytes, the fault becomes sticky. The boundary call returns its byte count together with the error. Offset zero fails immediately; nil Err selectsio.ErrUnexpectedEOF. A real transport error takes precedence on the same call.- Empty buffers return zero and nil without consuming a fault.
Options and fault specifications are copied at construction. Reads and writes may run concurrently; calls in the same direction serialize. Underlying deadlines and Close release blocked operations. Error values and gate channels are shared with the caller and should not be mutated unsafely. No manual clock is included because these helpers do not schedule protocol timers.
NewByteHeavyDirectory constructs public-only signed directory capacity fixtures
without certificates, disk state or network services. Distributed revocation tags
fill the signed-frame byte allowance within 8 KiB. It returns an initial catalog,
a newer publication, and a replacement of every revocation list. Keys are erased
after construction. Build the fixture before starting service protocol clocks;
the runtime tests still use real budgets and unchanged transport deadlines.