HomeRoadmapsSynthetic Day 4
Synthetic Test Data Track Β· Day 4 Β· 20 min

Stateful Microservice Mocking & State Machines
Synthetic Test Data Sprint β€” Day 4

Choreograph complex entity lifecycles across distributed microservices (Order, Inventory, Payment, Shipping) with state transition rules and referential integrity.

πŸ”„ Choreography: Distributed Entity State MachineπŸ›‘οΈ Synthetic Data & Privacy Quality Architect
State MachinesMicroservice MocksSaga PatternReferential IntegrityAudit History
About this lab

Stateless mocks fail to capture reality when services depend on distributed state machines (e.g. an order cannot be fulfilled before payment succeeds, and payment cannot proceed without inventory reservation). In this lab, you build a multi-service choreography mock engine with transition guards and audit logs.

πŸ“¦Sample Codebase & Working Solution (Day 4)Available Now
View on GitHub

The official reference codebase for Day 4 is hosted in the repository. Clone the repository and navigate to day-4-stateful-microservice-mocks to run the working solution.

Git Clone β€” Working Solution
git clone https://github.com/777abhi/synthetic-test-data-roadmap.git
cd synthetic-test-data-roadmap/day-4-stateful-microservice-mocks
🏭Production Context & Enterprise Implementation
πŸ’‘ Why We Are Doing This: Static JSON mocks allow impossible business state combinations (e.g. refunded before charged). A finite state machine enforces real-world business constraints across mock services.
🏒 Real-World Production Usecase: Testing checkout saga compensation workflows when downstream payment declines or warehouse inventory runs out.
βš™οΈ How Implemented in Production: Finite State Machine transition map and OrderLifecycleMesh in synthlab/state_machine_mock.py.
Phase 1

Inspect Stateful Lifecycle Mocks

Review the OrderState enum, valid transition matrix, and cross-service mesh.

Step 4.1: View State Machine Engine
Terminal β€” View State Machine
cat synthlab/state_machine_mock.py | head -45
Step 4.2: Stateful Mock Unit Tests
test_day4.py
import unittest
from synthlab.state_machine_mock import EntityStateMachine, OrderState, OrderLifecycleMesh

class TestDay4StatefulMocks(unittest.TestCase):
    def test_valid_state_transitions(self):
        sm = EntityStateMachine(entity_id="ord_100")
        sm.transition_to(OrderState.INVENTORY_RESERVED, actor="inventory-service")
        sm.transition_to(OrderState.PAYMENT_PENDING, actor="payment-service")
        sm.transition_to(OrderState.PAID, actor="payment-service")
        sm.transition_to(OrderState.FULFILLED, actor="fulfillment-service")
        self.assertEqual(sm.current_state, OrderState.FULFILLED)

    def test_illegal_state_transition_raises_error(self):
        sm = EntityStateMachine(entity_id="ord_200")
        # Direct CREATED -> FULFILLED is illegal
        with self.assertRaises(ValueError):
            sm.transition_to(OrderState.FULFILLED, actor="bad-actor")
Phase 2

Execute Day 4 Tests

Run the stateful lifecycle mock test suite.

Step 4.3: Run Test Suite
Terminal β€” Run Day 4 Tests
npm run test:day4
Day 4 Key Learnings
  • Engineered a deterministic finite state machine (FSM) enforcing valid enterprise entity transitions.
  • Coordinated simulated microservice actors (Order, Inventory, Payment, Shipping) with referential checks.
  • Recorded immutable audit logs capturing timestamps, actor services, and transition metadata.

🧠 Knowledge Check

Why should test mocks implement an explicit finite state machine (FSM)?