Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithState ¶
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 ¶
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.
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.
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 ¶
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