Skip to content

Commit

Permalink
Merge pull request #7042 from kozlovsky/fix_gui_process_watcher_tests
Browse files Browse the repository at this point in the history
Fix GuiProcessWatcher tests
  • Loading branch information
kozlovsky authored Sep 9, 2022
2 parents 10c7154 + bb4fab0 commit 7808c05
Showing 1 changed file with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
GUI_PID_ENV_KEY


@pytest.fixture(name='watcher')
async def watcher_fixture():
gui_process = Mock()
gui_process.is_running.return_value = True
gui_process.status.return_value = psutil.STATUS_RUNNING
shutdown_callback = Mock()
watcher = GuiProcessWatcher(gui_process, shutdown_callback)
watcher.start()
yield watcher
await watcher.stop()


def test_get_gui_pid(caplog):
with patch.dict(os.environ, {GUI_PID_ENV_KEY: ''}):
assert GuiProcessWatcher.get_gui_pid() is None
Expand Down Expand Up @@ -40,43 +52,34 @@ def test_get_gui_process():
GuiProcessWatcher.get_gui_process()


def test_check_gui_process(caplog):
gui_process = Mock()
gui_process.is_running.return_value = True
gui_process.status.return_value = psutil.STATUS_RUNNING

# GUI process is working
shutdown_callback = Mock()
watcher = GuiProcessWatcher(gui_process, shutdown_callback)
async def test_check_gui_process_working(caplog, watcher):
caplog.clear()
watcher.check_gui_process()
assert caplog.records[-1].message == 'GUI process checked, it is still working'
assert not shutdown_callback.called
assert not watcher.shutdown_callback.called
assert not watcher.shutdown_callback_called

# GUI process is zombie
gui_process.status.return_value = psutil.STATUS_ZOMBIE
watcher = GuiProcessWatcher(gui_process, shutdown_callback)
async def test_check_gui_process_zombie(caplog, watcher):
watcher.gui_process.status.return_value = psutil.STATUS_ZOMBIE
caplog.clear()
watcher.check_gui_process()
assert caplog.records[-1].message == 'GUI process is not working, initiate Core shutdown'
assert shutdown_callback.called
assert watcher.shutdown_callback.called
assert watcher.shutdown_callback_called

# The process is not running
gui_process.is_running.return_value = False
gui_process.status.reset_mock()
watcher = GuiProcessWatcher(gui_process, shutdown_callback)
async def test_check_gui_process_not_running(caplog, watcher):
watcher.gui_process.is_running.return_value = False
caplog.clear()
watcher.check_gui_process()
assert not gui_process.status.called
assert not watcher.gui_process.status.called
assert caplog.records[-1].message == 'GUI process is not working, initiate Core shutdown'
assert shutdown_callback.called
assert watcher.shutdown_callback.called
assert watcher.shutdown_callback_called

# Calling check_gui_process after shutdown_callback was already called
shutdown_callback.reset_mock()
watcher.shutdown_callback.reset_mock()
caplog.clear()
watcher.check_gui_process()
assert caplog.records[-1].message == 'The shutdown callback was already called; skip checking the GUI process'
assert watcher.shutdown_callback_called
assert not shutdown_callback.called
assert not watcher.shutdown_callback.called

0 comments on commit 7808c05

Please sign in to comment.