2008-07-20 06:32:54 +00:00
|
|
|
import gc
|
2010-10-11 12:55:17 +00:00
|
|
|
import sys
|
2013-08-20 03:14:21 +00:00
|
|
|
import weakref
|
2013-10-30 11:11:04 +00:00
|
|
|
from types import TracebackType
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2012-06-08 10:00:51 +00:00
|
|
|
from django.dispatch import Signal, receiver
|
2016-11-20 18:02:10 +00:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.test.utils import override_settings
|
2008-07-20 06:32:54 +00:00
|
|
|
|
2019-11-06 15:08:12 +00:00
|
|
|
if hasattr(sys, 'pypy_version_info'):
|
2012-01-01 21:11:05 +00:00
|
|
|
def garbage_collect():
|
|
|
|
# Collecting weakreferences can take two collections on PyPy.
|
|
|
|
gc.collect()
|
|
|
|
gc.collect()
|
2008-07-20 06:32:54 +00:00
|
|
|
else:
|
|
|
|
def garbage_collect():
|
|
|
|
gc.collect()
|
2007-02-26 03:17:04 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2008-08-06 15:32:46 +00:00
|
|
|
def receiver_1_arg(val, **kwargs):
|
|
|
|
return val
|
2007-02-26 03:17:04 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class Callable:
|
2008-08-06 15:32:46 +00:00
|
|
|
def __call__(self, val, **kwargs):
|
|
|
|
return val
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2008-08-06 15:32:46 +00:00
|
|
|
def a(self, val, **kwargs):
|
|
|
|
return val
|
|
|
|
|
2016-11-12 17:11:23 +00:00
|
|
|
|
2020-03-01 17:22:03 +00:00
|
|
|
a_signal = Signal()
|
|
|
|
b_signal = Signal()
|
|
|
|
c_signal = Signal()
|
|
|
|
d_signal = Signal(use_caching=True)
|
2013-08-20 03:14:21 +00:00
|
|
|
|
2007-02-26 03:17:04 +00:00
|
|
|
|
2016-11-20 18:02:10 +00:00
|
|
|
class DispatcherTests(SimpleTestCase):
|
2008-08-06 15:32:46 +00:00
|
|
|
|
2015-01-02 16:29:06 +00:00
|
|
|
def assertTestIsClean(self, signal):
|
2007-02-26 03:17:04 +00:00
|
|
|
"""Assert that everything has been cleaned up automatically"""
|
2014-02-04 18:19:14 +00: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 15:32:46 +00:00
|
|
|
self.assertEqual(signal.receivers, [])
|
|
|
|
|
2016-11-20 18:02:10 +00:00
|
|
|
@override_settings(DEBUG=True)
|
|
|
|
def test_cannot_connect_no_kwargs(self):
|
|
|
|
def receiver_no_kwargs(sender):
|
|
|
|
pass
|
|
|
|
|
|
|
|
msg = 'Signal receivers must accept keyword arguments (**kwargs).'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
a_signal.connect(receiver_no_kwargs)
|
|
|
|
self.assertTestIsClean(a_signal)
|
|
|
|
|
|
|
|
@override_settings(DEBUG=True)
|
|
|
|
def test_cannot_connect_non_callable(self):
|
|
|
|
msg = 'Signal receivers must be callable.'
|
2021-06-20 18:16:33 +00:00
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
2016-11-20 18:02:10 +00:00
|
|
|
a_signal.connect(object())
|
|
|
|
self.assertTestIsClean(a_signal)
|
|
|
|
|
|
|
|
def test_send(self):
|
2008-08-06 15:32:46 +00:00
|
|
|
a_signal.connect(receiver_1_arg, sender=self)
|
2016-11-20 18:02:10 +00:00
|
|
|
result = a_signal.send(sender=self, val='test')
|
|
|
|
self.assertEqual(result, [(receiver_1_arg, 'test')])
|
2008-08-06 15:32:46 +00:00
|
|
|
a_signal.disconnect(receiver_1_arg, sender=self)
|
2015-01-02 16:29:06 +00:00
|
|
|
self.assertTestIsClean(a_signal)
|
2008-08-06 15:32:46 +00:00
|
|
|
|
2016-11-20 18:02:10 +00:00
|
|
|
def test_send_no_receivers(self):
|
|
|
|
result = a_signal.send(sender=self, val='test')
|
|
|
|
self.assertEqual(result, [])
|
|
|
|
|
|
|
|
def test_send_connected_no_sender(self):
|
2008-08-06 15:32:46 +00:00
|
|
|
a_signal.connect(receiver_1_arg)
|
2016-11-20 18:02:10 +00:00
|
|
|
result = a_signal.send(sender=self, val='test')
|
|
|
|
self.assertEqual(result, [(receiver_1_arg, 'test')])
|
2008-08-06 15:32:46 +00:00
|
|
|
a_signal.disconnect(receiver_1_arg)
|
2015-01-02 16:29:06 +00:00
|
|
|
self.assertTestIsClean(a_signal)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2016-11-20 18:02:10 +00:00
|
|
|
def test_send_different_no_sender(self):
|
|
|
|
a_signal.connect(receiver_1_arg, sender=object)
|
|
|
|
result = a_signal.send(sender=self, val='test')
|
|
|
|
self.assertEqual(result, [])
|
|
|
|
a_signal.disconnect(receiver_1_arg, sender=object)
|
|
|
|
self.assertTestIsClean(a_signal)
|
|
|
|
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_garbage_collected(self):
|
2007-02-26 03:17:04 +00:00
|
|
|
a = Callable()
|
2008-08-06 15:32:46 +00:00
|
|
|
a_signal.connect(a.a, sender=self)
|
2007-02-26 03:17:04 +00:00
|
|
|
del a
|
2008-07-20 06:32:54 +00:00
|
|
|
garbage_collect()
|
2008-08-06 15:32:46 +00:00
|
|
|
result = a_signal.send(sender=self, val="test")
|
2016-11-20 18:02:10 +00:00
|
|
|
self.assertEqual(result, [])
|
2015-01-02 16:29:06 +00:00
|
|
|
self.assertTestIsClean(a_signal)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_cached_garbaged_collected(self):
|
2013-08-20 03:14:21 +00: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-07 23:08:42 +00:00
|
|
|
def test_multiple_registration(self):
|
2007-02-26 03:17:04 +00:00
|
|
|
a = Callable()
|
2008-08-06 15:32:46 +00: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 03:44:36 +00:00
|
|
|
self.assertEqual(len(result), 1)
|
2008-08-06 15:32:46 +00:00
|
|
|
self.assertEqual(len(a_signal.receivers), 1)
|
2007-02-26 03:17:04 +00:00
|
|
|
del a
|
|
|
|
del result
|
2008-07-20 06:32:54 +00:00
|
|
|
garbage_collect()
|
2015-01-02 16:29:06 +00:00
|
|
|
self.assertTestIsClean(a_signal)
|
2008-08-06 15:32:46 +00:00
|
|
|
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_uid_registration(self):
|
2008-08-06 15:32:46 +00:00
|
|
|
def uid_based_receiver_1(**kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def uid_based_receiver_2(**kwargs):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
a_signal.connect(uid_based_receiver_1, dispatch_uid="uid")
|
|
|
|
a_signal.connect(uid_based_receiver_2, dispatch_uid="uid")
|
2008-08-06 15:32:46 +00:00
|
|
|
self.assertEqual(len(a_signal.receivers), 1)
|
2013-11-03 04:36:09 +00:00
|
|
|
a_signal.disconnect(dispatch_uid="uid")
|
2015-01-02 16:29:06 +00:00
|
|
|
self.assertTestIsClean(a_signal)
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2016-11-20 18:02:10 +00:00
|
|
|
def test_send_robust_success(self):
|
|
|
|
a_signal.connect(receiver_1_arg)
|
|
|
|
result = a_signal.send_robust(sender=self, val='test')
|
|
|
|
self.assertEqual(result, [(receiver_1_arg, 'test')])
|
|
|
|
a_signal.disconnect(receiver_1_arg)
|
|
|
|
self.assertTestIsClean(a_signal)
|
|
|
|
|
|
|
|
def test_send_robust_no_receivers(self):
|
|
|
|
result = a_signal.send_robust(sender=self, val='test')
|
|
|
|
self.assertEqual(result, [])
|
|
|
|
|
|
|
|
def test_send_robust_ignored_sender(self):
|
|
|
|
a_signal.connect(receiver_1_arg)
|
|
|
|
result = a_signal.send_robust(sender=self, val='test')
|
|
|
|
self.assertEqual(result, [(receiver_1_arg, 'test')])
|
|
|
|
a_signal.disconnect(receiver_1_arg)
|
|
|
|
self.assertTestIsClean(a_signal)
|
|
|
|
|
|
|
|
def test_send_robust_fail(self):
|
2008-08-06 15:32:46 +00:00
|
|
|
def fails(val, **kwargs):
|
2007-02-26 03:44:36 +00:00
|
|
|
raise ValueError('this')
|
2008-08-06 15:32:46 +00:00
|
|
|
a_signal.connect(fails)
|
2020-12-12 07:28:43 +00:00
|
|
|
try:
|
|
|
|
with self.assertLogs('django.dispatch', 'ERROR') as cm:
|
|
|
|
result = a_signal.send_robust(sender=self, val='test')
|
|
|
|
err = result[0][1]
|
|
|
|
self.assertIsInstance(err, ValueError)
|
|
|
|
self.assertEqual(err.args, ('this',))
|
|
|
|
self.assertIs(hasattr(err, '__traceback__'), True)
|
|
|
|
self.assertIsInstance(err.__traceback__, TracebackType)
|
|
|
|
|
|
|
|
log_record = cm.records[0]
|
|
|
|
self.assertEqual(
|
|
|
|
log_record.getMessage(),
|
|
|
|
'Error calling '
|
|
|
|
'DispatcherTests.test_send_robust_fail.<locals>.fails in '
|
|
|
|
'Signal.send_robust() (this)',
|
|
|
|
)
|
|
|
|
self.assertIsNotNone(log_record.exc_info)
|
|
|
|
_, exc_value, _ = log_record.exc_info
|
|
|
|
self.assertIsInstance(exc_value, ValueError)
|
|
|
|
self.assertEqual(str(exc_value), 'this')
|
|
|
|
finally:
|
|
|
|
a_signal.disconnect(fails)
|
2015-01-02 16:29:06 +00:00
|
|
|
self.assertTestIsClean(a_signal)
|
2007-02-26 03:17:04 +00:00
|
|
|
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_disconnection(self):
|
2009-04-10 18:58:32 +00: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-02 16:29:06 +00:00
|
|
|
self.assertTestIsClean(a_signal)
|
2012-06-08 10:00:51 +00:00
|
|
|
|
2015-01-02 14:25:33 +00:00
|
|
|
def test_values_returned_by_disconnection(self):
|
|
|
|
receiver_1 = Callable()
|
|
|
|
receiver_2 = Callable()
|
|
|
|
a_signal.connect(receiver_1)
|
|
|
|
receiver_1_disconnected = a_signal.disconnect(receiver_1)
|
|
|
|
receiver_2_disconnected = a_signal.disconnect(receiver_2)
|
|
|
|
self.assertTrue(receiver_1_disconnected)
|
|
|
|
self.assertFalse(receiver_2_disconnected)
|
|
|
|
self.assertTestIsClean(a_signal)
|
|
|
|
|
2012-09-20 00:18:19 +00:00
|
|
|
def test_has_listeners(self):
|
2012-09-20 15:51:30 +00:00
|
|
|
self.assertFalse(a_signal.has_listeners())
|
|
|
|
self.assertFalse(a_signal.has_listeners(sender=object()))
|
2012-09-20 00:18:19 +00:00
|
|
|
receiver_1 = Callable()
|
|
|
|
a_signal.connect(receiver_1)
|
2012-09-20 15:51:30 +00:00
|
|
|
self.assertTrue(a_signal.has_listeners())
|
|
|
|
self.assertTrue(a_signal.has_listeners(sender=object()))
|
2012-09-20 00:18:19 +00:00
|
|
|
a_signal.disconnect(receiver_1)
|
2012-09-20 15:51:30 +00:00
|
|
|
self.assertFalse(a_signal.has_listeners())
|
|
|
|
self.assertFalse(a_signal.has_listeners(sender=object()))
|
2012-09-20 00:18:19 +00:00
|
|
|
|
2012-06-08 10:00:51 +00:00
|
|
|
|
2016-11-20 18:02:10 +00:00
|
|
|
class ReceiverTestCase(SimpleTestCase):
|
|
|
|
|
2014-07-07 23:08:42 +00:00
|
|
|
def test_receiver_single_signal(self):
|
2012-06-08 10:00:51 +00: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-07 23:08:42 +00:00
|
|
|
def test_receiver_signal_list(self):
|
2012-06-08 10:00:51 +00: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)
|