mirror of https://github.com/scrapy/scrapy.git
utils.signal: made send_catch_log function more robust (by using pydispatch.robust.sendRobust) and added unittests
This commit is contained in:
parent
f204da82d1
commit
499dc6677c
|
|
@ -0,0 +1,42 @@
|
|||
import unittest
|
||||
|
||||
from scrapy.xlib.pydispatch import dispatcher
|
||||
from scrapy.utils.signal import send_catch_log
|
||||
from scrapy import log
|
||||
|
||||
test_signal = object()
|
||||
|
||||
class SignalUtilsTest(unittest.TestCase):
|
||||
|
||||
def test_send_catch_log(self):
|
||||
handlers_called = set()
|
||||
|
||||
def test_handler_error(arg):
|
||||
handlers_called.add(test_handler_error)
|
||||
a = 1/0
|
||||
|
||||
def test_handler_check(arg):
|
||||
handlers_called.add(test_handler_check)
|
||||
assert arg == 'test'
|
||||
|
||||
def log_received(message, level):
|
||||
handlers_called.add(log_received)
|
||||
assert "test_handler_error" in message
|
||||
assert level == log.ERROR
|
||||
|
||||
dispatcher.connect(log_received, signal=log.logmessage_received)
|
||||
dispatcher.connect(test_handler_error, signal=test_signal)
|
||||
dispatcher.connect(test_handler_check, signal=test_signal)
|
||||
send_catch_log(test_signal, arg='test')
|
||||
|
||||
assert test_handler_error in handlers_called
|
||||
assert test_handler_check in handlers_called
|
||||
assert log_received in handlers_called
|
||||
|
||||
dispatcher.disconnect(log_received, signal=log.logmessage_received)
|
||||
dispatcher.disconnect(test_handler_error, signal=test_signal)
|
||||
dispatcher.disconnect(test_handler_check, signal=test_signal)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
@ -1,13 +1,14 @@
|
|||
"""Helper functinos for working with signals"""
|
||||
|
||||
from scrapy.xlib.pydispatch import dispatcher
|
||||
from scrapy.xlib.pydispatch.robust import sendRobust
|
||||
from scrapy import log
|
||||
|
||||
def send_catch_log(*args, **kwargs):
|
||||
"""Same as dispatcher.send but logs any exceptions raised by the signal
|
||||
handlers
|
||||
"""Same as dispatcher.robust.sendRobust but logs any exceptions raised by
|
||||
the signal handlers
|
||||
"""
|
||||
try:
|
||||
dispatcher.send(*args, **kwargs)
|
||||
except:
|
||||
log.exc("Exception catched on signal dispatch")
|
||||
for receiver, result in sendRobust(*args, **kwargs):
|
||||
if isinstance(result, Exception):
|
||||
log.msg("Exception caught on signal dispatch: receiver=%r, " \
|
||||
" exception=%r" % (receiver, result), level=log.ERROR)
|
||||
|
|
|
|||
Loading…
Reference in New Issue