2008-07-20 14:32:54 +08:00
|
|
|
import gc
|
2010-10-11 20:55:17 +08:00
|
|
|
import sys
|
2011-10-14 02:51:33 +08:00
|
|
|
import time
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2013-08-20 11:14:21 +08:00
|
|
|
import weakref
|
2013-10-30 19:11:04 +08:00
|
|
|
from types import TracebackType
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2012-06-08 18:00:51 +08:00
|
|
|
from django.dispatch import Signal, receiver
|
2008-07-20 14:32:54 +08:00
|
|
|
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2008-07-20 14:32:54 +08:00
|
|
|
if sys.platform.startswith('java'):
|
|
|
|
def garbage_collect():
|
2012-01-02 05:11:05 +08:00
|
|
|
# Some JVM GCs will execute finalizers in a different thread, meaning
|
|
|
|
# we need to wait for that to complete before we go on looking for the
|
|
|
|
# effects of that.
|
2008-07-20 14:32:54 +08:00
|
|
|
gc.collect()
|
|
|
|
time.sleep(0.1)
|
2012-01-02 05:11:05 +08:00
|
|
|
elif hasattr(sys, "pypy_version_info"):
|
|
|
|
def garbage_collect():
|
|
|
|
# Collecting weakreferences can take two collections on PyPy.
|
|
|
|
gc.collect()
|
|
|
|
gc.collect()
|
2008-07-20 14:32:54 +08:00
|
|
|
else:
|
|
|
|
def garbage_collect():
|
|
|
|
gc.collect()
|
2007-02-26 11:17:04 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
def receiver_1_arg(val, **kwargs):
|
|
|
|
return val
|
2007-02-26 11:17:04 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2007-02-26 11:17:04 +08:00
|
|
|
class Callable(object):
|
2008-08-06 23:32:46 +08:00
|
|
|
def __call__(self, val, **kwargs):
|
|
|
|
return val
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
def a(self, val, **kwargs):
|
|
|
|
return val
|
|
|
|
|
|
|
|
a_signal = Signal(providing_args=["val"])
|
2012-06-08 18:00:51 +08:00
|
|
|
b_signal = Signal(providing_args=["val"])
|
|
|
|
c_signal = Signal(providing_args=["val"])
|
2013-08-20 11:14:21 +08:00
|
|
|
d_signal = Signal(providing_args=["val"], use_caching=True)
|
|
|
|
|
2007-02-26 11:17:04 +08:00
|
|
|
|
|
|
|
class DispatcherTests(unittest.TestCase):
|
|
|
|
"""Test suite for dispatcher (barely started)"""
|
2008-08-06 23:32:46 +08:00
|
|
|
|
2015-01-03 00:29:06 +08:00
|
|
|
def assertTestIsClean(self, signal):
|
2007-02-26 11:17:04 +08:00
|
|
|
"""Assert that everything has been cleaned up automatically"""
|
2014-02-05 02:19:14 +08:00
|
|
|
# Note that dead weakref cleanup happens as side effect of using
|
|
|
|
# the signal's receivers through the signals API. So, first do a
|
|
|
|
# call to an API method to force cleanup.
|
|
|
|
self.assertFalse(signal.has_listeners())
|
2008-08-06 23:32:46 +08:00
|
|
|
self.assertEqual(signal.receivers, [])
|
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_exact(self):
|
2008-08-06 23:32:46 +08:00
|
|
|
a_signal.connect(receiver_1_arg, sender=self)
|
2013-10-27 03:15:03 +08:00
|
|
|
expected = [(receiver_1_arg, "test")]
|
2008-08-06 23:32:46 +08:00
|
|
|
result = a_signal.send(sender=self, val="test")
|
2007-02-26 11:44:36 +08:00
|
|
|
self.assertEqual(result, expected)
|
2008-08-06 23:32:46 +08:00
|
|
|
a_signal.disconnect(receiver_1_arg, sender=self)
|
2015-01-03 00:29:06 +08:00
|
|
|
self.assertTestIsClean(a_signal)
|
2008-08-06 23:32:46 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_ignored_sender(self):
|
2008-08-06 23:32:46 +08:00
|
|
|
a_signal.connect(receiver_1_arg)
|
2013-10-27 03:15:03 +08:00
|
|
|
expected = [(receiver_1_arg, "test")]
|
2008-08-06 23:32:46 +08:00
|
|
|
result = a_signal.send(sender=self, val="test")
|
2007-02-26 11:44:36 +08:00
|
|
|
self.assertEqual(result, expected)
|
2008-08-06 23:32:46 +08:00
|
|
|
a_signal.disconnect(receiver_1_arg)
|
2015-01-03 00:29:06 +08:00
|
|
|
self.assertTestIsClean(a_signal)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_garbage_collected(self):
|
2007-02-26 11:17:04 +08:00
|
|
|
a = Callable()
|
2008-08-06 23:32:46 +08:00
|
|
|
a_signal.connect(a.a, sender=self)
|
2007-02-26 11:17:04 +08:00
|
|
|
expected = []
|
|
|
|
del a
|
2008-07-20 14:32:54 +08:00
|
|
|
garbage_collect()
|
2008-08-06 23:32:46 +08:00
|
|
|
result = a_signal.send(sender=self, val="test")
|
2007-02-26 11:17:04 +08:00
|
|
|
self.assertEqual(result, expected)
|
2015-01-03 00:29:06 +08:00
|
|
|
self.assertTestIsClean(a_signal)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_cached_garbaged_collected(self):
|
2013-08-20 11:14:21 +08:00
|
|
|
"""
|
|
|
|
Make sure signal caching sender receivers don't prevent garbage
|
|
|
|
collection of senders.
|
|
|
|
"""
|
|
|
|
class sender:
|
|
|
|
pass
|
|
|
|
wref = weakref.ref(sender)
|
|
|
|
d_signal.connect(receiver_1_arg)
|
|
|
|
d_signal.send(sender, val='garbage')
|
|
|
|
del sender
|
|
|
|
garbage_collect()
|
|
|
|
try:
|
|
|
|
self.assertIsNone(wref())
|
|
|
|
finally:
|
|
|
|
# Disconnect after reference check since it flushes the tested cache.
|
|
|
|
d_signal.disconnect(receiver_1_arg)
|
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_multiple_registration(self):
|
2007-02-26 11:17:04 +08:00
|
|
|
a = Callable()
|
2008-08-06 23:32:46 +08:00
|
|
|
a_signal.connect(a)
|
|
|
|
a_signal.connect(a)
|
|
|
|
a_signal.connect(a)
|
|
|
|
a_signal.connect(a)
|
|
|
|
a_signal.connect(a)
|
|
|
|
a_signal.connect(a)
|
|
|
|
result = a_signal.send(sender=self, val="test")
|
2007-02-26 11:44:36 +08:00
|
|
|
self.assertEqual(len(result), 1)
|
2008-08-06 23:32:46 +08:00
|
|
|
self.assertEqual(len(a_signal.receivers), 1)
|
2007-02-26 11:17:04 +08:00
|
|
|
del a
|
|
|
|
del result
|
2008-07-20 14:32:54 +08:00
|
|
|
garbage_collect()
|
2015-01-03 00:29:06 +08:00
|
|
|
self.assertTestIsClean(a_signal)
|
2008-08-06 23:32:46 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_uid_registration(self):
|
2008-08-06 23:32:46 +08:00
|
|
|
def uid_based_receiver_1(**kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def uid_based_receiver_2(**kwargs):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
a_signal.connect(uid_based_receiver_1, dispatch_uid="uid")
|
|
|
|
a_signal.connect(uid_based_receiver_2, dispatch_uid="uid")
|
2008-08-06 23:32:46 +08:00
|
|
|
self.assertEqual(len(a_signal.receivers), 1)
|
2013-11-03 12:36:09 +08:00
|
|
|
a_signal.disconnect(dispatch_uid="uid")
|
2015-01-03 00:29:06 +08:00
|
|
|
self.assertTestIsClean(a_signal)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_robust(self):
|
2015-01-03 00:29:06 +08:00
|
|
|
"""Test the send_robust() function"""
|
2008-08-06 23:32:46 +08:00
|
|
|
def fails(val, **kwargs):
|
2007-02-26 11:44:36 +08:00
|
|
|
raise ValueError('this')
|
2008-08-06 23:32:46 +08:00
|
|
|
a_signal.connect(fails)
|
|
|
|
result = a_signal.send_robust(sender=self, val="test")
|
2007-02-26 11:17:04 +08:00
|
|
|
err = result[0][1]
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(err, ValueError)
|
2007-02-26 11:44:36 +08:00
|
|
|
self.assertEqual(err.args, ('this',))
|
2013-10-30 19:11:04 +08:00
|
|
|
self.assertTrue(hasattr(err, '__traceback__'))
|
|
|
|
self.assertTrue(isinstance(err.__traceback__, TracebackType))
|
2008-08-06 23:32:46 +08:00
|
|
|
a_signal.disconnect(fails)
|
2015-01-03 00:29:06 +08:00
|
|
|
self.assertTestIsClean(a_signal)
|
2007-02-26 11:17:04 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_disconnection(self):
|
2009-04-11 02:58:32 +08:00
|
|
|
receiver_1 = Callable()
|
|
|
|
receiver_2 = Callable()
|
|
|
|
receiver_3 = Callable()
|
|
|
|
a_signal.connect(receiver_1)
|
|
|
|
a_signal.connect(receiver_2)
|
|
|
|
a_signal.connect(receiver_3)
|
|
|
|
a_signal.disconnect(receiver_1)
|
|
|
|
del receiver_2
|
|
|
|
garbage_collect()
|
|
|
|
a_signal.disconnect(receiver_3)
|
2015-01-03 00:29:06 +08:00
|
|
|
self.assertTestIsClean(a_signal)
|
2012-06-08 18:00:51 +08:00
|
|
|
|
2012-09-20 08:18:19 +08:00
|
|
|
def test_has_listeners(self):
|
2012-09-20 23:51:30 +08:00
|
|
|
self.assertFalse(a_signal.has_listeners())
|
|
|
|
self.assertFalse(a_signal.has_listeners(sender=object()))
|
2012-09-20 08:18:19 +08:00
|
|
|
receiver_1 = Callable()
|
|
|
|
a_signal.connect(receiver_1)
|
2012-09-20 23:51:30 +08:00
|
|
|
self.assertTrue(a_signal.has_listeners())
|
|
|
|
self.assertTrue(a_signal.has_listeners(sender=object()))
|
2012-09-20 08:18:19 +08:00
|
|
|
a_signal.disconnect(receiver_1)
|
2012-09-20 23:51:30 +08:00
|
|
|
self.assertFalse(a_signal.has_listeners())
|
|
|
|
self.assertFalse(a_signal.has_listeners(sender=object()))
|
2012-09-20 08:18:19 +08:00
|
|
|
|
2012-06-08 18:00:51 +08:00
|
|
|
|
|
|
|
class ReceiverTestCase(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Test suite for receiver.
|
|
|
|
"""
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_receiver_single_signal(self):
|
2012-06-08 18:00:51 +08:00
|
|
|
@receiver(a_signal)
|
|
|
|
def f(val, **kwargs):
|
|
|
|
self.state = val
|
|
|
|
self.state = False
|
|
|
|
a_signal.send(sender=self, val=True)
|
|
|
|
self.assertTrue(self.state)
|
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_receiver_signal_list(self):
|
2012-06-08 18:00:51 +08:00
|
|
|
@receiver([a_signal, b_signal, c_signal])
|
|
|
|
def f(val, **kwargs):
|
|
|
|
self.state.append(val)
|
|
|
|
self.state = []
|
|
|
|
a_signal.send(sender=self, val='a')
|
|
|
|
c_signal.send(sender=self, val='c')
|
|
|
|
b_signal.send(sender=self, val='b')
|
|
|
|
self.assertIn('a', self.state)
|
|
|
|
self.assertIn('b', self.state)
|
|
|
|
self.assertIn('c', self.state)
|