test_long_running.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright 2021-2024 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 taipy.gui import Gui, State, invoke_long_callback
  14. def test_long_callback(gui: Gui):
  15. status = None # noqa: F841
  16. def heavy_function(delay=1):
  17. sleep(delay)
  18. def heavy_function_with_exception(delay=1):
  19. sleep(delay)
  20. raise Exception("Heavy function Exception")
  21. def heavy_function_status(state: State, status: int):
  22. state.status = status
  23. def on_exception(state: State, function_name: str, e: Exception):
  24. state.status = -1
  25. gui._set_frame(inspect.currentframe())
  26. gui.run(run_server=False, single_client=True)
  27. state = gui._Gui__state # type: ignore[attr-defined]
  28. with gui.get_flask_app().app_context():
  29. assert state.status is None
  30. invoke_long_callback(state, heavy_function)
  31. invoke_long_callback(state, heavy_function_with_exception)
  32. invoke_long_callback(state, heavy_function, (), heavy_function_status)
  33. invoke_long_callback(state, heavy_function, (2,), heavy_function_status, (), 1000)
  34. invoke_long_callback(state, heavy_function_with_exception, (), heavy_function_status)