wait-without-timeout

A test waits with no timeout.

Group
Tests
Runs
On by default
Level
note
Reads
Tests
Fix
None: how long the wait may take is the author's to say
Defends
Precision: when the test fails, its failure says where to look.

Why

The test waits for a thread, an event, a latch, or a queue with no timeout, so when the code under test never signals, the test hangs instead of failing.

The fix, as Ur suggests it: give the wait a timeout.

For agents

Give every blocking wait in a test a timeout, and fail the test when it runs out.

Example

shop/restock.py
import threading


class Restock:
    def __init__(self, levels):
        self.levels = levels
        self.done = threading.Event()

    def run(self, name, count):
        self.levels[name] = self.levels.get(name, 0) + count
        self.done.set()


def start_restock(levels, name, count):
    restock = Restock(levels)
    thread = threading.Thread(target=restock.run, args=(name, count))
    thread.start()
    return restock, thread
tests/test_restock.py
from shop.restock import start_restock


def test_restock_adds_to_the_level():
    levels = {"kettle": 3}
    restock, thread = start_restock(levels, "kettle", 2)
    thread.join()
    assert levels["kettle"] == 5
What Ur prints
$ ur lint --rule wait-without-timeout
note[wait-without-timeout]: `join` in test `test_restock_adds_to_the_level` waits with no timeout, so a failure hangs the test instead of failing it
  --> ./tests/test_restock.py:7:5 test `test_restock_adds_to_the_level`
  = hint: give the wait a timeout
  = see: TotT: Sleeping != Synchronization, Google Testing Blog (2008-08-21), https://testing.googleblog.com/2008/08/tott-sleeping-synchronization.html
0 clone classes and 1 test finding in 2 files

Try it in the playground →

Source

TotT: Sleeping != Synchronization, Google Testing Blog (2008-08-21).