test_event_warnings_errors.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. from openleadr import OpenADRClient, OpenADRServer, enable_default_logging
  2. import pytest
  3. from functools import partial
  4. import asyncio
  5. from datetime import datetime, timedelta
  6. import logging
  7. async def on_create_party_registration(ven_name):
  8. return 'venid', 'regid'
  9. async def on_event_accepted(ven_id, event_id, opt_type, future):
  10. future.set_result(opt_type)
  11. async def good_on_event(event):
  12. return 'optIn'
  13. async def faulty_on_event(event):
  14. return None
  15. async def broken_on_event(event):
  16. raise KeyError("BOOM")
  17. @pytest.mark.asyncio
  18. async def test_client_no_event_handler(caplog):
  19. caplog.set_level(logging.WARNING)
  20. enable_default_logging()
  21. logger = logging.getLogger('openleadr')
  22. logger.setLevel(logging.DEBUG)
  23. client = OpenADRClient(ven_name='myven',
  24. vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b')
  25. server = OpenADRServer(vtn_id='myvtn', requested_poll_freq=timedelta(seconds=1))
  26. server.add_handler('on_create_party_registration', on_create_party_registration)
  27. print("Running server")
  28. await server.run_async()
  29. await asyncio.sleep(0.1)
  30. print("Running client")
  31. await client.run()
  32. event_confirm_future = asyncio.get_event_loop().create_future()
  33. print("Adding event")
  34. server.add_event(ven_id='venid',
  35. signal_name='simple',
  36. signal_type='level',
  37. intervals=[{'dtstart': datetime.now(),
  38. 'duration': timedelta(seconds=10),
  39. 'signal_payload': 1.1}],
  40. target={'ven_id': 'venid'},
  41. callback=partial(on_event_accepted, future=event_confirm_future))
  42. print("Waiting for a response to the event")
  43. result = await event_confirm_future
  44. assert result == 'optOut'
  45. assert ("You should implement your own on_event handler. This handler receives "
  46. "an Event dict and should return either 'optIn' or 'optOut' based on your "
  47. "choice. Will opt out of the event for now.") in [rec.message for rec in caplog.records]
  48. await client.stop()
  49. await server.stop()
  50. await asyncio.sleep(0)
  51. @pytest.mark.asyncio
  52. async def test_client_faulty_event_handler(caplog):
  53. caplog.set_level(logging.WARNING)
  54. enable_default_logging()
  55. logger = logging.getLogger('openleadr')
  56. logger.setLevel(logging.DEBUG)
  57. client = OpenADRClient(ven_name='myven',
  58. vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b')
  59. client.add_handler('on_event', faulty_on_event)
  60. server = OpenADRServer(vtn_id='myvtn', requested_poll_freq=timedelta(seconds=1))
  61. server.add_handler('on_create_party_registration', on_create_party_registration)
  62. print("Running server")
  63. await server.run_async()
  64. await asyncio.sleep(0.1)
  65. print("Running client")
  66. await client.run()
  67. event_confirm_future = asyncio.get_event_loop().create_future()
  68. print("Adding event")
  69. server.add_event(ven_id='venid',
  70. signal_name='simple',
  71. signal_type='level',
  72. intervals=[{'dtstart': datetime.now(),
  73. 'duration': timedelta(seconds=10),
  74. 'signal_payload': 1.1}],
  75. target={'ven_id': 'venid'},
  76. callback=partial(on_event_accepted, future=event_confirm_future))
  77. print("Waiting for a response to the event")
  78. result = await event_confirm_future
  79. assert result == 'optOut'
  80. assert ("Your on_event handler must return 'optIn' or 'optOut'; "
  81. f"you supplied {None}. Please fix your on_event handler.") in [rec.message for rec in caplog.records]
  82. await client.stop()
  83. await server.stop()
  84. await asyncio.sleep(0)
  85. @pytest.mark.asyncio
  86. async def test_client_exception_event_handler(caplog):
  87. caplog.set_level(logging.WARNING)
  88. enable_default_logging()
  89. logger = logging.getLogger('openleadr')
  90. logger.setLevel(logging.DEBUG)
  91. client = OpenADRClient(ven_name='myven',
  92. vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b')
  93. client.add_handler('on_event', broken_on_event)
  94. server = OpenADRServer(vtn_id='myvtn', requested_poll_freq=timedelta(seconds=1))
  95. server.add_handler('on_create_party_registration', on_create_party_registration)
  96. print("Running server")
  97. await server.run_async()
  98. await asyncio.sleep(0.1)
  99. print("Running client")
  100. await client.run()
  101. event_confirm_future = asyncio.get_event_loop().create_future()
  102. print("Adding event")
  103. server.add_event(ven_id='venid',
  104. signal_name='simple',
  105. signal_type='level',
  106. intervals=[{'dtstart': datetime.now(),
  107. 'duration': timedelta(seconds=10),
  108. 'signal_payload': 1.1}],
  109. target={'ven_id': 'venid'},
  110. callback=partial(on_event_accepted, future=event_confirm_future))
  111. print("Waiting for a response to the event")
  112. result = await event_confirm_future
  113. assert result == 'optOut'
  114. err = KeyError("BOOM")
  115. assert ("Your on_event handler encountered an error. Will Opt Out of the event. "
  116. f"The error was {err.__class__.__name__}: {str(err)}") in [rec.message for rec in caplog.records]
  117. await client.stop()
  118. await server.stop()
  119. await asyncio.sleep(0)
  120. @pytest.mark.asyncio
  121. async def test_client_good_event_handler(caplog):
  122. caplog.set_level(logging.WARNING)
  123. enable_default_logging()
  124. logger = logging.getLogger('openleadr')
  125. logger.setLevel(logging.DEBUG)
  126. client = OpenADRClient(ven_name='myven',
  127. vtn_url='http://localhost:8080/OpenADR2/Simple/2.0b')
  128. client.add_handler('on_event', good_on_event)
  129. server = OpenADRServer(vtn_id='myvtn', requested_poll_freq=timedelta(seconds=1))
  130. server.add_handler('on_create_party_registration', on_create_party_registration)
  131. print("Running server")
  132. await server.run_async()
  133. await asyncio.sleep(0.1)
  134. print("Running client")
  135. await client.run()
  136. event_confirm_future = asyncio.get_event_loop().create_future()
  137. print("Adding event")
  138. server.add_event(ven_id='venid',
  139. signal_name='simple',
  140. signal_type='level',
  141. intervals=[{'dtstart': datetime.now(),
  142. 'duration': timedelta(seconds=10),
  143. 'signal_payload': 1.1}],
  144. target={'ven_id': 'venid'},
  145. callback=partial(on_event_accepted, future=event_confirm_future))
  146. print("Waiting for a response to the event")
  147. result = await event_confirm_future
  148. assert result == 'optIn'
  149. assert len(caplog.records) == 0
  150. await asyncio.sleep(1)
  151. await client.stop()
  152. await server.stop()
  153. await asyncio.sleep(1)
  154. @pytest.mark.asyncio
  155. async def test_server_warning_conflicting_poll_methods(caplog):
  156. caplog.set_level(logging.WARNING)
  157. enable_default_logging()
  158. logger = logging.getLogger('openleadr')
  159. logger.setLevel(logging.DEBUG)
  160. server = OpenADRServer(vtn_id='myvtn', requested_poll_freq=timedelta(seconds=1))
  161. server.add_handler('on_poll', print)
  162. print("Running server")
  163. await server.run_async()
  164. await asyncio.sleep(0.1)
  165. server.add_event(ven_id='venid',
  166. signal_name='simple',
  167. signal_type='level',
  168. intervals=[{'dtstart': datetime.now(),
  169. 'duration': timedelta(seconds=10),
  170. 'signal_payload': 1.1}],
  171. target={'ven_id': 'venid'},
  172. callback=print)
  173. assert ("You cannot use the add_event method after you assign your own on_poll "
  174. "handler. If you use your own on_poll handler, you are responsible for "
  175. "delivering events from that handler. If you want to use OpenLEADRs "
  176. "message queuing system, you should not assign an on_poll handler. "
  177. "Your Event will NOT be added.") in [record.msg for record in caplog.records]
  178. await server.stop()
  179. await asyncio.sleep(0)