Skip to content

test(pyamber): add unit tests for replace_print and AtomicInteger#4795

Merged
aglinxinyuan merged 3 commits into
apache:mainfrom
Yicong-Huang:test-pyamber-util-thin
May 3, 2026
Merged

test(pyamber): add unit tests for replace_print and AtomicInteger#4795
aglinxinyuan merged 3 commits into
apache:mainfrom
Yicong-Huang:test-pyamber-util-thin

Conversation

@Yicong-Huang
Copy link
Copy Markdown
Contributor

What changes were proposed in this PR?

Adds pytest coverage for two thin core utilities:

  • amber/src/main/python/core/util/console_message/replace_print.py — the replace_print context manager that intercepts builtins.print and enqueues ConsoleMessage payloads on a buffer.
  • amber/src/main/python/core/util/thread/atomic.py — the AtomicInteger thread-safe counter.

Any related issues, documentation, discussions?

Closes #4793.

Bug pinned in the spec with comment + xfail-strict for the intended contract (filed separately as a Bug issue): AtomicInteger.get_and_set deadlocks because it holds the non-reentrant lock while invoking the value property, which tries to acquire the same lock again. The pinned test surfaces the deadlock via a thread + timeout (so the suite does not hang), and the xfail-strict companion asserts the intended get_and_set contract — that test will flip to XPASS the moment the deadlock is fixed and force the spec to be updated alongside.

How was this PR tested?

cd amber/src/main/python
ruff check core/util/thread/test_atomic.py core/util/console_message/test_replace_print.py
ruff format --check core/util/thread/test_atomic.py core/util/console_message/test_replace_print.py
python -m pytest core/util/thread/test_atomic.py core/util/console_message/test_replace_print.py

Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Code (claude-opus-4-7)

Closes apache#4793

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 43.67%. Comparing base (5ecf746) to head (75dca07).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #4795      +/-   ##
============================================
+ Coverage     43.62%   43.67%   +0.04%     
  Complexity     2088     2088              
============================================
  Files           957      957              
  Lines         34077    34077              
  Branches       3753     3753              
============================================
+ Hits          14865    14882      +17     
+ Misses        18419    18401      -18     
- Partials        793      794       +1     
Flag Coverage Δ
amber 41.99% <ø> (+<0.01%) ⬆️
python 86.33% <ø> (+0.40%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds pytest coverage for two small pyamber core utilities (AtomicInteger and replace_print) to lock down current behavior and pin a known deadlock bug in AtomicInteger.get_and_set.

Changes:

  • Add single-threaded, concurrency, and bug-pinning tests for core.util.thread.atomic.AtomicInteger (including strict xfail for the intended get_and_set contract).
  • Add lifecycle and payload-shape tests for core.util.console_message.replace_print.replace_print to validate print interception and ConsoleMessage contents.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
amber/src/main/python/core/util/thread/test_atomic.py New unit tests for AtomicInteger, including concurrency check and pinned deadlock behavior for get_and_set.
amber/src/main/python/core/util/console_message/test_replace_print.py New unit tests for replace_print context manager lifecycle and ConsoleMessage buffer payload semantics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread amber/src/main/python/core/util/thread/test_atomic.py
Previously `completed.wait(timeout=0.5)` returning False could be
satisfied by a scheduling delay or by `get_and_set` raising before
ever reaching the lock. Track a `started` event set at the top of
`attempt`, capture any exception, and assert the worker started, is
still alive, and didn't exit due to an error before checking that
`completed` is unset. The pin now actually proves the deadlock path.
@aglinxinyuan aglinxinyuan enabled auto-merge (squash) May 3, 2026 04:16
@aglinxinyuan aglinxinyuan merged commit ced3448 into apache:main May 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add unit tests for pyamber replace_print and AtomicInteger

4 participants