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
- func BlockEventOf(sessionID, blockID, creator, name string, payload any) any
- func DefaultWelcome(Hello) any
- func EventOf(typ string, payload any) any
- func SessionEventOf(sessionID, typ string, payload any) any
- type Caller
- type Close
- type Error
- type HandlerFunc
- type Hello
- type Request
- type Server
- func (s *Server) Callers() []Caller
- func (s *Server) Close()
- func (s *Server) CloseConn(code websocket.StatusCode, reason string) error
- func (s *Server) Connections() int
- func (s *Server) Drop() error
- func (s *Server) Emit(event any) (int64, error)
- func (s *Server) EmitAttachAll(event any) (int64, error)
- func (s *Server) Errs() []error
- func (s *Server) Fail(method, code string, status int, message string)
- func (s *Server) HTTPClient() *http.Client
- func (s *Server) Handle(method string, h HandlerFunc)
- func (s *Server) Hellos() []Hello
- func (s *Server) Reply(method string, result any)
- func (s *Server) Requests() []Request
- func (s *Server) Send(msg any) error
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) SetWelcome(f WelcomeFunc)
- func (s *Server) WaitConnected(ctx context.Context) error
- type WelcomeFunc
Constants ¶
const Subprotocol = "rex.control.v1"
Variables ¶
This section is empty.
Functions ¶
func BlockEventOf ¶
BlockEventOf is a block's own event inside a session_event.
func DefaultWelcome ¶
DefaultWelcome is the welcome a revision-7 server sends.
func SessionEventOf ¶
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
CallerFrom returns the connection a handler's request arrived on. The zero Caller means ctx did not come from a handler.
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").
type HandlerFunc ¶
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 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 (*Server) Callers ¶ added in v0.3.0
Callers returns the attach state of every live connection, oldest first.
func (*Server) CloseConn ¶
func (s *Server) CloseConn(code websocket.StatusCode, reason string) error
func (*Server) Connections ¶ added in v0.3.0
Connections is the number of live connections.
func (*Server) Drop ¶
Drop closes the client's connection without a close frame, as a crashed server or a lost network would.
func (*Server) Emit ¶
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
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 ¶
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) HTTPClient ¶
func (*Server) Handle ¶
func (s *Server) Handle(method string, h HandlerFunc)
func (*Server) Send ¶
Send writes an arbitrary message, bypassing the fake's bookkeeping. Tests use it for malformed or out-of-sequence traffic.
func (*Server) SetWelcome ¶
func (s *Server) SetWelcome(f WelcomeFunc)
SetWelcome replaces the welcome behaviour for the next connection.
type WelcomeFunc ¶
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).