Skip to content

Commit

Permalink
cleanup timeout member, add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
walshmm committed Feb 17, 2025
1 parent de1412c commit 788f840
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/snapred/backend/recipe/algorithm/MantidSnapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def __init__(self, parentAlgorithm, name):
self._algorithmQueue = []
self._exportScript = ""
self._export = False
self.timeout = 60 # seconds
self.checkInterval = 0.05 # 50 ms
if self._export:
self._cleanOldExport()

Expand Down Expand Up @@ -146,14 +148,12 @@ def createAlgorithm(self, name):
return alg

def _waitForAlgorithmCompletion(self, name):
timeout = 60 # seconds
checkInterval = 0.05 # 50 ms
currentTimeout = 0
while len(AlgorithmManager.runningInstancesOf(name)) > 0:
if currentTimeout >= timeout:
raise RuntimeError(f"Timeout occured while waiting for instance of {name} to cleanup")
currentTimeout += checkInterval
time.sleep(checkInterval)
if currentTimeout >= self.timeout:
raise TimeoutError(f"Timeout occured while waiting for instance of {name} to cleanup")
currentTimeout += self.checkInterval
time.sleep(self.checkInterval)

def obtainMutex(self, name):
mutex = self._nonReentrantMutexes.get(name)
Expand Down
42 changes: 40 additions & 2 deletions tests/unit/backend/recipe/algorithm/test_MantidSnapper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import unittest
from unittest import mock

import pytest
from mantid.kernel import Direction

from snapred.backend.recipe.algorithm.MantidSnapper import MantidSnapper
from snapred.meta.Callback import callback

PatchRoot: str = "snapred.backend.recipe.algorithm.MantidSnapper.{0}"


class TestMantidSnapper(unittest.TestCase):
def setUp(self):
Expand All @@ -17,19 +20,54 @@ def setUp(self):
self.fakeFunction.getProperties.return_value = [self.fakeOutput]
self.fakeFunction.getProperty.return_value = self.fakeOutput

@mock.patch("snapred.backend.recipe.algorithm.MantidSnapper.AlgorithmManager")
@mock.patch(PatchRoot.format("AlgorithmManager"))
def test_snapper_fake_algo(self, mock_AlgorithmManager):
mock_AlgorithmManager.create.return_value = self.fakeFunction
return_of_algo = "return of algo"
mantidSnapper = MantidSnapper(parentAlgorithm=None, name="")
test = mantidSnapper.fakeFunction("test", fakeOutput=return_of_algo)
assert str(test.__class__) == str(callback(return_of_algo.__class__).__class__)

@mock.patch("snapred.backend.recipe.algorithm.MantidSnapper.AlgorithmManager")
@mock.patch(PatchRoot.format("AlgorithmManager"))
def test_snapper_fake_queue(self, mock_AlgorithmManager):
mock_AlgorithmManager.create.return_value = self.fakeFunction
return_of_algo = "return of algo"
mantidSnapper = MantidSnapper(parentAlgorithm=None, name="")
mantidSnapper.fakeFunction("test", fakeOutput=return_of_algo)
mantidSnapper.executeQueue()
assert self.fakeFunction.execute.called

@mock.patch(PatchRoot.format("AlgorithmManager"))
def test_timeout(self, mockAlgorithmManager):
mockAlgorithmManager.runningInstancesOf = mock.Mock(return_value=["theAlgoThatNeverEnds"])
mockAlgorithmManager.create.return_value = self.fakeFunction

mantidSnapper = MantidSnapper(parentAlgorithm=None, name="")
mantidSnapper.timeout = 0.2
mantidSnapper.fakeFunction("test", fakeOutput="output")

with pytest.raises(TimeoutError, match="Timeout occured while waiting for instance of"):
mantidSnapper.executeQueue()

@mock.patch(PatchRoot.format("AlgorithmManager"))
def test_mutexIsObtained_nonConcurrent(self, mockAlgorithmManager):
mockAlgorithmManager.create.return_value = self.fakeFunction
mantidSnapper = MantidSnapper(parentAlgorithm=None, name="")
mantidSnapper.fakeFunction("test", fakeOutput="output")
mantidSnapper._nonConcurrentAlgorithms = mantidSnapper._nonConcurrentAlgorithms + ("fakeFunction",)
mantidSnapper._nonConcurrentAlgorithmMutex = mock.Mock()

mantidSnapper.executeQueue()
assert mantidSnapper._nonConcurrentAlgorithmMutex.acquire.called
assert mantidSnapper._nonConcurrentAlgorithmMutex.release.called

@mock.patch(PatchRoot.format("AlgorithmManager"))
def test_mutexIsObtained_nonReentrant(self, mockAlgorithmManager):
mockAlgorithmManager.create.return_value = self.fakeFunction
mantidSnapper = MantidSnapper(parentAlgorithm=None, name="")
mantidSnapper.fakeFunction("test", fakeOutput="output")
mantidSnapper._nonReentrantMutexes["fakeFunction"] = mock.Mock()

mantidSnapper.executeQueue()
assert mantidSnapper._nonReentrantMutexes["fakeFunction"].acquire.called
assert mantidSnapper._nonReentrantMutexes["fakeFunction"].release.called

0 comments on commit 788f840

Please sign in to comment.