sim

package
v0.12.14-0...-d6ed8d0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithState

func WithState[S any](fn func(rng *rand.Rand) *S) apply[S]

WithState configures the initial state for the simulation. The provided function receives a random number generator and should return a pointer to a newly initialized state with random values.

func WithValidator

func WithValidator[S any](validator Validator[S]) apply[S]

Types

type Event

type Event[State any] interface {
	// Name returns a human-readable name for the event, used for logging and debugging.
	Name() string

	// Run executes the event logic against the given state using the provided
	// random number generator. It should modify the state according to the event's
	// logic and return any errors that occur during execution.
	Run(rng *rand.Rand, state *State) error
}

Event defines the interface for simulation events that can be run against a state. Events are the building blocks of simulations, representing possible actions that can occur in the system being tested.

type Fail

type Fail struct {
	// Message is the error message to return when the event runs.
	Message string
}

Fail is an event that always fails with a specified error message. It's useful for testing error handling in simulations.

func (Fail) Name

func (f Fail) Name() string

Name returns the name of the fail event.

func (Fail) Run

func (f Fail) Run(rng *rand.Rand, state *any) error

Run always returns an error with the specified message.

type Idle

type Idle struct {
}

Idle is a no-op event that does nothing. It's useful as a placeholder or to simulate periods of inactivity.

func (Idle) Name

func (i Idle) Name() string

Name returns the name of the idle event.

func (Idle) Run

func (i Idle) Run(rng *rand.Rand, state *any) error

Run implements the Event interface but does nothing.

type Rand

type Rand = rand.Rand

type Seed

type Seed [32]byte

func NewSeed

func NewSeed() Seed

NewSeed generates a cryptographically secure random 32-byte seed suitable for simulations requiring high entropy and unpredictability.

func NewSeedFromInt

func NewSeedFromInt(i int) Seed

func (Seed) String

func (s Seed) String() string

type Simulation

type Simulation[State any] struct {
	Errors []error // Errors encountered during the simulation
	// contains filtered or unexported fields
}

Simulation manages a property-based testing simulation with a typed state. It provides a framework for running randomized sequences of events against a system state to discover edge cases and bugs that might not be found with traditional unit testing.

func New

func New[State any](seed Seed, fns ...apply[State]) *Simulation[State]

New creates a new simulation for property-based testing. It initializes the simulation with the provided seed and applies any configuration functions provided.

Example:

sim := sim.New[MyState](sim.NewSeed(),
    sim.WithSteps(1000000),
    sim.WithState(func(rng *rand.Rand) *MyState {
        // Initialize state with random values
        return &MyState{
            Counter: rng.Intn(100),
            Name:    fmt.Sprintf("test-%d", rng.Intn(1000)),
        }
    }),
)

func (*Simulation[State]) PrintEventStats

func (s *Simulation[State]) PrintEventStats()

func (*Simulation[State]) Run

func (s *Simulation[State]) Run(events []Event[State]) error

Run executes the simulation with the provided events. It runs the configured number of steps, selecting a random event at each step with weighted probability. Any errors are collected in the Errors slice.

Example:

sim.Run([]sim.Event[MyState]{
    &AddEvent{},
    &RemoveEvent{},
    &UpdateEvent{},
})

func (*Simulation[State]) State

func (s *Simulation[State]) State() *State

type Validator

type Validator[S any] func(*S) error

Jump to

Keyboard shortcuts

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