Skip to content

Commit

Permalink
Mock fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindeide committed Jan 18, 2024
1 parent 7ee5cc0 commit 41674a9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/ert/gui/simulation/run_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ def killJobs(self):
if kill_job == QMessageBox.Yes:
# Normally this slot would be invoked by the signal/slot system,
# but the worker is busy tracking the evaluation.
self._tracker.request_termination()
self._worker_thread.quit()
self._worker_thread.wait()
# self._tracker.request_termination()
# self._worker_thread.quit()
# self._worker_thread.wait()
self._on_finished()
self.finished.emit(-1)
return kill_job
Expand Down
79 changes: 43 additions & 36 deletions tests/unit_tests/gui/simulation/test_run_dialog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from pathlib import Path
from unittest.mock import Mock, patch
from unittest.mock import MagicMock, Mock, patch

import pytest
from pytestqt.qtbot import QtBot
Expand All @@ -25,31 +25,37 @@
from tests.unit_tests.gui.simulation.test_run_path_dialog import handle_run_path_dialog


def test_success(runmodel, qtbot: QtBot, mock_tracker):
class RunModelMock(MagicMock):
def add_send_event_callback(self, callback):
self.callback = callback


def test_success(qtbot: QtBot):
notifier = Mock()
widget = RunDialog("poly.ert", runmodel, notifier)
run_model_mock = RunModelMock()

widget = RunDialog("mock.ert", run_model_mock, notifier)
widget.show()
qtbot.addWidget(widget)

with patch("ert.gui.simulation.run_dialog.EvaluatorTracker") as tracker:
tracker.return_value = mock_tracker([EndEvent(failed=False, failed_msg="")])
widget.startSimulation()
widget.startSimulation()
run_model_mock.callback(EndEvent(failed=False, failed_msg=""))

with qtbot.waitExposed(widget, timeout=30000):
qtbot.waitUntil(lambda: widget._total_progress_bar.value() == 100)
qtbot.waitUntil(widget.done_button.isVisible, timeout=100)
assert widget.done_button.text() == "Done"


def test_kill_simulations(runmodel, qtbot: QtBot, mock_tracker):
def test_kill_simulations(qtbot: QtBot):
notifier = Mock()
widget = RunDialog("poly.ert", runmodel, notifier)
run_model_mock = RunModelMock()
widget = RunDialog("mock.ert", run_model_mock, notifier)
widget.show()
qtbot.addWidget(widget)

with patch("ert.gui.simulation.run_dialog.EvaluatorTracker") as tracker:
tracker.return_value = mock_tracker([EndEvent(failed=False, failed_msg="")])
widget.startSimulation()
widget.startSimulation()
run_model_mock.callback(EndEvent(failed=False, failed_msg=""))

with qtbot.waitSignal(widget.finished, timeout=30000):

Expand Down Expand Up @@ -78,37 +84,38 @@ def handle_dialog():
widget.killJobs()


def test_large_snapshot(
runmodel, large_snapshot, qtbot: QtBot, mock_tracker, timeout_per_iter=5000
):
def test_large_snapshot(large_snapshot, qtbot: QtBot, timeout_per_iter=5000):
notifier = Mock()
widget = RunDialog("poly.ert", runmodel, notifier)
run_model_mock = RunModelMock()
widget = RunDialog("mock.ert", run_model_mock, notifier)
widget.show()
qtbot.addWidget(widget)

with patch("ert.gui.simulation.run_dialog.EvaluatorTracker") as tracker:
iter_0 = FullSnapshotEvent(
events = [
FullSnapshotEvent(
snapshot=large_snapshot,
phase_name="Foo",
current_phase=0,
total_phases=1,
progress=0.5,
iteration=0,
indeterminate=False,
)
iter_1 = FullSnapshotEvent(
),
FullSnapshotEvent(
snapshot=large_snapshot,
phase_name="Foo",
current_phase=0,
total_phases=1,
progress=0.5,
iteration=1,
indeterminate=False,
)
tracker.return_value = mock_tracker(
[iter_0, iter_1, EndEvent(failed=False, failed_msg="")]
)
widget.startSimulation()
),
EndEvent(failed=False, failed_msg=""),
]

widget.startSimulation()
for event in events:
run_model_mock.callback(event)

with qtbot.waitExposed(widget, timeout=timeout_per_iter * 6):
qtbot.waitUntil(
Expand Down Expand Up @@ -317,15 +324,16 @@ def test_large_snapshot(
),
],
)
def test_run_dialog(events, tab_widget_count, runmodel, qtbot: QtBot):
def test_run_dialog(events, tab_widget_count, qtbot: QtBot):
notifier = Mock()
widget = RunDialog("poly.ert", runmodel, notifier)
run_model_mock = RunModelMock()
widget = RunDialog("mock.ert", run_model_mock, notifier)
widget.show()
qtbot.addWidget(widget)

with patch("ert.gui.simulation.run_dialog.EvaluatorTracker") as tracker:
tracker.return_value = mock_tracker(events)
widget.startSimulation()
widget.startSimulation()
for event in events:
run_model_mock.callback(event)

with qtbot.waitExposed(widget, timeout=30000):
qtbot.mouseClick(widget.show_details_button, Qt.LeftButton)
Expand Down Expand Up @@ -476,17 +484,16 @@ def handle_dialog():
),
],
)
def test_run_dialog_memory_usage_showing(
events, tab_widget_count, runmodel, qtbot: QtBot, mock_tracker
):
def test_run_dialog_memory_usage_showing(events, tab_widget_count, qtbot: QtBot):
notifier = Mock()
widget = RunDialog("poly.ert", runmodel, notifier)
run_model_mock = RunModelMock()
widget = RunDialog("poly.ert", run_model_mock, notifier)
widget.show()
qtbot.addWidget(widget)

with patch("ert.gui.simulation.run_dialog.EvaluatorTracker") as tracker:
tracker.return_value = mock_tracker(events)
widget.startSimulation()
widget.startSimulation()
for event in events:
run_model_mock.callback(event)

with qtbot.waitExposed(widget, timeout=30000):
qtbot.mouseClick(widget.show_details_button, Qt.LeftButton)
Expand Down

0 comments on commit 41674a9

Please sign in to comment.