import time import mock from robot_interface.utils.timeblock import TimeBlock class AnyFloat(object): """ A helper class used in tests to assert that a mock function was called with an argument that is specifically a float, regardless of its value. It overrides the equality comparison (`__eq__`) to check only the type. """ def __eq__(self, other): return isinstance(other, float) def test_no_limit(): """ Tests the scenario where the `TimeBlock` context manager is used without a time limit. """ callback = mock.Mock() with TimeBlock(callback): pass callback.assert_called_once_with(AnyFloat()) def test_exceed_limit(): """ Tests the scenario where the execution time within the `TimeBlock` exceeds the provided limit. """ callback = mock.Mock() with TimeBlock(callback, 0): time.sleep(0.001) callback.assert_called_once_with(AnyFloat()) def test_within_limit(): """ Tests the scenario where the execution time within the `TimeBlock` stays within the provided limit. """ callback = mock.Mock() with TimeBlock(callback, 5): pass callback.assert_not_called()