__init__.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. from datetime import datetime
  12. from time import sleep
  13. from taipy import Submission
  14. def assert_true_after_time(assertion, time=120, msg=None, **msg_params):
  15. loops = 0
  16. start = datetime.now()
  17. while (datetime.now() - start).seconds < time:
  18. sleep(1) # Limit CPU usage
  19. try:
  20. if assertion():
  21. return
  22. except BaseException as e:
  23. print("Raise : ", e) # noqa: T201
  24. loops += 1
  25. continue
  26. if msg:
  27. print(msg(**msg_params)) # noqa: T201
  28. assert assertion()
  29. def assert_submission_status(submission: Submission, expected_status, timeout=120):
  30. assert_true_after_time(
  31. lambda: submission.submission_status == expected_status,
  32. time=timeout,
  33. msg=submission_status_message,
  34. submission=submission,
  35. timeout=timeout)
  36. def submission_status_message(submission: Submission, expected_status, timeout=120):
  37. ms = "\n--------------------------------------------------------------------------------\n"
  38. ms += f"Submission status is {submission.submission_status} after {timeout} seconds.\n"
  39. ms += f" Expected status was {expected_status}.\n"
  40. ms += "--------------------------------------------------------------------------------\n"
  41. ms += " -------------- \n"
  42. ms += " Job statuses \n"
  43. for job in submission.jobs:
  44. ms += f"{job.id}: {job.status}\n"
  45. ms += "--------------------------------------------------------------------------------\n"
  46. return ms