Skip to content
Tutorials Center
My learning

Topics

Lesson 45 of 60 ยท Non-functional Testing Concepts

Concurrency and Stability Testing

Learn concurrency and stability testing: race conditions, deadlocks, lost updates, concurrency vs parallelism, how to test for them, and what stability testing checks.

Many defects only appear when two things happen at once. Two customers buy the last item in the same second. Two tabs save the same profile. Two background jobs update one account. Each action works perfectly alone, and the system still ends in a wrong state. These bugs are rare in a quiet test lab, nearly impossible to reproduce by hand, and painful in production. Concurrency testing looks for them on purpose. Its sibling, stability testing, asks whether the system keeps working correctly over time and under stress.

This lesson explains the classic concurrency defects, how to provoke them, and what stability testing covers. It builds on the load ideas in Performance Testing Concepts.

Concurrency versus parallelism

The two words are related but different:

  • Concurrency means several tasks are in progress during the same period, with their steps interleaved. One CPU core can do this by switching quickly between tasks.
  • Parallelism means tasks are literally executing at the same instant on different cores or machines.

Both lead to the same testing problem: the order in which steps of different tasks interleave is not fixed, so a bug may appear in only a small fraction of orderings.

Classic concurrency defects

Race condition

A race condition happens when the result depends on the timing of two or more operations on shared data. The standard example is "check then act":

  1. Request A checks: stock = 1, so I can sell.
  2. Request B checks: stock = 1, so I can sell.
  3. A sells and sets stock = 0. B sells too and sets stock = 0.

Two customers bought one item. This is also called overselling.

Lost update

Two users read the same record, each changes a different field, and the second save overwrites the first save's change, so one update silently disappears. Fixes include optimistic locking (a version number: the save fails if the record changed) and pessimistic locking (hold a lock while editing).

Deadlock

A deadlock is a standstill: task A holds lock 1 and waits for lock 2, while task B holds lock 2 and waits for lock 1. Neither can continue. In a database, the engine detects it and kills one transaction; in application code, the program may simply hang.

Starvation and livelock

In starvation, one task never gets the resource because others always go first. In livelock, tasks keep reacting to each other without making progress, like two people repeatedly stepping aside in a corridor.

Visibility and ordering issues

Without proper synchronization, one thread may not see another's update, or operations may be reordered. This is typical in multi-threaded code and caching layers.

sequenceDiagram
  participant A as Request A
  participant DB as Stock = 1
  participant B as Request B
  A->>DB: read stock (1)
  B->>DB: read stock (1)
  A->>DB: write stock = 0, sell
  B->>DB: write stock = 0, sell
  Note over A,B: Two sales for one item

The classic oversell: both requests read before either writes.

How to test for concurrency problems

You cannot rely on luck, so you create pressure and check invariants, statements that must always be true ("stock is never negative", "balance equals the sum of transactions").

  • Parallel requests. Fire many identical requests at the same instant (a script or a load tool with a barrier) and check the final state.
  • Two sessions. Manually edit the same record in two browsers and save in both orders.
  • Repeat and vary. Run the scenario hundreds of times with random delays; a failure that appears once in a thousand is still a bug.
  • Double submit. Click the pay button twice, refresh during submit, send a retried request: the system should be idempotent, doing the work once.
  • Inject delays. Slow down one step in a test environment to widen the race window.
  • Inspect locks and logs. Look for deadlock reports, timeouts and retries in database logs.
  • Static tools. Thread-safety analyzers and race detectors (many languages ship one) find some problems without running at scale.

Try it: an oversell race, then a fix

JavaScript runs one step at a time, but the await points let tasks interleave exactly as concurrent requests do. The stand-in below simulates ten buyers racing for five items, first with a broken check-then-act and then with a guarded version.

JS Overselling race condition

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The racy run sells all ten buyers an item from a stock of five, and the stock ends at 4, a nonsensical number. The locked version serialises the critical section, so exactly five sales succeed. In real systems the same fix is a database transaction with a lock, an atomic UPDATE ... WHERE stock > 0, or a version check.

Try it: lost update and optimistic locking

Two editors load the same profile. Without a version check, the second save wipes the first editor's change. With one, the second save is rejected and must retry.

JS Lost update vs optimistic locking

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Stability testing

Stability testing checks that the system continues to behave correctly, and does not degrade, over an extended period or under adverse conditions. It overlaps with soak testing and with reliability testing, but its emphasis is on keeping going.

Typical things it watches:

  • Memory leaks: memory usage grows steadily until the process is killed.
  • Resource leaks: open file handles, database connections, sockets or threads that are never released.
  • Performance drift: response time slowly worsens as caches fill or logs grow.
  • Error accumulation: a low rate of errors that increases over time.
  • Queue and disk growth: backlogs or log files that never shrink.
  • Recovery: after a restart or a failure, the system returns to normal, without manual cleanup.

How to run one: apply a realistic steady load for hours or days (for a long-lived service, up to a week), sample metrics regularly (memory, CPU, connections, error rate, latency percentiles), and look at the trend, not only the average. A flat line is healthy; a rising line is a leak.

Include stress ends too: bursts above normal load, then a return to normal. A stable system recovers its previous numbers.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Two customers buy the last item at the same moment and both orders succeed. What is this defect?

    The outcome depends on the timing of two operations on shared data; both read the stock before either wrote.
  2. 2. Task A waits for a lock held by B while B waits for a lock held by A. What is this?

    A circular wait where no task can proceed is a deadlock.
  3. 3. Which approach helps prevent lost updates when two users edit the same record?

    A version check rejects a save based on stale data, so one edit does not silently overwrite another.
  4. 4. A stability test shows memory rising steadily over eight hours of constant load. What does it suggest?

    Under steady load, memory should plateau. A constant upward trend usually means something is not being released.

Summary

  • Concurrency means interleaved tasks; parallelism means simultaneous execution. Both create timing-dependent bugs.
  • Classic defects: race conditions (overselling), lost updates, deadlocks, starvation, livelock.
  • Test by firing parallel requests, using two sessions, adding delays, repeating many times and checking invariants; design for idempotent operations.
  • Stability testing runs a system for a long time and watches trends in memory, connections, latency and errors.

Next up: Penetration Testing, SAST, DAST and IAST.