Repository navigation
GUI: Resume button resumes a paused plan (run RE.resume() on the main thread) #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jakub Wlodek (jwlodek)
merged 3 commits into
NSLS2:main
from
sligara7:hex-gui-resume-main-thread
Oct 8, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") | ||
| pytest.importorskip("qtpy") | ||
| pytest.importorskip("bluesky_widgets") | ||
|
|
||
| import bluesky.plans as bp # noqa: E402 | ||
| import matplotlib # noqa: E402 | ||
| from bluesky import RunEngine # noqa: E402 | ||
| from bluesky import plan_stubs as bps # noqa: E402 | ||
| from IPython.core.interactiveshell import InteractiveShell # noqa: E402 | ||
| from ophyd.sim import det, motor # noqa: E402 | ||
| from qtpy.QtCore import QTimer # noqa: E402 | ||
| from qtpy.QtWidgets import QApplication # noqa: E402 | ||
|
|
||
| from hextools.gui._ipython import run_in_ipython # noqa: E402, PLC2701 | ||
| from hextools.gui.re_execution_controls import QtReExecutionControls # noqa: E402 | ||
|
|
||
|
|
||
| def _slow_step(detectors, step, pos_cache): | ||
| yield from bps.one_nd_step(detectors, step, pos_cache) | ||
| yield from bps.sleep(0.1) | ||
|
|
||
|
|
||
| # RE.resume() installs a SIGINT handler, which Python allows only on the main | ||
| # thread; resuming from a worker thread raised and the plan stayed paused. | ||
| # PR 94 review: Resume must act on the engine the controls hold, even when the | ||
| # shell's RE is a different one. | ||
| @pytest.mark.parametrize("given", ["shell", "re", "namespace"]) | ||
| def test_resume_button_resumes_a_paused_plan(given): | ||
| # As in the GUI: with a Qt matplotlib backend, RE() keeps Qt events flowing. | ||
| matplotlib.use("qtagg") | ||
| app = QApplication.instance() or QApplication([]) | ||
| shell = InteractiveShell.instance() | ||
| RE = RunEngine({}) | ||
| shell.user_ns.update( | ||
| RE=RE if given == "shell" else RunEngine({}), | ||
| test_RE=RE, | ||
| bp=bp, | ||
| det=det, | ||
| motor=motor, | ||
| _slow_step=_slow_step, | ||
| ) | ||
| if given == "shell": | ||
| controls = QtReExecutionControls(local=True, namespace=shell.user_ns) | ||
| elif given == "re": | ||
| controls = QtReExecutionControls(local=True, re=RE) | ||
| else: | ||
| controls = QtReExecutionControls(local=True, namespace={"RE": RE}) | ||
| seen = [] | ||
|
|
||
| def resume(): | ||
| seen.append(RE.state) | ||
| controls._pb_plan_resume_clicked() | ||
|
|
||
| QTimer.singleShot( | ||
| 0, | ||
| lambda: run_in_ipython( | ||
| "test_RE(bp.scan([det], motor, 0, 1, 10, per_step=_slow_step))" | ||
| ), | ||
| ) | ||
| QTimer.singleShot(300, controls._pb_plan_pause_immediate_clicked) | ||
| QTimer.singleShot(800, resume) | ||
| QTimer.singleShot(5000, app.quit) | ||
| app.exec() | ||
| controls._timer.stop() | ||
|
|
||
| assert seen == ["paused"] | ||
| assert RE.state == "idle", f"plan did not resume: state={RE.state}" | ||
| assert motor.position == pytest.approx(1) | ||
|
|
||
|
|
||
| # PR 94 review: a double-click on Resume must resume once, not queue a second | ||
| # resume() that could run through a later pause or hit an idle engine. | ||
| def test_double_click_on_resume_resumes_once(): | ||
| matplotlib.use("qtagg") | ||
| app = QApplication.instance() or QApplication([]) | ||
| shell = InteractiveShell.instance() | ||
| RE = RunEngine({}) | ||
| shell.user_ns.update(RE=RE, bp=bp, det=det, motor=motor, _slow_step=_slow_step) | ||
| controls = QtReExecutionControls(local=True, namespace=shell.user_ns) | ||
| resumes = [] | ||
| real_resume = RE.resume | ||
|
|
||
| def counting_resume(): | ||
| resumes.append(RE.state) | ||
| return real_resume() | ||
|
|
||
| RE.resume = counting_resume | ||
| enabled_after_click = [] | ||
|
|
||
| def double_click(): | ||
| controls._poll_local_state() | ||
| controls._pb_plan_resume_clicked() | ||
| enabled_after_click.append(controls._pb_plan_resume.isEnabled()) | ||
| controls._pb_plan_resume_clicked() | ||
|
|
||
| QTimer.singleShot( | ||
| 0, | ||
| lambda: run_in_ipython( | ||
| "RE(bp.scan([det], motor, 0, 1, 10, per_step=_slow_step))" | ||
| ), | ||
| ) | ||
| QTimer.singleShot(300, controls._pb_plan_pause_immediate_clicked) | ||
| QTimer.singleShot(800, double_click) | ||
| QTimer.singleShot(5000, app.quit) | ||
| app.exec() | ||
| controls._timer.stop() | ||
|
|
||
| assert resumes == ["paused"], f"resume() called {len(resumes)} times: {resumes}" | ||
| assert enabled_after_click == [False] | ||
| assert RE.state == "idle" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.