test_terminals.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # coding: utf-8
  2. import time
  3. from synergine2.config import Config
  4. from synergine2.log import SynergineLogger
  5. from synergine2.simulation import Event
  6. from synergine2.terminals import Terminal
  7. from synergine2.terminals import TerminalPackage
  8. from synergine2.terminals import TerminalManager
  9. from tests import BaseTest
  10. class ValueTerminalPackage(TerminalPackage):
  11. def __init__(self, value, *args, **kwargs):
  12. super().__init__(*args, **kwargs)
  13. self.value = value
  14. class MultiplyTerminal(Terminal):
  15. def receive(self, package: ValueTerminalPackage):
  16. self.send(ValueTerminalPackage(value=package.value * 2))
  17. self.send(ValueTerminalPackage(value=package.value * 4))
  18. class DivideTerminal(Terminal):
  19. def receive(self, package: ValueTerminalPackage):
  20. self.send(ValueTerminalPackage(value=package.value / 2))
  21. self.send(ValueTerminalPackage(value=package.value / 4))
  22. class AnEvent(Event):
  23. pass
  24. class AnOtherEvent(Event):
  25. pass
  26. class SendBackTerminal(Terminal):
  27. def receive(self, package: ValueTerminalPackage):
  28. self.send(package)
  29. class TestTerminals(BaseTest):
  30. def test_terminal_communications(self):
  31. terminals_manager = TerminalManager(
  32. Config(),
  33. SynergineLogger('test'),
  34. terminals=[
  35. MultiplyTerminal(Config(), SynergineLogger('test')),
  36. ]
  37. )
  38. terminals_manager.start()
  39. terminals_manager.send(ValueTerminalPackage(value=42))
  40. # We wait max 2s (see time.sleep) to consider
  41. # process have finished. If not, it will fail
  42. packages = []
  43. for i in range(200):
  44. packages.extend(terminals_manager.receive())
  45. if len(packages) == 2:
  46. break
  47. time.sleep(0.01)
  48. assert 2 == len(packages)
  49. values = [p.value for p in packages]
  50. assert 84 in values
  51. assert 168 in values
  52. terminals_manager.stop() # pytest must execute this if have fail
  53. def test_terminals_communications(self):
  54. terminals_manager = TerminalManager(
  55. Config(),
  56. SynergineLogger('test'),
  57. terminals=[
  58. MultiplyTerminal(Config(), SynergineLogger('test')),
  59. DivideTerminal(Config(), SynergineLogger('test')),
  60. ]
  61. )
  62. terminals_manager.start()
  63. terminals_manager.send(ValueTerminalPackage(value=42))
  64. # We wait max 2s (see time.sleep) to consider
  65. # process have finished. If not, it will fail
  66. packages = []
  67. for i in range(200):
  68. packages.extend(terminals_manager.receive())
  69. if len(packages) == 4:
  70. break
  71. time.sleep(0.01)
  72. assert 4 == len(packages)
  73. values = [p.value for p in packages]
  74. assert 84 in values
  75. assert 168 in values
  76. assert 21 in values
  77. assert 10.5 in values
  78. terminals_manager.stop() # TODO pytest must execute this if have fail
  79. def test_event_listen_everything(self):
  80. class ListenEverythingTerminal(SendBackTerminal):
  81. pass
  82. terminals_manager = TerminalManager(
  83. Config(),
  84. SynergineLogger('test'),
  85. terminals=[ListenEverythingTerminal(Config(), SynergineLogger('test'))]
  86. )
  87. terminals_manager.start()
  88. terminals_manager.send(ValueTerminalPackage(value=42))
  89. an_event = AnEvent(84)
  90. terminals_manager.send(TerminalPackage(events=[an_event]))
  91. # We wait max 2s (see time.sleep) to consider
  92. # process have finished. If not, it will fail
  93. packages = []
  94. for i in range(200):
  95. packages.extend(terminals_manager.receive())
  96. if len(packages) == 2:
  97. break
  98. time.sleep(0.01)
  99. assert 2 == len(packages)
  100. assert 42 == packages[0].value
  101. assert AnEvent == type(packages[1].events[0])
  102. terminals_manager.stop() # TODO pytest must execute this if have fail
  103. def test_event_listen_specified(self):
  104. class ListenAnEventTerminal(SendBackTerminal):
  105. subscribed_events = [AnOtherEvent]
  106. terminals_manager = TerminalManager(
  107. Config(),
  108. SynergineLogger('test'),
  109. terminals=[ListenAnEventTerminal(Config(), SynergineLogger('test'))]
  110. )
  111. terminals_manager.start()
  112. terminals_manager.send(ValueTerminalPackage(value=42))
  113. an_event = AnEvent(84)
  114. an_other_event = AnOtherEvent(168)
  115. terminals_manager.send(TerminalPackage(events=[an_event, an_other_event]))
  116. # We wait max 10s (see time.sleep) to consider
  117. # process have finished. If not, it will fail
  118. packages = []
  119. for i in range(1000):
  120. packages.extend(terminals_manager.receive())
  121. if len(packages) == 2:
  122. break
  123. time.sleep(0.01)
  124. assert 2 == len(packages)
  125. assert AnOtherEvent == type(packages[1].events[0])
  126. terminals_manager.stop() # TODO pytest must execute this if have fail