TestEventDispatcher.py 2.05 KB
import unittest
import mock

from os.path import dirname, realpath
from sys import argv, path
path.append(dirname(dirname(realpath(__file__))) + '/lib')

from Event.Event import Event
from Event.EventDispatcher import EventDispatcher

class TestEventDisptcher(unittest.TestCase):
    def setUp(self):
        self._dispatcher    = EventDispatcher()
        self._handler_mock1 = mock.Mock()
        self._handler_mock2 = mock.Mock()

        self._handler_mock1.getHandledIds.return_value = [1, 2]
        self._handler_mock2.getHandledIds.return_value = [1, 3]

    def testRegisterHandler(self):
        self._dispatcher.registerHandler(self._handler_mock1)
        self._dispatcher.registerHandler(self._handler_mock2)

        self._handler_mock1.getHandledIds.called_once()
        self._handler_mock2.getHandledIds.called_once()

        self._handler_mock1.setDispatcher.called_once()
        self._handler_mock2.setDispatcher.called_once()

        self.assertIn(1, self._dispatcher._handler)
        self.assertIn(2, self._dispatcher._handler)
        self.assertIn(3, self._dispatcher._handler)
        self.assertNotIn(4, self._dispatcher._handler)
        self.assertIn(self._handler_mock1, self._dispatcher._handler[1])
        self.assertIn(self._handler_mock2, self._dispatcher._handler[1])
        self.assertIn(self._handler_mock1, self._dispatcher._handler[2])
        self.assertNotIn(self._handler_mock2, self._dispatcher._handler[2])
        self.assertIn(self._handler_mock2, self._dispatcher._handler[3])

    def testSetHeartbeat(self):
        self._dispatcher.setHeartbeat(None)
        self.assertEqual(self._dispatcher._heartbeat, None)
        self.assertEqual(self._dispatcher._nextbeat, 0.0)
        self._dispatcher.setHeartbeat(1.0)
        self.assertEqual(self._dispatcher._heartbeat, 1.0)
        self.assertNotEqual(self._dispatcher._nextbeat, 0.0)

def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TestEventDisptcher)

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())

# vim: set ft=python et ts=8 sw=4 sts=4: