Test cancelling preview
This commit is contained in:
parent
05644456e0
commit
c066843f9b
2 changed files with 35 additions and 3 deletions
|
|
@ -11,6 +11,9 @@ def main():
|
||||||
# This is used to check we catch errors correctly.
|
# This is used to check we catch errors correctly.
|
||||||
if arg == "ERROR":
|
if arg == "ERROR":
|
||||||
raise RuntimeError("I was told to do this.")
|
raise RuntimeError("I was told to do this.")
|
||||||
|
if arg == "HANG":
|
||||||
|
# Rather than hang, just sleep for 10s:
|
||||||
|
time.sleep(10)
|
||||||
print(arg, flush=True)
|
print(arg, flush=True)
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import os
|
||||||
from copy import copy
|
from copy import copy
|
||||||
import uuid
|
import uuid
|
||||||
import threading
|
import threading
|
||||||
from time import sleep
|
import time
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
@ -200,6 +200,7 @@ def test_preview_stitching_command(caplog, mocker):
|
||||||
mocker.patch("openflexure_microscope_server.stitching.STITCHING_CMD", mock_cmd)
|
mocker.patch("openflexure_microscope_server.stitching.STITCHING_CMD", mock_cmd)
|
||||||
|
|
||||||
with caplog.at_level(logging.INFO):
|
with caplog.at_level(logging.INFO):
|
||||||
|
cancel_hook = lt.deps.CancelHook(id=uuid.uuid4())
|
||||||
stitcher = PreviewStitcher(FAKE_DIR, overlap=0.1, correlation_resize=0.5)
|
stitcher = PreviewStitcher(FAKE_DIR, overlap=0.1, correlation_resize=0.5)
|
||||||
stitcher.start()
|
stitcher.start()
|
||||||
# Should take a second or so to run so will still be running
|
# Should take a second or so to run so will still be running
|
||||||
|
|
@ -208,12 +209,40 @@ def test_preview_stitching_command(caplog, mocker):
|
||||||
with pytest.raises(RuntimeError):
|
with pytest.raises(RuntimeError):
|
||||||
stitcher.start()
|
stitcher.start()
|
||||||
# Wait for it to complete
|
# Wait for it to complete
|
||||||
stitcher.wait()
|
stitcher.wait(cancel_hook)
|
||||||
# It is now not running
|
# It is now not running
|
||||||
assert not stitcher.running
|
assert not stitcher.running
|
||||||
assert len(caplog.records) == 0
|
assert len(caplog.records) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_preview_stitching_cancelled(caplog, mocker):
|
||||||
|
"""Check that preview stitch can be cancelled."""
|
||||||
|
mock_cmd = f"python {MOCK_STITCHER}"
|
||||||
|
|
||||||
|
mocker.patch("openflexure_microscope_server.stitching.STITCHING_CMD", mock_cmd)
|
||||||
|
|
||||||
|
with caplog.at_level(logging.INFO):
|
||||||
|
stitcher = PreviewStitcher(FAKE_DIR, overlap=0.1, correlation_resize=0.5)
|
||||||
|
# Send in the argument HANG to mock-stitch and it just hang for 10s
|
||||||
|
stitcher._extra_args = ["HANG"]
|
||||||
|
cancel_hook = lt.deps.CancelHook(id=uuid.uuid4())
|
||||||
|
t_start = time.time()
|
||||||
|
# Start stitching
|
||||||
|
stitcher.start()
|
||||||
|
# Run wait() in a thread.
|
||||||
|
thread = threading.Thread(target=stitcher.wait, kwargs={"cancel": cancel_hook})
|
||||||
|
thread.start()
|
||||||
|
# Sleep long enough for at least 1 log.
|
||||||
|
time.sleep(0.5)
|
||||||
|
# use set() to cancel!
|
||||||
|
cancel_hook.set()
|
||||||
|
thread.join()
|
||||||
|
# If it wasn't cancelled it would hang for 10 s. Here we check the cancel killed
|
||||||
|
# it within 2s.
|
||||||
|
assert t_start - time.time() < 2
|
||||||
|
assert len(caplog.records) == 0
|
||||||
|
|
||||||
|
|
||||||
def test_final_stitching_command(caplog, mocker):
|
def test_final_stitching_command(caplog, mocker):
|
||||||
"""Check the final stitch runs until completion, and print statements are logged."""
|
"""Check the final stitch runs until completion, and print statements are logged."""
|
||||||
mock_cmd = f"python {MOCK_STITCHER}"
|
mock_cmd = f"python {MOCK_STITCHER}"
|
||||||
|
|
@ -252,7 +281,7 @@ def test_final_stitching_command_cancelled(caplog, mocker):
|
||||||
thread = threading.Thread(target=stitcher.run, kwargs={"cancel": cancel_hook})
|
thread = threading.Thread(target=stitcher.run, kwargs={"cancel": cancel_hook})
|
||||||
thread.start()
|
thread.start()
|
||||||
# Sleep long enough for at least 1 log.
|
# Sleep long enough for at least 1 log.
|
||||||
sleep(0.5)
|
time.sleep(0.5)
|
||||||
# use set() to cancel!
|
# use set() to cancel!
|
||||||
cancel_hook.set()
|
cancel_hook.set()
|
||||||
thread.join()
|
thread.join()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue