PageRenderTime 50ms CodeModel.GetById 35ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/dispatch/tests/test_dispatcher.py

https://code.google.com/p/mango-py/
Python | 124 lines | 99 code | 20 blank | 5 comment | 2 complexity | c6f61d91185a08c86356584ac1faa0cf MD5 | raw file
  1import gc
  2import sys
  3
  4from django.dispatch import Signal
  5from django.utils import unittest
  6import django.utils.copycompat as copy
  7
  8if sys.platform.startswith('java'):
  9    def garbage_collect():
 10        """Run the garbage collector and wait a bit to let it do his work"""
 11        import time
 12        gc.collect()
 13        time.sleep(0.1)
 14else:
 15    def garbage_collect():
 16        gc.collect()
 17
 18def receiver_1_arg(val, **kwargs):
 19    return val
 20
 21class Callable(object):
 22    def __call__(self, val, **kwargs):
 23        return val
 24
 25    def a(self, val, **kwargs):
 26        return val
 27
 28a_signal = Signal(providing_args=["val"])
 29
 30class DispatcherTests(unittest.TestCase):
 31    """Test suite for dispatcher (barely started)"""
 32
 33    def _testIsClean(self, signal):
 34        """Assert that everything has been cleaned up automatically"""
 35        self.assertEqual(signal.receivers, [])
 36
 37        # force cleanup just in case
 38        signal.receivers = []
 39
 40    def testExact(self):
 41        a_signal.connect(receiver_1_arg, sender=self)
 42        expected = [(receiver_1_arg,"test")]
 43        result = a_signal.send(sender=self, val="test")
 44        self.assertEqual(result, expected)
 45        a_signal.disconnect(receiver_1_arg, sender=self)
 46        self._testIsClean(a_signal)
 47
 48    def testIgnoredSender(self):
 49        a_signal.connect(receiver_1_arg)
 50        expected = [(receiver_1_arg,"test")]
 51        result = a_signal.send(sender=self, val="test")
 52        self.assertEqual(result, expected)
 53        a_signal.disconnect(receiver_1_arg)
 54        self._testIsClean(a_signal)
 55
 56    def testGarbageCollected(self):
 57        a = Callable()
 58        a_signal.connect(a.a, sender=self)
 59        expected = []
 60        del a
 61        garbage_collect()
 62        result = a_signal.send(sender=self, val="test")
 63        self.assertEqual(result, expected)
 64        self._testIsClean(a_signal)
 65
 66    def testMultipleRegistration(self):
 67        a = Callable()
 68        a_signal.connect(a)
 69        a_signal.connect(a)
 70        a_signal.connect(a)
 71        a_signal.connect(a)
 72        a_signal.connect(a)
 73        a_signal.connect(a)
 74        result = a_signal.send(sender=self, val="test")
 75        self.assertEqual(len(result), 1)
 76        self.assertEqual(len(a_signal.receivers), 1)
 77        del a
 78        del result
 79        garbage_collect()
 80        self._testIsClean(a_signal)
 81
 82    def testUidRegistration(self):
 83        def uid_based_receiver_1(**kwargs):
 84            pass
 85
 86        def uid_based_receiver_2(**kwargs):
 87            pass
 88
 89        a_signal.connect(uid_based_receiver_1, dispatch_uid = "uid")
 90        a_signal.connect(uid_based_receiver_2, dispatch_uid = "uid")
 91        self.assertEqual(len(a_signal.receivers), 1)
 92        a_signal.disconnect(dispatch_uid = "uid")
 93        self._testIsClean(a_signal)
 94
 95    def testRobust(self):
 96        """Test the sendRobust function"""
 97        def fails(val, **kwargs):
 98            raise ValueError('this')
 99        a_signal.connect(fails)
100        result = a_signal.send_robust(sender=self, val="test")
101        err = result[0][1]
102        self.assertTrue(isinstance(err, ValueError))
103        self.assertEqual(err.args, ('this',))
104        a_signal.disconnect(fails)
105        self._testIsClean(a_signal)
106
107    def testDisconnection(self):
108        receiver_1 = Callable()
109        receiver_2 = Callable()
110        receiver_3 = Callable()
111        a_signal.connect(receiver_1)
112        a_signal.connect(receiver_2)
113        a_signal.connect(receiver_3)
114        a_signal.disconnect(receiver_1)
115        del receiver_2
116        garbage_collect()
117        a_signal.disconnect(receiver_3)
118        self._testIsClean(a_signal)
119
120def getSuite():
121    return unittest.makeSuite(DispatcherTests,'test')
122
123if __name__ == "__main__":
124    unittest.main()