chasmtest

package
v1.32.1 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecutePureTask

func ExecutePureTask[C chasm.Component, T any](
	ctx context.Context,
	e *Engine,
	component C,
	handler chasm.PureTaskHandler[C, T],
	attrs chasm.TaskAttributes,
	task T,
) (taskDropped bool, err error)

ExecutePureTask validates and executes a pure task atomically via Engine.UpdateComponent. It returns taskDropped set to true if chasm.PureTaskHandler.Validate returns (false, nil), indicating the task is no longer relevant and was not executed.

The component ref is resolved automatically — no separate Engine.ReadComponent call to obtain a ref is needed. Pass the component pointer directly.

This helper ensures that Validate is always exercised alongside Execute, matching the real engine's behavior. Use chasm.MockMutableContext directly when you need to inspect the typed task payloads added to the context during execution.

func ExecuteSideEffectTask

func ExecuteSideEffectTask[C chasm.Component, T any](
	ctx context.Context,
	e *Engine,
	component C,
	handler chasm.SideEffectTaskHandler[C, T],
	attrs chasm.TaskAttributes,
	task T,
) (taskDropped bool, err error)

ExecuteSideEffectTask validates and executes a side effect task. Validation runs via Engine.ReadComponent in read only mode, and if valid, chasm.SideEffectTaskHandler.Execute is called with an engine context so that chasm.UpdateComponent and chasm.ReadComponent inside the handler route through the test engine.

It returns taskDropped set to true if chasm.SideEffectTaskHandler.Validate returns (false, nil), indicating the task is no longer relevant and was not executed.

The component ref is resolved automatically — no separate Engine.ReadComponent call to obtain a ref is needed. Pass the component pointer directly.

Use chasm.MockMutableContext directly when you need to inspect typed task payloads added during execution, since the real engine serializes them into history layer tasks.

Types

type Engine

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

Engine is a lightweight in memory CHASM engine for unit tests. It implements chasm.Engine and supports the full set of conflict and reuse policies, as well as blocking [PollComponent] with [NotifyExecution], matching the behavior of the production engine as closely as possible without persistence or shard logic.

func NewEngine

func NewEngine(
	t *testing.T,
	registry *chasm.Registry,
	opts ...EngineOption,
) *Engine

func (*Engine) DeleteExecution

func (e *Engine) DeleteExecution(
	_ context.Context,
	ref chasm.ComponentRef,
	_ chasm.DeleteExecutionRequest,
) error

func (*Engine) FirePureTasks

func (e *Engine) FirePureTasks(ref chasm.ComponentRef, referenceTime time.Time) (executed int, err error)

FirePureTasks executes persisted pure tasks due by referenceTime and commits.

func (*Engine) NotifyExecution

func (e *Engine) NotifyExecution(key chasm.ExecutionKey)

NotifyExecution wakes up any [PollComponent] callers waiting on the execution.

func (*Engine) PollComponent

func (e *Engine) PollComponent(
	ctx context.Context,
	ref chasm.ComponentRef,
	predicate func(chasm.Context, chasm.Component) (bool, error),
	_ ...chasm.TransitionOption,
) ([]byte, error)

PollComponent waits until the supplied predicate is satisfied when evaluated against the component identified by ref. If the predicate is true immediately it returns without blocking. Otherwise it subscribes to [NotifyExecution] signals and re evaluates after each one, just like the production engine. Returns (nil, nil) if ctx is cancelled, matching the long poll timeout semantics of the production engine where the caller is expected to re-poll.

func (*Engine) ReadComponent

func (e *Engine) ReadComponent(
	ctx context.Context,
	ref chasm.ComponentRef,
	readFn func(chasm.Context, chasm.Component) error,
	_ ...chasm.TransitionOption,
) error

func (*Engine) StartExecution

func (*Engine) Tasks

func (e *Engine) Tasks(ref chasm.ComponentRef) (map[tasks.Category][]tasks.Task, error)

Tasks returns all physical tasks scheduled for the execution identified by ref, grouped by category. Logical tasks accumulate across every Engine.UpdateComponent, Engine.StartExecution, and Engine.UpdateWithStartExecution call on the execution, and convert to physical tasks on CloseTransaction, matching what the real engine would deliver to task processors.

func (*Engine) UpdateComponent

func (e *Engine) UpdateComponent(
	ctx context.Context,
	ref chasm.ComponentRef,
	updateFn func(chasm.MutableContext, chasm.Component) error,
	_ ...chasm.TransitionOption,
) ([]byte, error)

type EngineOption

type EngineOption func(*Engine)

func WithTimeSource

func WithTimeSource(ts clock.TimeSource) EngineOption

WithTimeSource overrides the engine's default time source. The default is a clock.EventTimeSource initialized to time.Now at engine creation, which gives deterministic, frozen time suitable for most unit tests. Pass a *clock.EventTimeSource when tests need to advance time explicitly; the caller holds the reference and calls ts.Update(...) directly.

Jump to

Keyboard shortcuts

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