test_terminals.py 4.5KB

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