Change is_thread_finished() to return True if a thread is missing

This commit is contained in:
Raoul Snyman 2018-01-12 22:24:53 -07:00
parent 83bc19520a
commit 11f528d09c
2 changed files with 11 additions and 14 deletions

View File

@ -93,7 +93,8 @@ def is_thread_finished(thread_name):
""" """
thread_info = Registry().get('application').worker_threads.get(thread_name) thread_info = Registry().get('application').worker_threads.get(thread_name)
if not thread_info: if not thread_info:
raise KeyError('No thread named "{}" exists'.format(thread_name)) # If the thread doesnt exist anymore, it's probably because it is finished
return True
return thread_info['thread'].isFinished() return thread_info['thread'].isFinished()

View File

@ -151,33 +151,29 @@ def test_is_thread_finished(MockRegistry):
""" """
# GIVEN: A mock thread and worker # GIVEN: A mock thread and worker
mocked_thread = MagicMock() mocked_thread = MagicMock()
mocked_thread.isFinished.return_value = True mocked_thread.isFinished.return_value = False
MockRegistry.return_value.get.return_value.worker_threads = {'test': {'thread': mocked_thread}} MockRegistry.return_value.get.return_value.worker_threads = {'test': {'thread': mocked_thread}}
# WHEN: is_thread_finished() is called # WHEN: is_thread_finished() is called
result = is_thread_finished('test') result = is_thread_finished('test')
# THEN: The result should be correct # THEN: The result should be correct
assert result is True, 'is_thread_finished should have returned True' assert result is False, 'is_thread_finished should have returned False'
@patch('openlp.core.threading.Registry') @patch('openlp.core.threading.Registry')
def test_is_thread_finished_mising(MockRegistry): def test_is_thread_finished_missing(MockRegistry):
""" """
Test that calling the is_thread_finished() function raises a KeyError if it does not exist Test that calling the is_thread_finished() function returns True if the thread doesn't exist
""" """
# GIVEN: A mocked thread worker # GIVEN: A mocked thread worker
MockRegistry.return_value.get.return_value.worker_threads = {} MockRegistry.return_value.get.return_value.worker_threads = {}
try:
# WHEN: get_thread_worker() is called # WHEN: get_thread_worker() is called
is_thread_finished('test_thread') result = is_thread_finished('test_thread')
assert False, 'A KeyError should have been raised when calling is_thread_finished'
except KeyError: # THEN: The result should be correct
# THEN: The mocked worker is returned assert result is True, 'is_thread_finished should return True when a thread is missing'
pass
except Exception:
assert False, 'A KeyError should have been raised when calling is_thread_finished'
def test_make_remove_thread(): def test_make_remove_thread():