Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Task 4 — State Machine Validation

Objective

Create a function that validates a sequence of state transitions against a predefined state machine.

The function must:

  • verify every requested transition
  • report invalid transitions
  • preserve the last valid state
  • return the final state after processing

Service States

Use the following states:

created
starting
running
stopping
stopped
failed

Allowed Transitions

The valid transitions are:

created  -> starting

starting -> running
starting -> failed

running  -> stopping
running  -> failed

stopping -> stopped
stopping -> failed

failed   -> starting

stopped  -> starting

Any transition not listed above is invalid.

Transition Model

A transition request contains:

From
To

A possible model is:

type Transition struct {
    From string
    To   string
}

However, a safer API may accept only requested target states and derive From from the current state.

For example:

ProcessTransitions(
    initialState,
    requestedStates,
)

Input

Initial state:

created

Requested states:

[]string{
    "starting",
    "running",
    "stopping",
    "stopped",
    "starting",
    "running",
}

Expected Processing

Start:

created

Transition:

created -> starting

valid.

Next:

starting -> running

valid.

Next:

running -> stopping

valid.

Next:

stopping -> stopped

valid.

Next:

stopped -> starting

valid.

Next:

starting -> running

valid.

Expected Final State

running

Invalid Transition Example

Initial state:

created

Requested sequence:

[]string{
    "running",
    "stopping",
}

The first transition would be:

created -> running

which is invalid.

The implementation must report the failure.

Processing Policy

When a requested transition is invalid:

  1. report the invalid transition
  2. do not update the current state

The next request is evaluated against the last valid state.

For example:

Current state = created

Request:
running

invalid.

Current state remains:

created

If the next request is:

starting

then:

created -> starting

is valid.

Suggested Result Model

type TransitionResult struct {
    Index     int
    From      string
    To        string
    Valid     bool
    Message   string
}

type StateMachineResult struct {
    InitialState string
    FinalState   string
    Transitions  []TransitionResult
}

Unknown State

A state not defined by the state machine is invalid.

For example:

paused

must not be silently accepted.

Same-State Transition

Transitions such as:

running -> running

are invalid unless explicitly listed.

The provided state machine does not define self-transitions.

Failed State

The failed state may recover through:

failed -> starting

This models a restart attempt.

Direct transition:

failed -> running

is not allowed.

Stopped State

A stopped service may restart through:

stopped -> starting

It may not directly transition to:

running

Requirements

The function must:

  1. validate the initial state
  2. validate every target state
  3. validate transitions against the state graph
  4. record every attempted transition
  5. preserve the current state after invalid transitions
  6. return the final valid state
  7. distinguish valid and invalid requests

Additional Test

Initial:

created

Requests:

starting
failed
running
starting
running

Processing:

created -> starting
valid

starting -> failed
valid

failed -> running
invalid

failed -> starting
valid

starting -> running
valid

Final state:

running

Configuration Extension

An advanced implementation may represent allowed transitions as data rather than hard-coded conditions.

For example:

created:
  starting

starting:
  running
  failed

running:
  stopping
  failed

This makes the state machine easier to extend.

Implementation Notes

This task can be modeled as a directed graph where:

states = nodes
allowed transitions = directed edges

The processing algorithm is simple, but correctness depends on maintaining the current state consistently after failures.

Scalionix Docs

Keyboard Shortcuts

Navigate the documentation without leaving the keyboard.
Navigation
Previous subject
←
Next subject
→
Previous subsection
Alt + ↑
Next subsection
Alt + ↓
Interface
Documentation Home
Ctrl + Enter
Search
Alt + Q
Open shortcuts
?
Close dialog
Esc
Scalionix Docs

Search Documentation