test_core.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 taipy.config import Config
  14. from taipy.config.exceptions.exceptions import ConfigurationUpdateBlocked
  15. from taipy.core import Core
  16. from taipy.core._orchestrator._dispatcher import _DevelopmentJobDispatcher, _StandaloneJobDispatcher
  17. from taipy.core._orchestrator._orchestrator import _Orchestrator
  18. from taipy.core._orchestrator._orchestrator_factory import _OrchestratorFactory
  19. from taipy.core.config.job_config import JobConfig
  20. from taipy.core.exceptions.exceptions import CoreServiceIsAlreadyRunning
  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, s3_object, 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. with patch("sys.argv", ["prog"]):
  72. core_instance_1 = Core()
  73. core_instance_2 = Core()
  74. core_instance_1.run()
  75. with pytest.raises(CoreServiceIsAlreadyRunning):
  76. core_instance_1.run()
  77. with pytest.raises(CoreServiceIsAlreadyRunning):
  78. core_instance_2.run()
  79. # Stop the Core service and run it again should work
  80. core_instance_1.stop()
  81. core_instance_1.run()
  82. core_instance_1.stop()
  83. core_instance_2.run()
  84. core_instance_2.stop()
  85. def test_block_config_update_when_core_service_is_running_development_mode(self):
  86. _OrchestratorFactory._dispatcher = None
  87. with patch("sys.argv", ["prog"]):
  88. core = Core()
  89. core.run()
  90. with pytest.raises(ConfigurationUpdateBlocked):
  91. Config.configure_data_node(id="i1")
  92. core.stop()
  93. @pytest.mark.standalone
  94. def test_block_config_update_when_core_service_is_running_standalone_mode(self):
  95. _OrchestratorFactory._dispatcher = None
  96. with patch("sys.argv", ["prog"]):
  97. core = Core()
  98. Config.configure_job_executions(mode=JobConfig._STANDALONE_MODE, max_nb_of_workers=2)
  99. core.run()
  100. with pytest.raises(ConfigurationUpdateBlocked):
  101. Config.configure_data_node(id="i1")
  102. core.stop()