test_retry.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright 2021-2025 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 taipy.common.config import Config
  13. from taipy.core.common._utils import _retry_repository_operation
  14. from taipy.core.exceptions import InvalidExposedType
  15. def test_retry_decorator(mocker):
  16. func = mocker.Mock(side_effect=InvalidExposedType())
  17. @_retry_repository_operation((InvalidExposedType,))
  18. def decorated_func():
  19. func()
  20. with pytest.raises(InvalidExposedType):
  21. decorated_func()
  22. # Called once in the normal flow and no retry
  23. # The Config.core.read_entity_retry is set to 0 at conftest.py
  24. assert Config.core.read_entity_retry == 0
  25. assert func.call_count == 1
  26. func.reset_mock()
  27. Config.core.read_entity_retry = 3
  28. with pytest.raises(InvalidExposedType):
  29. decorated_func()
  30. # Called once in the normal flow and 3 more times on the retry flow
  31. assert func.call_count == 4
  32. def test_retry_decorator_exception_not_in_list(mocker):
  33. func = mocker.Mock(side_effect=KeyError())
  34. Config.core.read_entity_retry = 3
  35. @_retry_repository_operation((InvalidExposedType,))
  36. def decorated_func():
  37. func()
  38. with pytest.raises(KeyError):
  39. decorated_func()
  40. # Called only on the first time and not trigger retry because KeyError is not on the exceptions list
  41. assert func.called == 1