ssetest

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package ssetest is the test seam between the sse hub and its clients: an httptest server for a hub, a frame reader for the wire, a recorder that answers http.ErrNotSupported to the deadline setters, and Fixture, the controllable server the TypeScript integration suites drive through the ssetest/cmd binary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serve

func Serve(t testing.TB, h *sse.Hub, opts ...sse.ServeOption) string

Serve starts an httptest server whose every request is h.Serve with opts and returns its URL. The server closes at test cleanup; the caller still owns the hub and shuts it down.

Types

type Fixture

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

Fixture is the controllable SSE server the integration suites drive: a hub that /control/restart replaces, an in-memory subject store with one version counter per key, a presence table keyed by client tag, a stamped REST stub, and flags that stall, delay, fail or close a stream on demand. Every method is safe for concurrent use.

func NewFixture

func NewFixture(opts ...sse.Option) *Fixture

NewFixture builds a fixture whose hub carries WithReplay(1024), WithReplayTTL(10m), WithReplyMaxEvents(256), WithClientBuffer(4096) and the fixture's own presence hook, then the caller's options. It panics on an incoherent option set, which is a fixture bug rather than a runtime condition.

func (*Fixture) Handler

func (f *Fixture) Handler() http.Handler

Handler mounts the fixture's routes on a fresh ServeMux.

func (*Fixture) Hub

func (f *Fixture) Hub() *sse.Hub

Hub returns the current hub; /control/restart replaces it.

func (*Fixture) Shutdown

func (f *Fixture) Shutdown(ctx context.Context) error

Shutdown shuts the current hub down, ending every stream with sse:reset.

type Frame

type Frame struct {
	ID    string
	Event string
	Data  string
}

Frame is one dispatched event: the id: field, the event: name and the data: lines joined by LF. A retry: field is reported as a Frame with Event "retry" and the milliseconds in Data.

func ReadFrames

func ReadFrames(r io.Reader, n int) ([]Frame, error)

ReadFrames parses the WHATWG event-stream line grammar from r and returns the first n dispatched frames, or every frame up to EOF when n <= 0. Comment lines are skipped, and a blank line that closes no data or retry field dispatches nothing, exactly as a browser's parser behaves.

This is the one-shot form: the buffered reader is discarded on return, and with it any bytes it read past frame n. Read successive batches off one live stream through a FrameReader instead.

type FrameReader added in v1.1.0

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

FrameReader parses dispatched frames off one event stream across successive reads. It owns the buffered reader for the whole stream, which is what makes the second read correct: a buffered reader fills in chunks, so the bytes of frame n+1 routinely arrive in the same read as frame n, and a reader created per call would discard them along with itself. Not safe for concurrent use.

func NewFrameReader added in v1.1.0

func NewFrameReader(r io.Reader) *FrameReader

NewFrameReader returns a FrameReader over r, buffered for the hub's largest frame.

func (*FrameReader) Read added in v1.1.0

func (fr *FrameReader) Read(n int) ([]Frame, error)

Read returns the next n dispatched frames, or every remaining frame up to EOF when n <= 0, and reports io.ErrUnexpectedEOF when the stream ended before n frames dispatched.

type Recorder

type Recorder struct {
	*httptest.ResponseRecorder
}

Recorder is an httptest.ResponseRecorder that flushes but answers http.ErrNotSupported to both deadline setters, the shape of a wrapper that exposes Flush without Unwrap, so a Serve against it runs the tolerated unbounded path.

func NewRecorder

func NewRecorder() *Recorder

NewRecorder returns a Recorder over a fresh httptest.ResponseRecorder.

func (*Recorder) Frames

func (r *Recorder) Frames() ([]Frame, error)

Frames parses everything written so far.

func (*Recorder) SetReadDeadline

func (r *Recorder) SetReadDeadline(time.Time) error

SetReadDeadline always answers http.ErrNotSupported.

func (*Recorder) SetWriteDeadline

func (r *Recorder) SetWriteDeadline(time.Time) error

SetWriteDeadline always answers http.ErrNotSupported.

Directories

Path Synopsis
Command ssetest serves an ssetest.Fixture for the TypeScript integration suites.
Command ssetest serves an ssetest.Fixture for the TypeScript integration suites.

Jump to

Keyboard shortcuts

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