Skip to content

Testing a system: unit, integration, contract, load, chaos

What unit, integration, contract, load and chaos tests each catch, and how to break a part of a system on purpose in a safe way.

  • Lesson 96 of 125
  • 12 min read
  • Depth
  • Free to read, no account
On this page
  1. Why interviewers ask this
  2. The idea in plain words
  3. Pause and predict
  4. Worked example
  5. Trade-offs
  6. Common misconceptions
  7. How to say it in the interview
  8. Check your understanding
  9. Answers
  10. Sources

What you need first

This lesson uses ideas from these earlier lessons. Read them first if any idea is new to you.

01Why interviewers ask this

The interviewer asks: "How will you know it works before users find out?" A weak answer is "just test it". A strong answer names each test and what it catches. At SDE-3 level, add a test that breaks a part on purpose.

02The idea in plain words

Think of a bridge. Engineers test each bolt, then how parts fit, then trucks, then a broken cable.

  • A unit test runs one small piece of code, a unit, such as a function. It catches wrong logic.
  • An integration test runs your code with the real parts it uses, such as a real database. A fake is a stand-in that returns fixed answers. It can hide a mismatch, such as a wrong column name. A real part shows it.
  • A contract test checks the agreement between a caller and a provider, the service it calls (lesson 11). The caller lists what it reads, such as field names. The provider's tests must pass against that list, so a rename fails before release.
  • A load test sends many requests and measures p99 and errors (lessons 4 and 91) against a goal, your target. A breakpoint test raises the load until the goal breaks. A soak test runs for hours and finds slow leaks of memory.
  • A chaos test breaks a part on purpose and checks that users stay safe.

Write many unit tests and fewer of the others (the test pyramid).

Servers needed

Choose a target CPU share: the most CPU you allow at peak, such as 80%. Servers needed = load / (rate per server x target CPU share). If the goal must hold when one server stops, add one more server, so the rest carry its load.

How to break a part on purpose

A fault is something going wrong. You can stop a server, slow a call, make a call return errors such as 503 (lesson 54), cut the network, or fill a disk. A proxy, a server that passes calls between two others, can add delay or errors with no code change. Then:

  1. Write the steady state: p99 and error rate (lesson 91).
  2. Write a hypothesis: "If one server stops, errors stay under 1%."
  3. Limit the blast radius: how many users can be hurt. Start in staging, a copy of the system for tests. Then try a small share of live traffic (our advice).
  4. Set a stop line, for example errors above 2%, and undo the fault if it is crossed.
  5. Inject the fault, compare, and fix what you find.

Stuck on something? The AI tutor has read this lesson. Ask it with a free account

03Pause and predict

Pause. Guess first.

One server handles 125 requests per second at 100% CPU. A sale brings 600 requests per second. You want CPU at 80% or less. How many servers do you need? How many if one may stop?

Write your guess before you read on. The answer is in the worked example.

04Worked example

The checkout of lesson 91. Assumptions: 125 requests per second per server at 100% CPU, 4 servers, peak 200, sale at 3 times the peak. Goal: p99 under 1,000 ms, CPU at most 80%. A coupon gives 10% off, at most Rs 50.

  1. Unit. An order of Rs 800: 10% is Rs 80, the cap is Rs 50, so the user pays 800 - 50 = Rs 750. Code with no cap gives 720, and the test fails.
  2. Integration. The code saves amount, but the real column is total. The fake accepted it. The real database fails.
  3. Contract. The payment team renames status to state. Checkout's list says status, so the provider's run fails.
  4. Load. Capacity is 4 x 125 = 500. Assumption: a test shows p99 700 ms at 400 requests per second (80% CPU), and over 1,000 ms at 600. The sale is 3 x 200 = 600. Then 600 - 500 = 100 requests have no room: 100 / 600 = 16.7%. Servers needed = 600 / (125 x 0.8) = 600 / 100 = 6.
  5. Chaos. Hypothesis: with 6 servers and one stopped, the goal holds. Five remain and take 600 / 5 = 120 each, which is 96% CPU. The hypothesis fails. With 7 servers, six remain: 600 / 6 = 100 each, which is 80%. So the answer to the predict box is 6 servers, and 7 if one may stop.

05Trade-offs

Layered tests solve faults found late. They cost time to write and run. Load and chaos tests solve surprises on sale day. They cost a test copy of the system and some risk. I would repeat them after big changes.

06Common misconceptions

"All unit tests pass, so the system works." The fakes hid the wrong column name and the renamed field.

"Chaos testing means breaking production at random." It is an experiment with a hypothesis and a stop line.

07How to say it in the interview

Say it like this

I test in layers: many unit tests, fewer integration tests with a real database, and contract tests on each service call. Before a sale I load test at 3 times the peak and stop one server on purpose, to check the goal holds.

08Check your understanding

  1. What does a contract test catch that a unit test with fakes cannot? Name the five steps of a chaos test.
  2. One server handles 100 requests per second at 100% CPU. Peak is 450. CPU must stay at 75% or less. How many servers do you need, and how many if one may stop?
  3. All unit tests pass, but checkout fails after release. The payment service now returns state, but the unit tests use a fake that returns status. What was missing?

09Answers

Show the answers
  1. A change in what a service sends, such as a rename. Steps: steady state, hypothesis, blast radius, stop line, inject.
  2. 450 / 75 = 6 servers. With 6 and one stopped, 450 / 5 = 90%, too high. With 7, six remain: 450 / 6 = 75%.
  3. A contract test. The provider's tests must pass against checkout's status list.

10Sources

Try it now, no sign-up

Ask the AI tutor about this lesson

The tutor has read this lesson. Ask in your own words, any time. Free account, 20 questions a day.

Reading never needs an account. The account is for the tutor, the quiz, your notes and your progress.

All lessons in Module 15: Services and running it