Skip to content

Commit 3cae2ec

Browse files
author
Dimitar Tasev
authored
Merge pull request #775 from mantidproject/774_remove_free_all_by_default
2 parents b860eec + 811af5a commit 3cae2ec

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

mantidimaging/core/parallel/utility.py

+10
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,22 @@ def free_all_owned_by_this_instance():
3232
sa.delete(arr.name.decode("utf-8"))
3333

3434

35+
def has_other_shared_arrays() -> bool:
36+
return len(sa.list()) > 0
37+
38+
39+
def free_all():
40+
for arr in [array for array in sa.list()]:
41+
sa.delete(arr.name.decode("utf-8"))
42+
43+
3544
def create_shared_name(file_name=None) -> str:
3645
return f"{INSTANCE_PREFIX}-{uuid.uuid4()}{f'-{os.path.basename(file_name)}' if file_name is not None else ''}"
3746

3847

3948
def delete_shared_array(name, silent_failure=False):
4049
try:
50+
LOG.debug(f"Deleting array with name: {name}")
4151
sa.delete(f"shm://{name}")
4252
except FileNotFoundError as e:
4353
if not silent_failure:

mantidimaging/gui/windows/main/test/view_test.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
class MainWindowViewTest(unittest.TestCase):
2424
def setUp(self) -> None:
2525
with mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
26-
self.view = MainWindowView()
26+
with mock.patch("mantidimaging.gui.windows.main.view.has_other_shared_arrays", return_value=False):
27+
self.view = MainWindowView()
2728
self.presenter = mock.MagicMock()
2829
self.view.presenter = self.presenter
2930

@@ -306,3 +307,34 @@ def test_get_images_from_stack_uuid(self):
306307

307308
self.presenter.get_stack_visualiser.assert_called_once_with(uuid)
308309
self.assertEqual(images, return_value)
310+
311+
@mock.patch("mantidimaging.gui.windows.main.view.has_other_shared_arrays")
312+
@mock.patch("mantidimaging.gui.windows.main.view.free_all")
313+
@mock.patch("mantidimaging.gui.windows.main.view.QMessageBox")
314+
def test_ask_user_to_free_data(self, QMessageBox: Mock, free_all: Mock, has_other_shared_arrays: Mock):
315+
has_other_shared_arrays.return_value = True
316+
# makes the clickedButton the same return value mock as the addButton return
317+
QMessageBox.return_value.clickedButton.return_value = QMessageBox.return_value.addButton.return_value
318+
319+
self.view.ask_user_to_free_data()
320+
321+
QMessageBox.return_value.setWindowTitle.assert_called_once()
322+
QMessageBox.return_value.setText.assert_called_once()
323+
self.assertEquals(QMessageBox.return_value.addButton.call_count, 2)
324+
QMessageBox.return_value.exec.assert_called_once()
325+
free_all.assert_called_once()
326+
327+
@mock.patch("mantidimaging.gui.windows.main.view.has_other_shared_arrays")
328+
@mock.patch("mantidimaging.gui.windows.main.view.free_all")
329+
@mock.patch("mantidimaging.gui.windows.main.view.QMessageBox")
330+
def test_ask_user_to_free_data_ignore_pressed(self, QMessageBox: Mock, free_all: Mock,
331+
has_other_shared_arrays: Mock):
332+
has_other_shared_arrays.return_value = True
333+
334+
self.view.ask_user_to_free_data()
335+
336+
QMessageBox.return_value.setWindowTitle.assert_called_once()
337+
QMessageBox.return_value.setText.assert_called_once()
338+
self.assertEquals(QMessageBox.return_value.addButton.call_count, 2)
339+
QMessageBox.return_value.exec.assert_called_once()
340+
free_all.assert_not_called()

mantidimaging/gui/windows/main/view.py

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX - License - Identifier: GPL-3.0-or-later
33

44
from logging import getLogger
5+
from mantidimaging.core.parallel.utility import free_all, has_other_shared_arrays
56
from mantidimaging.core.utility.projection_angle_parser import ProjectionAngleFileParser
67
from typing import Optional
78
from uuid import UUID
@@ -83,6 +84,23 @@ def __init__(self):
8384
if WelcomeScreenPresenter.show_today():
8485
self.show_about()
8586

87+
if has_other_shared_arrays():
88+
self.ask_user_to_free_data()
89+
90+
def ask_user_to_free_data(self):
91+
msg_box = QMessageBox(self)
92+
msg_box.setWindowTitle("Previously loaded data found")
93+
msg_box.setText("This can happen if Mantid Imaging crashes, or there are multiple instances running.\n\n"
94+
"If Mantid Imaging crashed it is recommended to just 'Release all previous data'.\n\n"
95+
"If you have another instance running it is recommended you 'Ignore' "
96+
"otherwise the other instance will be corrupted.")
97+
delete_all = msg_box.addButton("Release all previous data", QMessageBox.ActionRole)
98+
_ = msg_box.addButton(QMessageBox.Ignore)
99+
msg_box.exec()
100+
101+
if msg_box.clickedButton() == delete_all:
102+
free_all()
103+
86104
def setup_shortcuts(self):
87105
self.actionLoad.triggered.connect(self.show_load_dialogue)
88106
self.actionSampleLoadLog.triggered.connect(self.load_sample_log_dialog)

mantidimaging/gui/windows/operations/test/test_view.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class OperationsWindowsViewTest(unittest.TestCase):
1717
def setUp(self):
1818
# mock the view so it has the same methods
1919
with mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
20-
self.main_window = MainWindowView()
20+
with mock.patch("mantidimaging.gui.windows.main.view.has_other_shared_arrays", return_value=False):
21+
self.main_window = MainWindowView()
2122
self.window = FiltersWindowView(self.main_window)
2223

2324
def test_collapse(self):

mantidimaging/gui/windows/stack_visualiser/test/view_test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def tearDown(self) -> None:
3838

3939
def setUp(self):
4040
with mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
41-
self.window = MainWindowView()
41+
with mock.patch("mantidimaging.gui.windows.main.view.has_other_shared_arrays", return_value=False):
42+
self.window = MainWindowView()
4243
self.window.remove_stack = mock.Mock()
4344
self.dock, self.view, self.test_data = self._add_stack_visualiser()
4445

0 commit comments

Comments
 (0)