Skip to content

Commit

Permalink
Add infinite loop detection to test
Browse files Browse the repository at this point in the history
From playing around deliberately introducing bugs for Hypothesis to
find, infinte looping is another failure mode its possible to introduce.
But without an explicit check for this it just results in the test
hanging which isn't that helpful.
  • Loading branch information
evansd committed Jan 17, 2025
1 parent 0e9a4cc commit 4a1d53f
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tests/unit/utils/test_sqlalchemy_exec_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ class FakeConnection:
def __init__(self, table_data):
self.table_data = list(table_data)
self.random = random.Random(202412190902)
self.last_query = None
self.duplicate_query_count = 0

def execute(self, query):
self.call_count += 1
compiled = query.compile()
sql = str(compiled).replace("\n", "").strip()
params = compiled.params

if self.last_query == (sql, params): # pragma: no branch
self.duplicate_query_count += 1
if self.duplicate_query_count > 10:
raise RuntimeError(
"Repeated duplicate queries: stuck in infinite loop?"
)
else:
self.last_query = (sql, params)
self.duplicate_query_count = 0

if sql == "SELECT t.key, t.value FROM t ORDER BY t.key LIMIT :param_1":
limit = params["param_1"]
return self.sorted_data()[:limit]
Expand Down

0 comments on commit 4a1d53f

Please sign in to comment.