Example #1
0
    def test_send_catch_log(self):
        test_signal = object()
        handlers_called = set()

        def log_received(event):
            handlers_called.add(log_received)
            assert "error_handler" in event['message'][0]
            assert event['logLevel'] == log.ERROR

        txlog.addObserver(log_received)
        dispatcher.connect(self.error_handler, signal=test_signal)
        dispatcher.connect(self.ok_handler, signal=test_signal)
        result = yield defer.maybeDeferred(self._get_result, test_signal, arg='test', \
            handlers_called=handlers_called)

        assert self.error_handler in handlers_called
        assert self.ok_handler in handlers_called
        assert log_received in handlers_called
        self.assertEqual(result[0][0], self.error_handler)
        self.assert_(isinstance(result[0][1], Failure))
        self.assertEqual(result[1], (self.ok_handler, "OK"))

        txlog.removeObserver(log_received)
        self.flushLoggedErrors()
        dispatcher.disconnect(self.error_handler, signal=test_signal)
        dispatcher.disconnect(self.ok_handler, signal=test_signal)
Example #2
0
    def test_send_catch_log(self):
        test_signal = object()
        handlers_called = set()

        def log_received(event):
            handlers_called.add(log_received)
            assert "error_handler" in event['message'][0]
            assert event['logLevel'] == log.ERROR

        txlog.addObserver(log_received)
        dispatcher.connect(self.error_handler, signal=test_signal)
        dispatcher.connect(self.ok_handler, signal=test_signal)
        result = yield defer.maybeDeferred(self._get_result, test_signal, arg='test',
                                           handlers_called=handlers_called)

        assert self.error_handler in handlers_called
        assert self.ok_handler in handlers_called
        assert log_received in handlers_called
        self.assertEqual(result[0][0], self.error_handler)
        self.assert_(isinstance(result[0][1], Failure))
        self.assertEqual(result[1], (self.ok_handler, "OK"))

        txlog.removeObserver(log_received)
        self.flushLoggedErrors()
        dispatcher.disconnect(self.error_handler, signal=test_signal)
        dispatcher.disconnect(self.ok_handler, signal=test_signal)
 def test_error_logged_if_deferred_not_supported(self):
     test_signal = object()
     test_handler = lambda: defer.Deferred()
     dispatcher.connect(test_handler, test_signal)
     with LogCapture() as l:
         send_catch_log(test_signal)
     self.assertEqual(len(l.records), 1)
     self.assertIn("Cannot return deferreds from signal handler", str(l))
     dispatcher.disconnect(test_handler, test_signal)
Example #4
0
 def test_error_logged_if_deferred_not_supported(self):
     test_signal = object()
     test_handler = lambda: defer.Deferred()
     dispatcher.connect(test_handler, test_signal)
     with LogCapture() as l:
         send_catch_log(test_signal)
     self.assertEqual(len(l.records), 1)
     self.assertIn("Cannot return deferreds from signal handler", str(l))
     dispatcher.disconnect(test_handler, test_signal)
Example #5
0
 def test_error_logged_if_deferred_not_supported(self):
     test_signal = object()
     test_handler = lambda: defer.Deferred()
     log_events = []
     txlog.addObserver(log_events.append)
     dispatcher.connect(test_handler, test_signal)
     send_catch_log(test_signal)
     self.assertTrue(log_events)
     self.assertIn("Cannot return deferreds from signal handler", str(log_events))
     txlog.removeObserver(log_events.append)
     self.flushLoggedErrors()
     dispatcher.disconnect(test_handler, test_signal)
Example #6
0
 def test_error_logged_if_deferred_not_supported(self):
     test_signal = object()
     test_handler = lambda: defer.Deferred()
     log_events = []
     txlog.addObserver(log_events.append)
     dispatcher.connect(test_handler, test_signal)
     send_catch_log(test_signal)
     self.failUnless(log_events)
     self.failUnless(
         "Cannot return deferreds from signal handler" in str(log_events))
     txlog.removeObserver(log_events.append)
     self.flushLoggedErrors()
     dispatcher.disconnect(test_handler, test_signal)
Example #7
0
 def disconnect(self, receiver, signal, **kwargs):
     """
     Disconnect a receiver function from a signal. This has the
     opposite effect of the :meth:`connect` method, and the arguments
     are the same.
     """
     kwargs.setdefault('sender', self.sender)
     return dispatcher.disconnect(receiver, signal, **kwargs)
Example #8
0
 def disconnect(self, receiver, signal, **kwargs):
     """
     Disconnect a receiver function from a signal. This has the
     opposite effect of the :meth:`connect` method, and the arguments
     are the same.
     """
     kwargs.setdefault('sender', self.sender)
     return dispatcher.disconnect(receiver, signal, **kwargs)
Example #9
0
    def test_signals(self):
        signals_catched = set()

        def spider_opened(spider):
            assert spider is self.spider
            signals_catched.add(stats_spider_opened)

        def spider_closing(spider, reason):
            assert spider is self.spider
            assert reason == 'testing'
            signals_catched.add(stats_spider_closing)

        def spider_closed(spider, reason, spider_stats):
            assert spider is self.spider
            assert reason == 'testing'
            assert spider_stats == {'test': 1}
            signals_catched.add(stats_spider_closed)

        dispatcher.connect(spider_opened, signal=stats_spider_opened)
        dispatcher.connect(spider_closing, signal=stats_spider_closing)
        dispatcher.connect(spider_closed, signal=stats_spider_closed)

        stats = StatsCollector()
        stats.open_spider(self.spider)
        stats.set_value('test', 1, spider=self.spider)
        self.assertEqual([(self.spider, {'test': 1})], list(stats.iter_spider_stats()))
        stats.close_spider(self.spider, 'testing')
        assert stats_spider_opened in signals_catched
        assert stats_spider_closing in signals_catched
        assert stats_spider_closed in signals_catched

        dispatcher.disconnect(spider_opened, signal=stats_spider_opened)
        dispatcher.disconnect(spider_closing, signal=stats_spider_closing)
        dispatcher.disconnect(spider_closed, signal=stats_spider_closed)
Example #10
0
    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'
            return "OK"

        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)
        result = 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
        self.assertEqual(result[0][0], test_handler_error)
        self.assert_(isinstance(result[0][1], Exception))
        self.assertEqual(result[1], (test_handler_check, "OK"))

        dispatcher.disconnect(log_received, signal=log.logmessage_received)
        dispatcher.disconnect(test_handler_error, signal=test_signal)
        dispatcher.disconnect(test_handler_check, signal=test_signal)
    def test_send_catch_log(self):
        test_signal = object()
        handlers_called = set()

        dispatcher.connect(self.error_handler, signal=test_signal)
        dispatcher.connect(self.ok_handler, signal=test_signal)
        with LogCapture() as l:
            result = yield defer.maybeDeferred(
                self._get_result, test_signal, arg='test',
                handlers_called=handlers_called
            )

        assert self.error_handler in handlers_called
        assert self.ok_handler in handlers_called
        self.assertEqual(len(l.records), 1)
        record = l.records[0]
        self.assertIn('error_handler', record.getMessage())
        self.assertEqual(record.levelname, 'ERROR')
        self.assertEqual(result[0][0], self.error_handler)
        self.assert_(isinstance(result[0][1], Failure))
        self.assertEqual(result[1], (self.ok_handler, "OK"))

        dispatcher.disconnect(self.error_handler, signal=test_signal)
        dispatcher.disconnect(self.ok_handler, signal=test_signal)
Example #12
0
    def test_send_catch_log(self):
        test_signal = object()
        handlers_called = set()

        dispatcher.connect(self.error_handler, signal=test_signal)
        dispatcher.connect(self.ok_handler, signal=test_signal)
        with LogCapture() as l:
            result = yield defer.maybeDeferred(self._get_result,
                                               test_signal,
                                               arg='test',
                                               handlers_called=handlers_called)

        assert self.error_handler in handlers_called
        assert self.ok_handler in handlers_called
        self.assertEqual(len(l.records), 1)
        record = l.records[0]
        self.assertIn('error_handler', record.getMessage())
        self.assertEqual(record.levelname, 'ERROR')
        self.assertEqual(result[0][0], self.error_handler)
        self.assert_(isinstance(result[0][1], Failure))
        self.assertEqual(result[1], (self.ok_handler, "OK"))

        dispatcher.disconnect(self.error_handler, signal=test_signal)
        dispatcher.disconnect(self.ok_handler, signal=test_signal)
Example #13
0
    def test_signals(self):
        signals_catched = set()

        def spider_opened(spider):
            assert spider is self.spider
            signals_catched.add(stats_spider_opened)

        def spider_closing(spider, reason):
            assert spider is self.spider
            assert reason == 'testing'
            signals_catched.add(stats_spider_closing)

        def spider_closed(spider, reason, spider_stats):
            assert spider is self.spider
            assert reason == 'testing'
            assert spider_stats == {'test': 1}
            signals_catched.add(stats_spider_closed)

        dispatcher.connect(spider_opened, signal=stats_spider_opened)
        dispatcher.connect(spider_closing, signal=stats_spider_closing)
        dispatcher.connect(spider_closed, signal=stats_spider_closed)

        stats = StatsCollector()
        stats.open_spider(self.spider)
        stats.set_value('test', 1, spider=self.spider)
        self.assertEqual([(self.spider, {
            'test': 1
        })], list(stats.iter_spider_stats()))
        stats.close_spider(self.spider, 'testing')
        assert stats_spider_opened in signals_catched
        assert stats_spider_closing in signals_catched
        assert stats_spider_closed in signals_catched

        dispatcher.disconnect(spider_opened, signal=stats_spider_opened)
        dispatcher.disconnect(spider_closing, signal=stats_spider_closing)
        dispatcher.disconnect(spider_closed, signal=stats_spider_closed)
Example #14
0
 def disconnect(self, *a, **kw):
     kw.setdefault('sender', self.sender)
     return dispatcher.disconnect(*a, **kw)
Example #15
0
def disconnect_all(signal=Any, sender=Any):
    """Disconnect all signal handlers. Useful for cleaning up after running
    tests
    """
    for receiver in liveReceivers(getAllReceivers(sender, signal)):
        disconnect(receiver, signal=signal, sender=sender)
Example #16
0
def disconnect_all(signal=Any, sender=Any):
    """Disconnect all signal handlers. Useful for cleaning up after running
    tests
    """
    for receiver in liveReceivers(getAllReceivers(sender, signal)):
        disconnect(receiver, signal=signal, sender=sender)
Example #17
0
 def tearDown(self):
     dispatcher.disconnect(self._catch_mail_sent, signal=mail_sent)