Level 3 / Project 14 - Service Simulator¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | Browser |
Estimated time: 60 minutes
Focus¶
- emulate service responses and failures
Why this project exists¶
This project gives you level-appropriate practice in a realistic operations context. Goal: run the baseline, alter behavior, break one assumption, recover safely, and explain the fix.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-3/14-service-simulator
python project.py request --seed 42
python project.py load --count 100 --json
python project.py retry --max-retries 5 --seed 42
pytest -q
Expected terminal output¶
{"status_code": 200, "body": {"message": "OK"}, ...}
Load test: 100 requests
200: 82
429: 5
500: 8
504: 5
10 passed
Expected artifacts¶
- Simulated responses on stdout
- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Add a
--failure-rateflag to override the default error rate. - Add exponential backoff to the
retry_requestfunction (wait 2^attempt ms). - Add a
healthsubcommand that checks if the service is "healthy" (3 consecutive 200s).
Break it (required) — Core¶
- Set
success_rateto 0.0 — doesretry_requestexhaust all retries? - Run
loadwith--count 0— what happens with zero requests? - Create a service with
--seedand verify results are identical each run.
Fix it (required) — Core¶
- Add validation for rate parameters (must sum to <= 1.0).
- Handle the zero-requests edge case in
run_load_test. - Add a
--timeoutflag that limits total retry duration.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- What HTTP status codes mean 200, 429, 500, 504?
- How does
random.Random(seed)make tests deterministic? - What is retry logic and when should you retry vs. fail fast?
- Why use a class (
SimulatedService) instead of standalone functions?
Mastery check¶
You can move on when you can: - simulate service responses with configurable probabilities, - implement retry logic with max attempts, - use seed-based randomness for reproducible tests, - understand HTTP status code categories.
Related Concepts¶
| ← Prev | Home | Next → |
|---|---|---|