Deterministic Simulation Testing (DST)
Relevant source files
The following files were used as context for generating this wiki page:
Deterministic Simulation Testing (DST) provides a game-logic-agnostic fuzzer and structural state checker for the Cardinal engine. It verifies engine correctness by generating random commands via reflection, injecting randomized engine operations, and validating structural ECS invariants after every tick. Correctness of game logic is secondary; the primary goal is ensuring the underlying engine maintains consistency under chaotic but reproducible conditions.
Sources: pkg/cardinal/dst.go:1-10
Architecture and Components
The DST system utilizes a harness that wraps a Cardinal World instance and replaces standard infrastructure with test-specific components. It swaps default NATS event handlers with local handlers and uses in-memory synchronous snapshot storage to prioritize reproducibility over latency.
Sources: pkg/cardinal/dst.go:130-155
Key Components
| Component | Description |
|---|---|
dstFixture | Manages the test environment, including the world instance, command type cache, and snapshot storage. |
dstConfig | Holds configurable parameters like total ticks, operation weights, and snapshot rates. |
memSnapshotStorage | An in-memory implementation of the snapshot.Storage interface that validates snapshot integrity. |
fillRandom | A recursive utility that populates command payloads with random data based on reflection. |
Sources: pkg/cardinal/dst.go:94-118, pkg/cardinal/dst.go:121-125, pkg/cardinal/dst.go:210-215
DST Execution Flow
The following diagram illustrates how RunDST orchestrates the simulation loop, selecting between ticking, enqueuing commands, or performing engine resets.
Sources: pkg/cardinal/dst.go:48-91
Randomized Command Generation
The system uses reflection to introspect registered command types and generate valid payloads with randomized data. The fillRandom function identifies field types and assigns random values, specifically targeting EntityID fields to reference live entities currently existing in the world.
Sources: pkg/cardinal/dst.go:179-191, pkg/cardinal/dst.go:210-250
Structural Invariant Checking
After every tick, the DST harness executes ecs.CheckWorld to validate the internal state of the Entity Component System (ECS). This process ensures that the engine maintains complex relationships between archetypes, entities, and component columns.
Sources: pkg/cardinal/dst.go:65-68, pkg/cardinal/internal/ecs/world.go:102-105
Validated Invariants
The engine performs the following checks:
- Archetype Integrity: Validates that archetype IDs match their array indices and that the void archetype remains empty.
- Column Alignment: Ensures every component column length matches the number of entities in its parent archetype.
- Bijective Mapping: Confirms the mapping between entity IDs and row indices is unique and valid.
- Entity Membership: Asserts that no entity exists in multiple archetypes simultaneously.
- ID Management: Checks that free IDs are not reused by live entities and that all IDs remain within the
nextIDcounter bounds.
Sources: pkg/cardinal/internal/ecs/world.go:106-160
Snapshot and Restore Operations
DST tests the fidelity of the snapshot system by performing randomized SnapshotRestore operations. It uses a memSnapshotStorage which performs deterministic marshalling and unmarshalling to ensure that a world state can survive a wire-format roundtrip without data loss.
Sources: pkg/cardinal/dst.go:81-86, pkg/cardinal/dst.go:275-295
Usage and Implementation
To implement DST in a game shard, you provide a setup function to register systems and any required bootstrap commands.
DST Setup Example
func TestDST(t *testing.T) {
cardinal.RunDST(t, func(w *cardinal.World) {
cardinal.RegisterSystem(w, system.PlayerSetUpdater, cardinal.WithHook(cardinal.PreUpdate))
cardinal.RegisterSystem(w, system.PlayerSpawnSystem)
// register additional systems
}, nil)
}
Sources: pkg/template/multi-shard/dst_game_test.go:11-20
Configuration Options
| Option | Default | Description |
|---|---|---|
-dst.ticks | 1000 | Number of simulation steps to perform. |
SnapshotRate | 1-25 | Randomly determined frequency of snapshot creation. |
OpWeights | Dynamic | Probability weights for tick, command, restart, and restore operations. |
Sources: pkg/cardinal/dst.go:42, pkg/cardinal/dst.go:101-118
Summary
Deterministic Simulation Testing (DST) acts as a rigorous quality assurance layer for the Cardinal engine. By combining randomized command fuzzing with strict ECS structural validation, it ensures that engine operations like state transitions and snapshot restoration remain reliable and reproducible. This allows developers to catch subtle engine bugs that may not surface during standard unit or integration testing.
Sources: pkg/cardinal/dst.go:1-10