events.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. import asyncio
  22. import qubes.events
  23. import qubes.tests
  24. class TC_00_Emitter(qubes.tests.QubesTestCase):
  25. def test_000_add_handler(self):
  26. # need something mutable
  27. testevent_fired = [False]
  28. def on_testevent(subject, event):
  29. # pylint: disable=unused-argument
  30. if event == 'testevent':
  31. testevent_fired[0] = True
  32. emitter = qubes.events.Emitter()
  33. emitter.add_handler('testevent', on_testevent)
  34. emitter.events_enabled = True
  35. emitter.fire_event('testevent')
  36. self.assertTrue(testevent_fired[0])
  37. def test_001_decorator(self):
  38. class TestEmitter(qubes.events.Emitter):
  39. def __init__(self):
  40. # pylint: disable=bad-super-call
  41. super(TestEmitter, self).__init__()
  42. self.testevent_fired = False
  43. @qubes.events.handler('testevent')
  44. def on_testevent(self, event):
  45. if event == 'testevent':
  46. self.testevent_fired = True
  47. emitter = TestEmitter()
  48. emitter.events_enabled = True
  49. emitter.fire_event('testevent')
  50. self.assertTrue(emitter.testevent_fired)
  51. def test_002_fire_for_effect(self):
  52. class TestEmitter(qubes.events.Emitter):
  53. @qubes.events.handler('testevent')
  54. def on_testevent_1(self, event):
  55. pass
  56. @qubes.events.handler('testevent')
  57. def on_testevent_2(self, event):
  58. yield 'testvalue1'
  59. yield 'testvalue2'
  60. @qubes.events.handler('testevent')
  61. def on_testevent_3(self, event):
  62. return ('testvalue3', 'testvalue4')
  63. emitter = TestEmitter()
  64. emitter.events_enabled = True
  65. effect = emitter.fire_event('testevent')
  66. self.assertCountEqual(effect,
  67. ('testvalue1', 'testvalue2', 'testvalue3', 'testvalue4'))
  68. def test_004_catch_all(self):
  69. # need something mutable
  70. testevent_fired = [0]
  71. def on_all(subject, event, *args, **kwargs):
  72. # pylint: disable=unused-argument
  73. testevent_fired[0] += 1
  74. def on_foo(subject, event, *args, **kwargs):
  75. # pylint: disable=unused-argument
  76. testevent_fired[0] += 1
  77. emitter = qubes.events.Emitter()
  78. emitter.add_handler('*', on_all)
  79. emitter.add_handler('foo', on_foo)
  80. emitter.events_enabled = True
  81. emitter.fire_event('testevent')
  82. self.assertEqual(testevent_fired[0], 1)
  83. emitter.fire_event('foo')
  84. # now catch-all and foo should be executed
  85. self.assertEqual(testevent_fired[0], 3)
  86. emitter.fire_event('bar')
  87. self.assertEqual(testevent_fired[0], 4)
  88. def test_005_instance_handlers(self):
  89. class TestEmitter(qubes.events.Emitter):
  90. @qubes.events.handler('testevent')
  91. def on_testevent_1(self, event):
  92. yield 'testevent_1'
  93. def on_testevent_2(subject, event):
  94. yield 'testevent_2'
  95. emitter = TestEmitter()
  96. emitter.add_handler('testevent', on_testevent_2)
  97. emitter.events_enabled = True
  98. emitter2 = TestEmitter()
  99. emitter2.events_enabled = True
  100. with self.subTest('fire_event'):
  101. effect = emitter.fire_event('testevent')
  102. effect2 = emitter2.fire_event('testevent')
  103. self.assertEqual(list(effect),
  104. ['testevent_1', 'testevent_2'])
  105. self.assertEqual(list(effect2),
  106. ['testevent_1'])
  107. with self.subTest('fire_event_pre'):
  108. effect = emitter.fire_event('testevent', pre_event=True)
  109. effect2 = emitter2.fire_event('testevent', pre_event=True)
  110. self.assertEqual(list(effect),
  111. ['testevent_2', 'testevent_1'])
  112. self.assertEqual(list(effect2),
  113. ['testevent_1'])
  114. def test_005_fire_for_effect_async(self):
  115. class TestEmitter(qubes.events.Emitter):
  116. @qubes.events.handler('testevent')
  117. @asyncio.coroutine
  118. def on_testevent_1(self, event):
  119. pass
  120. @qubes.events.handler('testevent')
  121. @asyncio.coroutine
  122. def on_testevent_2(self, event):
  123. yield from asyncio.sleep(0.01)
  124. return ['testvalue1']
  125. @qubes.events.handler('testevent')
  126. @asyncio.coroutine
  127. def on_testevent_3(self, event):
  128. return ('testvalue2', 'testvalue3')
  129. @qubes.events.handler('testevent')
  130. def on_testevent_4(self, event):
  131. return ('testvalue4',)
  132. loop = asyncio.get_event_loop()
  133. emitter = TestEmitter()
  134. emitter.events_enabled = True
  135. effect = loop.run_until_complete(emitter.fire_event_async('testevent'))
  136. loop.close()
  137. asyncio.set_event_loop(None)
  138. self.assertCountEqual(effect,
  139. ('testvalue1', 'testvalue2', 'testvalue3', 'testvalue4'))