test_events_published.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # Copyright 2021-2025 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 queue import SimpleQueue
  12. from typing import Any, Dict, List
  13. import pytest
  14. from taipy import Orchestrator
  15. from taipy.common.config import Config, Frequency
  16. from taipy.core import taipy as tp
  17. from taipy.core.job.status import Status
  18. from taipy.core.notification.core_event_consumer import CoreEventConsumerBase
  19. from taipy.core.notification.event import Event, EventEntityType, EventOperation
  20. from taipy.core.notification.notifier import Notifier
  21. from taipy.core.scenario._scenario_manager_factory import _ScenarioManagerFactory
  22. class Snapshot:
  23. """
  24. A captured snapshot of the recording core events consumer.
  25. """
  26. def __init__(self) -> None:
  27. self.collected_events: List[Event] = []
  28. self.entity_type_collected: Dict[EventEntityType, int] = {}
  29. self.operation_collected: Dict[EventEntityType, int] = {}
  30. self.attr_name_collected: Dict[EventEntityType, int] = {}
  31. self.attr_value_collected: Dict[EventEntityType, List[Any]] = {}
  32. def capture_event(self, event):
  33. self.collected_events.append(event)
  34. self.entity_type_collected[event.entity_type] = self.entity_type_collected.get(event.entity_type, 0) + 1
  35. self.operation_collected[event.operation] = self.operation_collected.get(event.operation, 0) + 1
  36. if event.attribute_name:
  37. self.attr_name_collected[event.attribute_name] = self.attr_name_collected.get(event.attribute_name, 0) + 1
  38. if self.attr_value_collected.get(event.attribute_name, None):
  39. self.attr_value_collected[event.attribute_name].append(event.attribute_value)
  40. else:
  41. self.attr_value_collected[event.attribute_name] = [event.attribute_value]
  42. class RecordingConsumer(CoreEventConsumerBase):
  43. """
  44. A straightforward and no-thread core events consumer that allows to
  45. capture snapshots of received events.
  46. """
  47. def __init__(self, registration_id: str, queue: SimpleQueue):
  48. super().__init__(registration_id, queue)
  49. def capture(self) -> Snapshot:
  50. """
  51. Capture a snapshot of events received between the previous snapshot
  52. (or from the start of this consumer).
  53. """
  54. snapshot = Snapshot()
  55. while not self.queue.empty():
  56. event = self.queue.get()
  57. snapshot.capture_event(event)
  58. return snapshot
  59. def process_event(self, event: Event):
  60. # Nothing to do
  61. pass
  62. def start(self):
  63. # Nothing to do here
  64. pass
  65. def stop(self):
  66. # Nothing to do here either
  67. pass
  68. def identity(x):
  69. return x
  70. def test_events_published_for_scenario_creation():
  71. input_config = Config.configure_data_node("the_input")
  72. output_config = Config.configure_data_node("the_output")
  73. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  74. sc_config = Config.configure_scenario(
  75. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  76. )
  77. register_id_0, register_queue_0 = Notifier.register()
  78. all_evts = RecordingConsumer(register_id_0, register_queue_0)
  79. all_evts.start()
  80. # Create a scenario via the manager
  81. # should only trigger 6 creation events (for cycle, data node(x2), task, sequence and scenario)
  82. _ScenarioManagerFactory._build_manager()._create(sc_config)
  83. snapshot = all_evts.capture()
  84. assert len(snapshot.collected_events) == 6
  85. assert snapshot.entity_type_collected.get(EventEntityType.CYCLE, 0) == 1
  86. assert snapshot.entity_type_collected.get(EventEntityType.DATA_NODE, 0) == 2
  87. assert snapshot.entity_type_collected.get(EventEntityType.TASK, 0) == 1
  88. assert snapshot.entity_type_collected.get(EventEntityType.SEQUENCE, 0) == 1
  89. assert snapshot.entity_type_collected.get(EventEntityType.SCENARIO, 0) == 1
  90. assert snapshot.operation_collected.get(EventOperation.CREATION, 0) == 6
  91. all_evts.stop()
  92. def test_no_event_published_for_getting_scenario():
  93. input_config = Config.configure_data_node("the_input")
  94. output_config = Config.configure_data_node("the_output")
  95. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  96. sc_config = Config.configure_scenario(
  97. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  98. )
  99. scenario = tp.create_scenario(sc_config)
  100. register_id_0, register_queue_0 = Notifier.register()
  101. all_evts = RecordingConsumer(register_id_0, register_queue_0)
  102. all_evts.start()
  103. # Get all scenarios does not trigger any event
  104. tp.get_scenarios()
  105. snapshot = all_evts.capture()
  106. assert len(snapshot.collected_events) == 0
  107. # Get one scenario does not trigger any event
  108. tp.get(scenario.id)
  109. snapshot = all_evts.capture()
  110. assert len(snapshot.collected_events) == 0
  111. all_evts.stop()
  112. def test_events_published_for_writing_dn():
  113. input_config = Config.configure_data_node("the_input")
  114. output_config = Config.configure_data_node("the_output")
  115. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  116. sc_config = Config.configure_scenario(
  117. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  118. )
  119. scenario = tp.create_scenario(sc_config)
  120. register_id_0, register_queue_0 = Notifier.register()
  121. all_evts = RecordingConsumer(register_id_0, register_queue_0)
  122. all_evts.start()
  123. # Write input manually trigger 5 data node update events
  124. # for last_edit_date, editor_id, editor_expiration_date, edit_in_progress and edits
  125. scenario.the_input.write("test")
  126. snapshot = all_evts.capture()
  127. assert len(snapshot.collected_events) == 5
  128. assert snapshot.entity_type_collected.get(EventEntityType.DATA_NODE, 0) == 5
  129. assert snapshot.operation_collected.get(EventOperation.UPDATE, 0) == 5
  130. all_evts.stop()
  131. @pytest.mark.parametrize("standalone", [False, True])
  132. def test_events_published_for_scenario_submission(standalone):
  133. if standalone:
  134. Config.configure_job_executions(mode="standalone", max_nb_of_workers=2)
  135. input_config = Config.configure_data_node("the_input")
  136. output_config = Config.configure_data_node("the_output")
  137. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  138. sc_config = Config.configure_scenario(
  139. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  140. )
  141. scenario = tp.create_scenario(sc_config)
  142. scenario.the_input.write("test")
  143. register_id_0, register_queue_0 = Notifier.register()
  144. all_evts = RecordingConsumer(register_id_0, register_queue_0)
  145. all_evts.start()
  146. # Submit a scenario triggers:
  147. # 1 scenario submission event
  148. # 7 dn update events (for last_edit_date, editor_id(x2), editor_expiration_date(x2) and edit_in_progress(x2))
  149. # 1 job creation event
  150. # 3 job update events (for status: PENDING, RUNNING and COMPLETED)
  151. # 1 submission creation event
  152. # 1 submission update event for jobs
  153. # 3 submission update events (for status: PENDING, RUNNING and COMPLETED)
  154. # 1 submission update event for is_completed
  155. if standalone:
  156. Orchestrator().run()
  157. scenario.submit(wait=True)
  158. else:
  159. scenario.submit()
  160. snapshot = all_evts.capture()
  161. assert len(snapshot.collected_events) == 18
  162. assert snapshot.entity_type_collected.get(EventEntityType.CYCLE, 0) == 0
  163. assert snapshot.entity_type_collected.get(EventEntityType.DATA_NODE, 0) == 8
  164. assert snapshot.entity_type_collected.get(EventEntityType.TASK, 0) == 0
  165. assert snapshot.entity_type_collected.get(EventEntityType.SEQUENCE, 0) == 0
  166. assert snapshot.entity_type_collected.get(EventEntityType.SCENARIO, 0) == 1
  167. assert snapshot.entity_type_collected.get(EventEntityType.JOB, 0) == 4
  168. assert snapshot.entity_type_collected.get(EventEntityType.SUBMISSION, 0) == 5
  169. assert snapshot.operation_collected.get(EventOperation.CREATION, 0) == 2
  170. assert snapshot.operation_collected.get(EventOperation.UPDATE, 0) == 15
  171. assert snapshot.operation_collected.get(EventOperation.SUBMISSION, 0) == 1
  172. assert snapshot.attr_name_collected["last_edit_date"] == 1
  173. assert snapshot.attr_name_collected["editor_id"] == 2
  174. assert snapshot.attr_name_collected["editor_expiration_date"] == 2
  175. assert snapshot.attr_name_collected["edit_in_progress"] == 2
  176. assert snapshot.attr_name_collected["edits"] == 1
  177. assert snapshot.attr_name_collected["status"] == 3
  178. assert snapshot.attr_name_collected["jobs"] == 1
  179. assert snapshot.attr_name_collected["submission_status"] == 3
  180. all_evts.stop()
  181. def test_events_published_for_scenario_deletion():
  182. input_config = Config.configure_data_node("the_input")
  183. output_config = Config.configure_data_node("the_output")
  184. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  185. sc_config = Config.configure_scenario(
  186. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  187. )
  188. scenario = tp.create_scenario(sc_config)
  189. scenario.the_input.write("test")
  190. scenario.submit()
  191. register_id_0, register_queue_0 = Notifier.register()
  192. all_evts = RecordingConsumer(register_id_0, register_queue_0)
  193. all_evts.start()
  194. # Delete a scenario trigger 8 deletion events
  195. # 1 scenario deletion event
  196. # 1 cycle deletion event
  197. # 2 dn deletion events (for input and output)
  198. # 1 task deletion event
  199. # 1 sequence deletion event
  200. # 1 job deletion event
  201. # 1 submission deletion event
  202. tp.delete(scenario.id)
  203. snapshot = all_evts.capture()
  204. assert len(snapshot.collected_events) == 8
  205. assert snapshot.entity_type_collected.get(EventEntityType.CYCLE, 0) == 1
  206. assert snapshot.entity_type_collected.get(EventEntityType.DATA_NODE, 0) == 2
  207. assert snapshot.entity_type_collected.get(EventEntityType.TASK, 0) == 1
  208. assert snapshot.entity_type_collected.get(EventEntityType.SEQUENCE, 0) == 1
  209. assert snapshot.entity_type_collected.get(EventEntityType.SCENARIO, 0) == 1
  210. assert snapshot.entity_type_collected.get(EventEntityType.SUBMISSION, 0) == 1
  211. assert snapshot.entity_type_collected.get(EventEntityType.JOB, 0) == 1
  212. assert snapshot.operation_collected.get(EventOperation.UPDATE, 0) == 0
  213. assert snapshot.operation_collected.get(EventOperation.SUBMISSION, 0) == 0
  214. assert snapshot.operation_collected.get(EventOperation.DELETION, 0) == 8
  215. all_evts.stop()
  216. def test_job_events():
  217. input_config = Config.configure_data_node("the_input")
  218. output_config = Config.configure_data_node("the_output")
  219. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  220. sc_config = Config.configure_scenario(
  221. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  222. )
  223. register_id, register_queue = Notifier.register(entity_type=EventEntityType.JOB)
  224. consumer = RecordingConsumer(register_id, register_queue)
  225. consumer.start()
  226. # Create scenario
  227. scenario = _ScenarioManagerFactory._build_manager()._create(sc_config)
  228. snapshot = consumer.capture()
  229. assert len(snapshot.collected_events) == 0
  230. # Submit scenario
  231. scenario.submit()
  232. snapshot = consumer.capture()
  233. # 2 events expected: one for creation, another for status update
  234. assert len(snapshot.collected_events) == 2
  235. assert snapshot.collected_events[0].operation == EventOperation.CREATION
  236. assert snapshot.collected_events[0].entity_type == EventEntityType.JOB
  237. assert snapshot.collected_events[0].metadata.get("task_config_id") == task_config.id
  238. assert snapshot.collected_events[1].operation == EventOperation.UPDATE
  239. assert snapshot.collected_events[1].entity_type == EventEntityType.JOB
  240. assert snapshot.collected_events[1].metadata.get("task_config_id") == task_config.id
  241. assert snapshot.collected_events[1].attribute_name == "status"
  242. assert snapshot.collected_events[1].attribute_value == Status.BLOCKED
  243. job = tp.get_jobs()[0]
  244. tp.cancel_job(job)
  245. snapshot = consumer.capture()
  246. assert len(snapshot.collected_events) == 1
  247. event = snapshot.collected_events[0]
  248. assert event.metadata.get("task_config_id") == task_config.id
  249. assert event.attribute_name == "status"
  250. assert event.attribute_value == Status.CANCELED
  251. consumer.stop()
  252. def test_scenario_events():
  253. input_config = Config.configure_data_node("the_input")
  254. output_config = Config.configure_data_node("the_output")
  255. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  256. sc_config = Config.configure_scenario(
  257. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  258. )
  259. register_id, register_queue = Notifier.register(entity_type=EventEntityType.SCENARIO)
  260. consumer = RecordingConsumer(register_id, register_queue)
  261. consumer.start()
  262. scenario = tp.create_scenario(sc_config)
  263. snapshot = consumer.capture()
  264. assert len(snapshot.collected_events) == 1
  265. assert snapshot.collected_events[0].operation == EventOperation.CREATION
  266. assert snapshot.collected_events[0].entity_type == EventEntityType.SCENARIO
  267. assert snapshot.collected_events[0].metadata.get("config_id") == scenario.config_id
  268. scenario.submit()
  269. snapshot = consumer.capture()
  270. assert len(snapshot.collected_events) == 1
  271. assert snapshot.collected_events[0].operation == EventOperation.SUBMISSION
  272. assert snapshot.collected_events[0].entity_type == EventEntityType.SCENARIO
  273. assert snapshot.collected_events[0].metadata.get("config_id") == scenario.config_id
  274. # Delete scenario
  275. tp.delete(scenario.id)
  276. snapshot = consumer.capture()
  277. assert len(snapshot.collected_events) == 1
  278. assert snapshot.collected_events[0].operation == EventOperation.DELETION
  279. assert snapshot.collected_events[0].entity_type == EventEntityType.SCENARIO
  280. consumer.stop()
  281. def test_data_node_events():
  282. input_config = Config.configure_data_node("the_input")
  283. output_config = Config.configure_data_node("the_output")
  284. task_config = Config.configure_task("the_task", identity, input=input_config, output=output_config)
  285. sc_config = Config.configure_scenario(
  286. "the_scenario", task_configs=[task_config], frequency=Frequency.DAILY, sequences={"the_seq": [task_config]}
  287. )
  288. register_id, register_queue = Notifier.register(entity_type=EventEntityType.DATA_NODE)
  289. consumer = RecordingConsumer(register_id, register_queue)
  290. consumer.start()
  291. scenario = _ScenarioManagerFactory._build_manager()._create(sc_config)
  292. snapshot = consumer.capture()
  293. # We expect two creation events since we have two data nodes:
  294. assert len(snapshot.collected_events) == 2
  295. assert snapshot.collected_events[0].operation == EventOperation.CREATION
  296. assert snapshot.collected_events[0].entity_type == EventEntityType.DATA_NODE
  297. assert snapshot.collected_events[0].metadata.get("config_id") in [output_config.id, input_config.id]
  298. assert snapshot.collected_events[1].operation == EventOperation.CREATION
  299. assert snapshot.collected_events[1].entity_type == EventEntityType.DATA_NODE
  300. assert snapshot.collected_events[1].metadata.get("config_id") in [output_config.id, input_config.id]
  301. # Delete scenario
  302. tp.delete(scenario.id)
  303. snapshot = consumer.capture()
  304. assert len(snapshot.collected_events) == 2
  305. assert snapshot.collected_events[0].operation == EventOperation.DELETION
  306. assert snapshot.collected_events[0].entity_type == EventEntityType.DATA_NODE
  307. assert snapshot.collected_events[1].operation == EventOperation.DELETION
  308. assert snapshot.collected_events[1].entity_type == EventEntityType.DATA_NODE
  309. consumer.stop()