1.6 KiB
Prompt
User asked to extend tests/test_resource_cache.py with a test that reproduces the edge case fixed in commit 02b2ef18 (per-key locking+user tracking in maybe_open_context()). The bug was originally triggered in piker’s brokerd.kraken backend where the same acm_func was called with different kwargs, and the old global _Cache.users counter caused:
- teardown skipped for one
ctx_keybc another key’s users kept the global count > 0 - re-entry hitting
assert not resources.get(ctx_key)during the teardown window
User requested a test that would fail under the old code and pass with the fix.
Response summary
Designed and implemented test_per_ctx_key_resource_lifecycle which verifies per-ctx_key resource isolation by:
- Holding resource
'a'open in a bg task - Opening+closing resource
'b'(sameacm_func, different kwargs) while'a'is still alive - Re-opening
'b'and asserting cache MISS — proving'b'was torn down independently despite'a'keeping its own user count > 0
With the old global counter, phase 3 would produce a stale cache HIT (leaked resource) or crash on the assert.
Also added a trivial acm_with_resource(resource_id) ACM helper at module level.
Files changed
tests/test_resource_cache.py— addacm_with_resourceACM +test_per_ctx_key_resource_lifecycletest fn
Human edits
None — committed as generated (pending user review).