liveBook · Manning

Busy-waiting loop

As explained in Effective Behavior-Driven Development

A pattern for handling an asynchronous result by repeatedly checking for completion until the result appears or a timeout occurs.

When an asynchronous request returns before its result is ready, a busy-waiting loop repeatedly checks for completion until the result appears or a timeout occurs. Also called active waiting, busy looping, or polling, it is a simple way to synchronize automation code with asynchronous resources. section 21.4.1

Use polling with its costs and failure modes in mind:

  • Avoid replacing polling with a fixed sleep: it can make test suites unnecessarily slow. Polling also adds repeated queries; shorter intervals increase that cost, while longer intervals can leave the test waiting after the result is ready. section 21.4.1
  • Set a timeout rather than looping endlessly. If the operation does not produce a result in time, treat it as an error and fail the scenario. section 21.4.1
  • Consider a Completed Event pattern when available; it synchronizes with a completion event and is usually more efficient than polling. section 21.5

Detecting a file update

If a request updates a file, the update might happen before the waiting loop begins. Capture the file’s status before requesting the operation, then compare it with the status during polling. section 21.4.1

How Effective Behavior-Driven Development covers it

How deep it goes
Explains polling, delay trade-offs, missed fast results, and timeout failures without specifying a tool-specific implementation.
Examples
Uses a file update to illustrate capturing resource status before an asynchronous request.
What it assumes
Defines asynchronous requests as calls that may return before their actions have completed.
What you can do after
Apply polling with adjustable intervals, a timeout, and a baseline check for changes that may occur before waiting begins.
How it connects
Compares polling with the Completed Event and Synchronous Testing patterns for handling asynchronous resources.

Also explained in

Compare how 4 books explain busy-waiting loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Behavior-Driven Development. The section links point to the parts of the book it draws on.