Documentation ¶
Overview ¶
Package testsim implements a deterministic execution runtime driven by simulated logical time.
The runtime controls execution by means of processes. The process represent a context of execution within the simulation runtime. Each process can be either active or inactive. Active processes are allowed to execute while inactive processes are blocked waiting for certain conditions to hold before they can continue execution.
Processes can synchronize and exchange values through channels provided by the runtime. The channel represents an object which processes can send to or receive values from. An attempt to perform the send or receive operation on a channel deactivates the process and blocks waiting until another process performs the complementary operation on the channel. Once that happens, both processes become active and can continue execution, whereas the receiving process obtains the value supplied by the sending process. In case multiple processes get blocked trying to send/receive on the same channel, they complete the operations in FIFO order.
Processes can also get blocked by waiting for a specified duration of simulated time to expire. Such process gets activated back once the simulation reaches that point of simulated time.
The simulation proceeds in steps. Each step represents an instance of simulated time and lasts as long as there is an active process to execute. Execution within the same step is considered happening instantaneously in terms of simulated time. Once there is no more active process, the simulation can proceed to the next step that corresponds to the next action scheduled in the simulated time.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Chan ¶
type Chan struct {
// contains filtered or unexported fields
}
Chan represents a channel for synchronization and communication between processes in the simulated runtime.
type Process ¶
type Process struct { *Runtime // contains filtered or unexported fields }
Process represents a context of execution in the simulation.
func (*Process) Delay ¶
Delay suspends the execution and resumes it after d amount of simulated time. It returns false in case the process was killed while waiting.
func (*Process) Kill ¶
func (p *Process) Kill()
Kill immediately resumes and terminates the process.
func (*Process) Recv ¶
Recv attempts to receive a value from the channel. It either resumes the process waiting in Send operation on the channel, or blocks until another process attempts to send to the channel. It returns the received value and true once the operation is complete. It returns nil and false if the process was killed while waiting.
type Runtime ¶
Runtime provides a mechanism to run a simulation with logical time.
func NewRuntime ¶
NewRuntime creates a new simulation runtime given a source of pseudo-random numbers.
func (*Runtime) Run ¶
func (r *Runtime) Run()
Run executes the simulation until there is no more active process or scheduled action.
func (*Runtime) RunFor ¶
RunFor works the same as Run, but stops execution after expiration of the duration d of simulated time.