QA Interview Hub · Knowledge base · Quizzes · Українська

API › API design: resources, pagination, versioning, GraphQL

Long-running operations: 202, a status resource, webhooks and timeouts

A long operation is not held on one connection: the server answers 202 Accepted with a status link, and the client polls it until the state is final. A 202 status does not mean success, the job's error is carried by the status body. A test checks the state machine, the timeout of a hung job, a repeated start through Idempotency-Key, and for webhooks the signature and duplicates.

A report export takes 40 seconds. The client sends POST /exports and waits on the same connection, while a gateway in front of the service cuts it at 30 seconds: the client sees 504, repeats the request, and now the server has two exports instead of one, and the first may still finish. No "201 and the right JSON" test shows this. An operation that takes longer than a reasonable response time is designed differently: the server answers "accepted" quickly, and the client collects the result separately.

Mechanism. The contract has three parts.

  • 202 Accepted at the start: the request is accepted for processing, but processing is not finished. The status says nothing about success; the response gives a link to a resource to follow (Location) and, if needed, Retry-After.
  • A status resource: an ordinary resource with a state field that the client reads with GET. The status request itself succeeded, so 200 comes even when the operation ended in an error: the error is described in the body. An unknown id gives 404, and after the retention period also 404 or 410, as the contract says.
  • A finite state machine, for example queued → running → succeeded | failed | cancelled. Final states do not change, and the state does not go backwards (running → queued).
POST /exports HTTP/1.1
Idempotency-Key: 7c1f0e52-4b6a-4a46-9d0c-5a5b6e1d2f10

HTTP/1.1 202 Accepted
Location: /exports/9f2/status
Retry-After: 5

{"id": "9f2", "state": "queued"}

GET /exports/9f2/status  →  200 {"state": "running", "progress": 40}
GET /exports/9f2/status  →  200 {"state": "succeeded", "resultUrl": "/exports/9f2/file"}
GET /exports/9f2/status  →  200 {"state": "failed", "error": {"code": "source_unavailable"}}

The result is given either in a resultUrl field or by a 303 See Other redirect from the status resource to the finished resource: both exist, and the contract picks the shape.

Why it works this way. A connection must not live as long as the work lasts: gateways, load balancers and mobile networks drop it arbitrarily, and a drop does not cancel work already started on the server. By separating "accepted" from "finished", the service returns control at once, and the state becomes data that can be re-read, repeated and checked.

Example: a test that does not depend on time. A fixed pause (sleep(10)) is either too slow or too short; endless polling hangs the pipeline. Poll with an upper bound and a growing interval:

const start = await request.post('/exports', {
  headers: { 'Idempotency-Key': key },
  data: { period: '2026-10' },
});
expect(start.status()).toBe(202);
const statusUrl = start.headers()['location'];

await expect
  .poll(async () => (await (await request.get(statusUrl)).json()).state, {
    timeout: 60_000,
    intervals: [1_000, 2_000, 5_000],
  })
  .toBe('succeeded');

Separately check that the POST itself returns 202 fast (seconds, not the duration of the work) even when the worker is slow; otherwise it is a synchronous operation with a different status instead of an asynchronous one.

What to test.

  • Timeouts. The client's polling limit ends with a clear test error. The server's: a hung operation (simulated with a slowed worker or a small timeout in the configuration) is moved by the service to failed with a reason, not left in running forever. The same after a worker restart in the middle of the work: the job is neither lost nor stuck.
  • Repeats. A repeated POST with the same Idempotency-Key gives the same id and one operation (and with a different body under the same key the contract requires an error). A side effect (an email, a charge) happens once despite the worker's retries. Polling is a safe GET, repeating it changes nothing.
  • Final states. Each state moves only to the allowed ones and the others are forbidden; after succeeded or failed the state does not change; cancelling (DELETE or POST .../cancel) during running works, and for an already finished operation gives 409 or another value from the contract. A failed status carries the error in a stable shape, not a stack trace.
  • Access. Only the owner sees the status and result: someone else's id gives 404 or 403 by policy (BOLA), not the state of another person's operation.

Polling or a webhook. Polling is simple: the client sets the pace and the server knows nothing about it, but it spends requests and adds delay up to the interval. A webhook notifies at once, but needs a publicly reachable receiver and has its own defects: delivery at least once, so duplicates; order is not guaranteed, so a late running event can arrive after succeeded; the receiver is temporarily down, so retries with backoff are needed. So the receiver handles an event idempotently (by event id), verifies the signature and, when in doubt, re-reads the state from the status resource instead of trusting the order of events. On the test side check the signature (a forged event is rejected), redelivery after a receiver error, a duplicate event and an out-of-order event. For a UI a third option can help: SSE or WebSocket, but the status resource stays the source of truth.

Common mistakes.

  • Treating 202 as success and not waiting for the end: the test is green, while the export breaks after the response.
  • A pause instead of polling: the test is slow or unstable depending on the load of the environment.
  • Polling with no upper bound: a worker failure hangs the whole pipeline.
  • Not checking the terminal failed state: the test waits only for succeeded and fails on a timeout with no reason instead of showing the error from the status resource.
  • Not checking a repeated start: a double click or a retry after 504 creates a duplicate.

Check yourself

The status resource returns 200 and {"state":"failed"}. Is that an API defect or normal?

Answer

Normal if the contract says so: the status GET succeeded, and the error is carried by the operation's state. A defect would be a 5xx on the status request itself, or failed with no reason in the body.

The client got 504 on POST and repeated it. What must the server guarantee and what does the test check?

Answer

A repeat with the same Idempotency-Key does not create a second operation and returns the same one (the same id). The test sends the request twice and checks one id and one operation in the list.

In an interview. Junior: a long operation is not held on one request: the server answers 202 with a link to a status, and the client polls it until the state is final. Senior: I describe the state machine and check forbidden transitions, the server-side timeout of a hung operation, a repeated start through Idempotency-Key and the final states. Polling with a limit and a growing interval beats a pause. A webhook gives speed, but delivery is at least once and the order is arbitrary, so the receiver is idempotent, verifies the signature and re-reads the state.

More articles in this chapter