clienttest
Import path: github.com/AmiraliOmidvar/mirexa/client/clienttest
Package clienttest provides an in-process mirexa server for testing programs built on the client SDK.
It is the SDK’s answer to net/http/httptest: start one, point a client at its address, and assert on behavior without running a real node, a real WAL, or a real disk. Downstream projects import it for their own unit tests — which is why it lives here rather than in an internal package.
What it is and is not
It speaks the real wire protocol, using the same codecs the real server does, and it answers the frame types the SDK sends — all but FILL_GAP, ACCEPT_GAP, LIST_NODES and NODE_STATS, which it refuses as Unsupported. It is NOT the storage engine: records live in a map, queries are a linear scan, and nothing is durable. Do not use it to test storage behavior — use a real node for that. Use this to test YOUR code’s reaction to a server that answers, refuses, or goes away.
Fault injection is the point
The happy path is the easy half. The reason to reach for a mock is the other one: FailNext makes the next request answer a chosen error code, and Close severs every connection so a client’s reconnect path can be driven deliberately rather than waited for.
type Server struct {
// Has unexported fields.
}
Server is a mirexa server for tests. Create with NewServer, point a client at Addr, and Close when done. Safe for concurrent use.
func NewServer() (*Server, error)
NewServer starts a server on a loopback port.
func (s *Server) Addr() string
Addr is the host:port to dial.
func (s *Server) Close() error
Close stops the server and severs every connection.
func (s *Server) FailNext(code ErrorCode, message string)
FailNext makes the next request answer an ERROR frame with code, then resumes normal service. Use it to drive a client’s error handling — ErrorCodeOverloaded to exercise shed-and-retry, ErrorCodeUnauthorized to exercise a permission failure — without contriving the condition.
func (s *Server) GrantPermissions(bitmap uint64)
GrantPermissions makes every subsequent handshake grant no more than bitmap, whatever the client asked for. Zero restores the default of granting the request in full.
This is how a downstream test drives its own permission-denied path: dial, observe Client.GrantedPermissions come back narrower than requested, and check that the code under test notices before it tries the write.
func (s *Server) LastToken() string
LastToken returns the token string presented at the most recent handshake, or “” if the client dialed without one. Useful for asserting that code under test actually passes its token through.
func (s *Server) PublishCandles(streamID StreamID, candles []Candle)
PublishCandles delivers candles to every subscriber of streamID as a live batch, so a test can drive a subscription without an ingest race.
func (s *Server) RequestCount(t wire.FrameType) int
RequestCount reports how many frames of a type the server has handled. It is how a test asserts that a client retried, or did not.
func (s *Server) SeedCandles(streamID StreamID, candles []Candle)
SeedCandles stores candles so a query can find them, bypassing ingest.
func (s *Server) SeedGaps(streamID StreamID, gaps []GapRange)
SeedGaps stores gaps so ListGaps can find them, sorted by start as gap.idx keeps them.
Gaps are seeded rather than derived from the candles this server holds, because the distinction that matters here cannot be derived: a hole between two candles could be an outage or a market closure, and which one it is comes from a trading calendar, not from the absence itself. A fixture that guessed would teach a downstream test the wrong lesson about the one thing this engine exists to get right.
func (s *Server) SeedStreamMetadata(streamID StreamID, md *StreamMetadata)
SeedStreamMetadata stores a stream’s metadata as if it had been set, registering the stream. Its lineage, if any, is kept as given.
func (s *Server) SeedTicks(streamID StreamID, ticks []Tick)
SeedTicks stores ticks so a tick query can find them.