test_core.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. from unittest.mock import patch
  12. import pytest
  13. from src.taipy.core import Core
  14. from src.taipy.core._orchestrator._dispatcher import _DevelopmentJobDispatcher, _StandaloneJobDispatcher
  15. from src.taipy.core._orchestrator._orchestrator import _Orchestrator
  16. from src.taipy.core._orchestrator._orchestrator_factory import _OrchestratorFactory
  17. from src.taipy.core.config.job_config import JobConfig
  18. from src.taipy.core.exceptions.exceptions import CoreServiceIsAlreadyRunning
  19. from taipy.config import Config
  20. from taipy.config.exceptions.exceptions import ConfigurationUpdateBlocked
  21. class TestCore:
  22. def test_run_core_trigger_config_check(self, caplog):
  23. Config.configure_data_node(id="d0", storage_type="toto")
  24. with patch("sys.argv", ["prog"]):
  25. with pytest.raises(SystemExit):
  26. core = Core()
  27. core.run()
  28. expected_error_message = (
  29. "`storage_type` field of DataNodeConfig `d0` must be either csv, sql_table,"
  30. " sql, mongo_collection, pickle, excel, generic, json, parquet, or in_memory."
  31. ' Current value of property `storage_type` is "toto".'
  32. )
  33. assert expected_error_message in caplog.text
  34. core.stop()
  35. def test_run_core_as_a_service_development_mode(self):
  36. _OrchestratorFactory._dispatcher = None
  37. with patch("sys.argv", ["prog"]):
  38. core = Core()
  39. assert core._orchestrator is None
  40. assert core._dispatcher is None
  41. assert _OrchestratorFactory._dispatcher is None
  42. core.run()
  43. assert core._orchestrator is not None
  44. assert core._orchestrator == _Orchestrator
  45. assert _OrchestratorFactory._orchestrator is not None
  46. assert _OrchestratorFactory._orchestrator == _Orchestrator
  47. assert core._dispatcher is not None
  48. assert isinstance(core._dispatcher, _DevelopmentJobDispatcher)
  49. assert isinstance(_OrchestratorFactory._dispatcher, _DevelopmentJobDispatcher)
  50. core.stop()
  51. def test_run_core_as_a_service_standalone_mode(self):
  52. _OrchestratorFactory._dispatcher = None
  53. with patch("sys.argv", ["prog"]):
  54. core = Core()
  55. assert core._orchestrator is None
  56. assert core._dispatcher is None
  57. assert _OrchestratorFactory._dispatcher is None
  58. Config.configure_job_executions(mode=JobConfig._STANDALONE_MODE, max_nb_of_workers=2)
  59. core.run()
  60. assert core._orchestrator is not None
  61. assert core._orchestrator == _Orchestrator
  62. assert _OrchestratorFactory._orchestrator is not None
  63. assert _OrchestratorFactory._orchestrator == _Orchestrator
  64. assert core._dispatcher is not None
  65. assert isinstance(core._dispatcher, _StandaloneJobDispatcher)
  66. assert isinstance(_OrchestratorFactory._dispatcher, _StandaloneJobDispatcher)
  67. assert core._dispatcher.is_running()
  68. assert _OrchestratorFactory._dispatcher.is_running()
  69. core.stop()
  70. def test_core_service_can_only_be_run_once(self):
  71. core_instance_1 = Core()
  72. core_instance_2 = Core()
  73. core_instance_1.run()
  74. with pytest.raises(CoreServiceIsAlreadyRunning):
  75. core_instance_1.run()
  76. with pytest.raises(CoreServiceIsAlreadyRunning):
  77. core_instance_2.run()
  78. # Stop the Core service and run it again should work
  79. core_instance_1.stop()
  80. core_instance_1.run()
  81. core_instance_1.stop()
  82. core_instance_2.run()
  83. core_instance_2.stop()
  84. def test_block_config_update_when_core_service_is_running_development_mode(self):
  85. _OrchestratorFactory._dispatcher = None
  86. with patch("sys.argv", ["prog"]):
  87. core = Core()
  88. core.run()
  89. with pytest.raises(ConfigurationUpdateBlocked):
  90. Config.configure_data_node(id="i1")
  91. core.stop()
  92. def test_block_config_update_when_core_service_is_running_standalone_mode(self):
  93. _OrchestratorFactory._dispatcher = None
  94. with patch("sys.argv", ["prog"]):
  95. core = Core()
  96. Config.configure_job_executions(mode=JobConfig._STANDALONE_MODE, max_nb_of_workers=2)
  97. core.run()
  98. with pytest.raises(ConfigurationUpdateBlocked):
  99. Config.configure_data_node(id="i1")
  100. core.stop()