clienttest

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package clienttest is an in-process fake of Rex's control endpoint for hermetic tests of rexuikit/client and the packages built on it.

It serves over httptest's TCP loopback rather than a unix socket because the sandbox the default test run uses denies unix-socket bind(); a unix fake would fail every sandboxed `make check`. Server is also an http.Handler, so a test that can bind a unix socket may serve it there.

The fake speaks the wire protocol from docs/rex-api/llms-r7.md ("## The Control Connection") and nothing more: methods answer only what a test registers with Handle, and everything else gets method_not_found.

It accepts any number of simultaneous connections, as the real server does (a short-lived call connection closing must not hide a long-lived event connection): Emit reaches all of them, EmitAttachAll only those that called session.attach_all, and a handler can see its caller with CallerFrom.

It deliberately does not import rexuikit/client, so tests inside package client can use it without an import cycle.

Index

Constants

View Source
const Subprotocol = "rex.control.v1"

Variables

This section is empty.

Functions

func BlockEventOf

func BlockEventOf(sessionID, blockID, creator, name string, payload any) any

BlockEventOf is a block's own event inside a session_event.

func DefaultWelcome

func DefaultWelcome(Hello) any

DefaultWelcome is the welcome a revision-7 server sends.

func EventOf

func EventOf(typ string, payload any) any

EventOf is a top-level event such as session_created.

func SessionEventOf

func SessionEventOf(sessionID, typ string, payload any) any

SessionEventOf wraps an event in a session_event ("### Events").

Types

type Caller added in v0.3.0

type Caller struct {
	ID         int      // 1 for the first connection the server accepted
	EventScope string   // "" until the connection calls session.attach_all
	Attached   []string // sessions this connection called session.attach for
}

Caller describes the connection a request arrived on, as the fake has seen it so far: what it attached to and the event scope it asked for. A handler reads it with CallerFrom, to model the server's rule that session.disown_block fails with conflict on a connection that never called session.attach.

func CallerFrom added in v0.3.0

func CallerFrom(ctx context.Context) Caller

CallerFrom returns the connection a handler's request arrived on. The zero Caller means ctx did not come from a handler.

func (Caller) Has added in v0.3.0

func (c Caller) Has(sessionID string) bool

Has reports whether the connection called session.attach for sessionID.

type Close

type Close struct {
	Code   websocket.StatusCode
	Reason string
}

Close, returned from a WelcomeFunc, makes the fake close the connection with this status instead of welcoming the client, as the real server does for no common API revision (status 1008).

type Error

type Error struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Status  int    `json:"status"`
}

Error is a scripted error response ("### Errors").

func (*Error) Error

func (e *Error) Error() string

type HandlerFunc

type HandlerFunc func(ctx context.Context, req Request) (result any, err *Error)

HandlerFunc answers one request. It runs on its own goroutine, so a handler may block (on ctx or a test channel) to force responses out of order. ctx ends when the connection does. A non-nil *Error becomes an error response; result is marshalled as the payload otherwise.

type Hello

type Hello struct {
	Type           string          `json:"type"`
	APIRevision    int             `json:"api_revision"`
	APIMinRevision int             `json:"api_min_revision"`
	ReconnectToken string          `json:"reconnect_token,omitempty"`
	Client         json.RawMessage `json:"client"`
	Raw            []byte          `json:"-"`
}

type Request

type Request struct {
	ID      string          `json:"id"`
	Method  string          `json:"method"`
	Payload json.RawMessage `json:"payload"`
}

type Server

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

Server is the fake. Configure it before the client dials.

func New

func New(t testing.TB) *Server

New starts a fake on TCP loopback and closes it when the test ends.

func (*Server) Callers added in v0.3.0

func (s *Server) Callers() []Caller

Callers returns the attach state of every live connection, oldest first.

func (*Server) Close

func (s *Server) Close()

func (*Server) CloseConn

func (s *Server) CloseConn(code websocket.StatusCode, reason string) error

func (*Server) Connections added in v0.3.0

func (s *Server) Connections() int

Connections is the number of live connections.

func (*Server) Drop

func (s *Server) Drop() error

Drop closes the client's connection without a close frame, as a crashed server or a lost network would.

func (*Server) Emit

func (s *Server) Emit(event any) (int64, error)

Emit sends an event to every live connection, each with its own next seq (seq is per connection), and returns the seq of the newest connection's copy. event is the inner event, {type, payload}; build one with EventOf, SessionEventOf or BlockEventOf. It fails when no client is connected.

func (*Server) EmitAttachAll added in v0.3.0

func (s *Server) EmitAttachAll(event any) (int64, error)

EmitAttachAll is Emit limited to connections that called session.attach_all, the only ones the real server sends session-wide events to. It does not model event_scope filtering of individual event types: a test that cares emits only what its scope would receive.

func (*Server) Errs

func (s *Server) Errs() []error

Errs returns protocol violations and I/O failures the fake saw. The fake never calls t.Fatal from its goroutines (that would hang the test); tests assert on this instead.

func (*Server) Fail

func (s *Server) Fail(method, code string, status int, message string)

Fail registers a handler that always fails with the given error.

func (*Server) HTTPClient

func (s *Server) HTTPClient() *http.Client

func (*Server) Handle

func (s *Server) Handle(method string, h HandlerFunc)

func (*Server) Hellos

func (s *Server) Hellos() []Hello

func (*Server) Reply

func (s *Server) Reply(method string, result any)

Reply registers a handler that always returns result.

func (*Server) Requests

func (s *Server) Requests() []Request

Requests returns every request received, in arrival order.

func (*Server) Send

func (s *Server) Send(msg any) error

Send writes an arbitrary message, bypassing the fake's bookkeeping. Tests use it for malformed or out-of-sequence traffic.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) SetWelcome

func (s *Server) SetWelcome(f WelcomeFunc)

SetWelcome replaces the welcome behaviour for the next connection.

func (*Server) WaitConnected

func (s *Server) WaitConnected(ctx context.Context) error

WaitConnected blocks until at least one client is connected, or ctx ends.

type WelcomeFunc

type WelcomeFunc func(Hello) any

WelcomeFunc chooses the server's first message for a hello. Return a value to marshal and send, a Close to refuse the client, or nil to send nothing at all (a server that never welcomes).

Jump to

Keyboard shortcuts

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