Commit a2a2d34
committed
test: pin counter drift, restructure at-cap bench, trim docstring
Three review follow-ups on #1718:
**Bench actually measures eviction (Copilot inline at L43).**
``test_cache_add_at_cap_evicts`` previously created an empty cache
and added ``_MAX_CACHE_RECORDS + 1000`` records, so ~91% of the
measured time was the below-cap fill path and only the final 1000
inserts evicted — the CodSpeed number was not representative of the
eviction performance the name implies. Restructure to pre-fill the
cache to the cap *outside* the timed body and consume one fresh
unique record per benchmark iteration. Each iteration measures
exactly one ``_async_add`` + ``_async_evict_oldest`` cycle, with the
cache permanently at the cap. ``_make_records`` gains a ``prefix=``
parameter so the fill records and the overflow pool don't collide on
``name``.
**Defensive test pins counter state (Kōan suggestion #3).**
``test_cache_eviction_empty_heap_returns_without_evicting`` already
asserts the new record lands; add the corresponding
``_total_records == _MAX_CACHE_RECORDS + 1`` assertion so a future
change to the broken-state recovery (refuse the add, clamp the
counter) fails this test explicitly rather than silently shifting
behaviour.
**One-line docstring (Kōan suggestion #2).**
``_maybe_rebuild_heap`` had an 11-line docstring restating the body
and including amortized-cost analysis. Per CLAUDE.md the why lives
at the call site comment in ``_async_add`` (already does); the
function's contract is a single line.
**No log on the empty-heap fall-through (Kōan suggestion #1).**
Kōan suggested an assertion or debug log; the assertion would crash
production on invariant drift, and even a guarded ``log.debug(...)``
adds Python attribute-lookup overhead in the function the cap
overflow path runs through. Drift is already caught loudly by
``test_cache_total_records_invariant_under_mixed_ops``. Comment
explains the deliberate silence.1 parent 052b1e2 commit a2a2d34
3 files changed
Lines changed: 26 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
138 | 141 | | |
139 | 142 | | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 143 | + | |
153 | 144 | | |
154 | 145 | | |
155 | 146 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
38 | 45 | | |
39 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
40 | 50 | | |
41 | 51 | | |
42 | | - | |
43 | | - | |
44 | | - | |
| 52 | + | |
| 53 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
525 | 525 | | |
526 | 526 | | |
527 | 527 | | |
528 | | - | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
529 | 532 | | |
530 | 533 | | |
531 | 534 | | |
532 | 535 | | |
| 536 | + | |
533 | 537 | | |
534 | 538 | | |
535 | 539 | | |
| |||
0 commit comments