2015-01-19 18:03:23 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2015-01-19 18:03:23 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-06-23 19:03:57 +02:00
|
|
|
import asyncio
|
2014-12-09 14:14:24 +01:00
|
|
|
|
|
|
|
import qubes.events
|
2015-01-05 14:41:59 +01:00
|
|
|
import qubes.tests
|
|
|
|
|
|
|
|
class TC_00_Emitter(qubes.tests.QubesTestCase):
|
2014-12-09 14:14:24 +01:00
|
|
|
def test_000_add_handler(self):
|
|
|
|
# need something mutable
|
|
|
|
testevent_fired = [False]
|
|
|
|
|
|
|
|
def on_testevent(subject, event):
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=unused-argument
|
2014-12-09 14:14:24 +01:00
|
|
|
if event == 'testevent':
|
|
|
|
testevent_fired[0] = True
|
|
|
|
|
|
|
|
emitter = qubes.events.Emitter()
|
|
|
|
emitter.add_handler('testevent', on_testevent)
|
2015-06-24 15:02:49 +02:00
|
|
|
emitter.events_enabled = True
|
2014-12-09 14:14:24 +01:00
|
|
|
emitter.fire_event('testevent')
|
|
|
|
self.assertTrue(testevent_fired[0])
|
|
|
|
|
|
|
|
|
|
|
|
def test_001_decorator(self):
|
|
|
|
class TestEmitter(qubes.events.Emitter):
|
|
|
|
def __init__(self):
|
2015-10-05 23:46:25 +02:00
|
|
|
# pylint: disable=bad-super-call
|
2014-12-09 14:14:24 +01:00
|
|
|
super(TestEmitter, self).__init__()
|
|
|
|
self.testevent_fired = False
|
|
|
|
|
|
|
|
@qubes.events.handler('testevent')
|
|
|
|
def on_testevent(self, event):
|
|
|
|
if event == 'testevent':
|
|
|
|
self.testevent_fired = True
|
|
|
|
|
|
|
|
emitter = TestEmitter()
|
2015-06-24 15:02:49 +02:00
|
|
|
emitter.events_enabled = True
|
2014-12-09 14:14:24 +01:00
|
|
|
emitter.fire_event('testevent')
|
|
|
|
self.assertTrue(emitter.testevent_fired)
|
2016-04-08 12:35:11 +02:00
|
|
|
|
|
|
|
def test_002_fire_for_effect(self):
|
|
|
|
class TestEmitter(qubes.events.Emitter):
|
|
|
|
@qubes.events.handler('testevent')
|
|
|
|
def on_testevent_1(self, event):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@qubes.events.handler('testevent')
|
|
|
|
def on_testevent_2(self, event):
|
|
|
|
yield 'testvalue1'
|
|
|
|
yield 'testvalue2'
|
|
|
|
|
|
|
|
@qubes.events.handler('testevent')
|
|
|
|
def on_testevent_3(self, event):
|
|
|
|
return ('testvalue3', 'testvalue4')
|
|
|
|
|
|
|
|
emitter = TestEmitter()
|
|
|
|
emitter.events_enabled = True
|
|
|
|
|
|
|
|
effect = emitter.fire_event('testevent')
|
|
|
|
|
2017-01-18 22:16:46 +01:00
|
|
|
self.assertCountEqual(effect,
|
2016-04-08 12:35:11 +02:00
|
|
|
('testvalue1', 'testvalue2', 'testvalue3', 'testvalue4'))
|
2016-09-19 22:49:24 +02:00
|
|
|
|
|
|
|
def test_004_catch_all(self):
|
|
|
|
# need something mutable
|
|
|
|
testevent_fired = [0]
|
|
|
|
|
|
|
|
def on_all(subject, event, *args, **kwargs):
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
testevent_fired[0] += 1
|
|
|
|
|
|
|
|
def on_foo(subject, event, *args, **kwargs):
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
testevent_fired[0] += 1
|
|
|
|
|
|
|
|
emitter = qubes.events.Emitter()
|
|
|
|
emitter.add_handler('*', on_all)
|
|
|
|
emitter.add_handler('foo', on_foo)
|
|
|
|
emitter.events_enabled = True
|
|
|
|
emitter.fire_event('testevent')
|
|
|
|
self.assertEqual(testevent_fired[0], 1)
|
|
|
|
emitter.fire_event('foo')
|
|
|
|
# now catch-all and foo should be executed
|
|
|
|
self.assertEqual(testevent_fired[0], 3)
|
|
|
|
emitter.fire_event('bar')
|
|
|
|
self.assertEqual(testevent_fired[0], 4)
|
2017-05-12 13:53:30 +02:00
|
|
|
|
|
|
|
def test_005_instance_handlers(self):
|
|
|
|
class TestEmitter(qubes.events.Emitter):
|
|
|
|
@qubes.events.handler('testevent')
|
|
|
|
def on_testevent_1(self, event):
|
|
|
|
yield 'testevent_1'
|
|
|
|
|
|
|
|
def on_testevent_2(subject, event):
|
|
|
|
yield 'testevent_2'
|
|
|
|
|
|
|
|
emitter = TestEmitter()
|
|
|
|
emitter.add_handler('testevent', on_testevent_2)
|
|
|
|
emitter.events_enabled = True
|
|
|
|
|
|
|
|
emitter2 = TestEmitter()
|
|
|
|
emitter2.events_enabled = True
|
|
|
|
|
|
|
|
with self.subTest('fire_event'):
|
|
|
|
effect = emitter.fire_event('testevent')
|
|
|
|
effect2 = emitter2.fire_event('testevent')
|
|
|
|
self.assertEqual(list(effect),
|
|
|
|
['testevent_1', 'testevent_2'])
|
|
|
|
self.assertEqual(list(effect2),
|
|
|
|
['testevent_1'])
|
|
|
|
|
|
|
|
with self.subTest('fire_event_pre'):
|
2017-06-23 17:29:09 +02:00
|
|
|
effect = emitter.fire_event('testevent', pre_event=True)
|
|
|
|
effect2 = emitter2.fire_event('testevent', pre_event=True)
|
2017-05-12 13:53:30 +02:00
|
|
|
self.assertEqual(list(effect),
|
|
|
|
['testevent_2', 'testevent_1'])
|
|
|
|
self.assertEqual(list(effect2),
|
|
|
|
['testevent_1'])
|
2017-06-23 19:03:57 +02:00
|
|
|
|
|
|
|
def test_005_fire_for_effect_async(self):
|
|
|
|
class TestEmitter(qubes.events.Emitter):
|
|
|
|
@qubes.events.handler('testevent')
|
|
|
|
@asyncio.coroutine
|
|
|
|
def on_testevent_1(self, event):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@qubes.events.handler('testevent')
|
|
|
|
@asyncio.coroutine
|
|
|
|
def on_testevent_2(self, event):
|
|
|
|
yield from asyncio.sleep(0.01)
|
|
|
|
return ['testvalue1']
|
|
|
|
|
|
|
|
@qubes.events.handler('testevent')
|
|
|
|
@asyncio.coroutine
|
|
|
|
def on_testevent_3(self, event):
|
|
|
|
return ('testvalue2', 'testvalue3')
|
|
|
|
|
|
|
|
@qubes.events.handler('testevent')
|
|
|
|
def on_testevent_4(self, event):
|
|
|
|
return ('testvalue4',)
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
emitter = TestEmitter()
|
|
|
|
emitter.events_enabled = True
|
|
|
|
|
|
|
|
effect = loop.run_until_complete(emitter.fire_event_async('testevent'))
|
|
|
|
|
|
|
|
self.assertCountEqual(effect,
|
|
|
|
('testvalue1', 'testvalue2', 'testvalue3', 'testvalue4'))
|