test_long_running.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 inspect
  12. from time import sleep
  13. from unittest.mock import patch
  14. from taipy.gui import Gui, State, invoke_long_callback
  15. def test_long_callback(gui: Gui):
  16. status = None # noqa: F841
  17. def heavy_function(delay=1):
  18. sleep(delay)
  19. def heavy_function_with_exception(delay=1):
  20. sleep(delay)
  21. raise Exception("Heavy function Exception")
  22. def heavy_function_status(state: State, status: int):
  23. state.status = status
  24. def on_exception(state: State, function_name: str, e: Exception):
  25. state.status = -1
  26. gui._set_frame(inspect.currentframe())
  27. with patch("sys.argv", ["prog"]):
  28. gui.run(run_server=False, single_client=True)
  29. state = gui._Gui__state
  30. with gui.get_flask_app().app_context():
  31. assert state.status is None
  32. invoke_long_callback(state, heavy_function)
  33. invoke_long_callback(state, heavy_function_with_exception)
  34. invoke_long_callback(state, heavy_function, (), heavy_function_status)
  35. invoke_long_callback(state, heavy_function, (2), heavy_function_status, (), 1000)
  36. invoke_long_callback(state, heavy_function_with_exception, (), heavy_function_status)