test_fatal_error_in_private_loop.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright 2016 by MPI-SWS and Data-Ken Research.
  2. # Licensed under the Apache 2.0 License.
  3. """Run a private event loop and then throw a fatal error in it to verify that
  4. we shut down cleanly and don't lose the error.
  5. """
  6. import thingflow.filters.output
  7. from thingflow.base import Scheduler, OutputThing, EventLoopOutputThingMixin, FatalError
  8. import unittest
  9. import asyncio
  10. s = Scheduler(asyncio.get_event_loop())
  11. import time
  12. class TestOutputThing(OutputThing, EventLoopOutputThingMixin):
  13. def __init__(self):
  14. super().__init__()
  15. def _observe_event_loop(self):
  16. print("starting event loop")
  17. for i in range(4):
  18. if self.stop_requested:
  19. break
  20. self._dispatch_next(i)
  21. time.sleep(1)
  22. raise FatalError("testing the fatal error")
  23. class TestFatalErrorInPrivateLoop(unittest.TestCase):
  24. def test_case(self):
  25. m = TestOutputThing()
  26. m.output()
  27. c = s.schedule_on_private_event_loop(m)
  28. m.print_downstream()
  29. try:
  30. s.run_forever()
  31. except FatalError:
  32. print("we got the fatal error as expected")
  33. else:
  34. print("The event loop exited without throwing a fatal error!")
  35. self.assertFalse(1, "The event loop exited without throwing a fatal error!")
  36. if __name__ == '__main__':
  37. unittest.main()