testkit

package
v0.0.0-...-1253d6d Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertDomainErrorCode

func AssertDomainErrorCode(t testing.TB, err error, code coreerrors.ErrorCode)

AssertDomainErrorCode asserts that err is a domain error with the given code. If err is nil or not a *coreerrors.DomainError, the test fails. Use this when the client surfaces server error codes so tests are stable against message text changes.

func AssertFrameValid

func AssertFrameValid(t *testing.T, frame []byte)

AssertFrameValid performs basic validation on a frame.

func AssertLengthPrefix

func AssertLengthPrefix(t *testing.T, data []byte) uint32

AssertLengthPrefix verifies and extracts TCP length prefix.

func AssertPayloadStructure

func AssertPayloadStructure(t *testing.T, payload []byte, minSize int)

AssertPayloadStructure verifies payload format without decoding details. Used to check that payload has minimum expected structure.

func AssertRouteValid

func AssertRouteValid(t *testing.T, route string)

AssertRouteValid checks if a route string is valid.

func BrokerConnectable

func BrokerConnectable(addr string) bool

BrokerConnectable checks if a broker is reachable.

func BytesToString

func BytesToString(b []byte) string

BytesToString is a helper to convert bytes to string.

func CloseQuietly

func CloseQuietly[T interface{ Close() error }](value T)

CloseQuietly closes a resource and discards the close error.

func ContextWithCancel

func ContextWithCancel() (context.Context, context.CancelFunc)

ContextWithCancel creates a cancellable context for testing.

func ExtractKeyValueFromPayload

func ExtractKeyValueFromPayload(payload []byte, startOffset int) (key []byte, value []byte, err error)

ExtractKeyValueFromPayload extracts key and value from a KV payload. Assumes format: [...][key_len (4)][key][value_len (4)][value][...] startOffset is the byte position where key_len begins.

func ExtractRouteFromPayload

func ExtractRouteFromPayload(payload []byte) (string, error)

ExtractRouteFromPayload extracts a route string from a payload. Assumes format: [route_len (4)][route][...]

func FastContext

func FastContext() (context.Context, context.CancelFunc)

FastContext creates a very short timeout context for testing timeouts.

func GenerateFrame

func GenerateFrame(msgType uint16, payloadSize int) []byte

GenerateFrame creates a complete TLV frame with specified message type and payload size.

func GenerateKey

func GenerateKey(size int) []byte

GenerateKey creates a key of specified size for testing.

func GenerateRoute

func GenerateRoute(domain string) string

GenerateRoute creates a route string for testing.

func GenerateString

func GenerateString(length int) string

GenerateString creates a string of the specified length.

func GenerateValue

func GenerateValue(size int) []byte

GenerateValue creates a value of specified size for testing.

func PrecomputeFrames

func PrecomputeFrames(count int, avgSize int) [][]byte

PrecomputeFrames creates precomputed test frames for benchmarking.

func ReleaseQuietly

func ReleaseQuietly[T interface{ Release(context.Context) error }](ctx context.Context, value T)

ReleaseQuietly releases a resource and discards the release error.

func RollbackQuietly

func RollbackQuietly[T interface{ Rollback(context.Context) error }](ctx context.Context, value T)

RollbackQuietly rolls back a resource and discards the rollback error.

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes is a helper to convert string to bytes.

func TCPFrameWrapper

func TCPFrameWrapper(frame []byte) []byte

TCPFrameWrapper wraps a frame with TCP length prefix.

func TimeoutContext

func TimeoutContext(timeout time.Duration) (context.Context, context.CancelFunc)

TimeoutContext creates a context with a specific timeout for testing.

func UniqueArea

func UniqueArea() string

UniqueArea generates a unique area name.

func UniqueOperationID

func UniqueOperationID() string

UniqueOperationID generates a unique operation ID for test isolation.

func UniqueRealm

func UniqueRealm() string

UniqueRealm generates a unique realm name.

func UniqueResource

func UniqueResource() string

UniqueResource generates a unique resource name.

func UniqueRoute

func UniqueRoute(domain string) string

UniqueRoute generates a unique route for test isolation.

Types

type MockTCPConn

type MockTCPConn struct {
	ToRead           []byte
	Written          []byte
	ReadPos          int
	Blocked          bool
	Closed           bool
	ReadDelay        time.Duration
	WriteDelay       time.Duration
	MaxWriteSize     int
	RemoteAddrString string
	// contains filtered or unexported fields
}

MockTCPConn is a mock TCP connection for testing transport layer.

func (*MockTCPConn) Close

func (m *MockTCPConn) Close() error

func (*MockTCPConn) LocalAddr

func (m *MockTCPConn) LocalAddr() net.Addr

func (*MockTCPConn) Read

func (m *MockTCPConn) Read(b []byte) (int, error)

func (*MockTCPConn) RemoteAddr

func (m *MockTCPConn) RemoteAddr() net.Addr

func (*MockTCPConn) SetDeadline

func (m *MockTCPConn) SetDeadline(t time.Time) error

func (*MockTCPConn) SetReadDeadline

func (m *MockTCPConn) SetReadDeadline(t time.Time) error

func (*MockTCPConn) SetWriteDeadline

func (m *MockTCPConn) SetWriteDeadline(t time.Time) error

func (*MockTCPConn) Write

func (m *MockTCPConn) Write(b []byte) (int, error)

type MockTransport

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

MockTransport is a controllable transport for unit tests. It allows tests to specify read responses and track written frames.

func NewMockTransport

func NewMockTransport() *MockTransport

NewMockTransport creates a new mock transport.

func (*MockTransport) Close

func (m *MockTransport) Close() error

Close marks the transport as closed.

func (*MockTransport) GetWrittenFrames

func (m *MockTransport) GetWrittenFrames() [][]byte

GetWrittenFrames returns all frames written via Write().

func (*MockTransport) Read

func (m *MockTransport) Read(ctx context.Context) ([]byte, error)

Read returns the next queued read frame or an error.

func (*MockTransport) RemoteAddr

func (m *MockTransport) RemoteAddr() string

RemoteAddr returns the remote address.

func (*MockTransport) SetReadError

func (m *MockTransport) SetReadError(err error)

SetReadError sets an error to return for all Read() calls.

func (*MockTransport) SetReadFrames

func (m *MockTransport) SetReadFrames(frames [][]byte)

SetReadFrames sets the frames to return on Read() calls.

func (*MockTransport) SetWriteError

func (m *MockTransport) SetWriteError(err error)

SetWriteError sets an error to return for all Write() calls.

func (*MockTransport) Write

func (m *MockTransport) Write(ctx context.Context, frame []byte) error

Write appends the frame to the written frames list.

type MockWSConn

type MockWSConn struct {
	NextMessage    []byte
	Messages       [][]byte
	IsText         bool
	Blocked        bool
	Closed         bool
	ReadBuf        []byte
	MaxWriteSize   int
	RemoteAddrHost string
	// contains filtered or unexported fields
}

MockWSConn is a mock WebSocket connection for testing.

func (*MockWSConn) Close

func (m *MockWSConn) Close() error

func (*MockWSConn) LocalAddr

func (m *MockWSConn) LocalAddr() net.Addr

func (*MockWSConn) Read

func (m *MockWSConn) Read(b []byte) (int, error)

func (*MockWSConn) RemoteAddr

func (m *MockWSConn) RemoteAddr() net.Addr

func (*MockWSConn) SetDeadline

func (m *MockWSConn) SetDeadline(t time.Time) error

func (*MockWSConn) SetReadDeadline

func (m *MockWSConn) SetReadDeadline(t time.Time) error

func (*MockWSConn) SetWriteDeadline

func (m *MockWSConn) SetWriteDeadline(t time.Time) error

func (*MockWSConn) Write

func (m *MockWSConn) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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