test_retry.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright 2023 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import pytest
  12. from src.taipy.core.common._utils import _retry_read_entity
  13. from taipy.config import Config
  14. def test_retry_decorator(mocker):
  15. func = mocker.Mock(side_effect=Exception())
  16. @_retry_read_entity((Exception,))
  17. def decorated_func():
  18. func()
  19. with pytest.raises(Exception):
  20. decorated_func()
  21. # Called once in the normal flow and no retry
  22. # The Config.core.read_entity_retry is set to 0 at conftest.py
  23. assert Config.core.read_entity_retry == 0
  24. assert func.call_count == 1
  25. func.reset_mock()
  26. Config.core.read_entity_retry = 3
  27. with pytest.raises(Exception):
  28. decorated_func()
  29. # Called once in the normal flow and 3 more times on the retry flow
  30. assert func.call_count == 4
  31. def test_retry_decorator_exception_not_in_list(mocker):
  32. func = mocker.Mock(side_effect=KeyError())
  33. Config.core.read_entity_retry = 3
  34. @_retry_read_entity((Exception,))
  35. def decorated_func():
  36. func()
  37. with pytest.raises(KeyError):
  38. decorated_func()
  39. # Called only on the first time and not trigger retry because KeyError is not on the exceptions list
  40. assert func.called == 1