"""
Tests the log message transportation mechanism.

Requires a running broker and a TKP worker accepting jobs and configured with
INFO level logging (-l INFO). Don't run with DEBUG, the worker will hang.
If not ran with logging level INFO the test will fail.
"""
import unittest
import logging
from tkp.distribute.celery.log import setup_event_listening
from tkp.distribute.celery import celery_app
from tkp.distribute.celery.tasks import bogus


class MockLoggingHandler(logging.Handler):
    """Mock logging handler to check for expected logs."""

    def __init__(self, *args, **kwargs):
        self.reset()
        logging.Handler.__init__(self, *args, **kwargs)

    def emit(self, record):
        self.records.append(record)

    def reset(self):
        self.records = []


class TestCelery(unittest.TestCase):
    """
    Tests related to distributing jobs using celery
    """

    @unittest.skip("Enable this if you have broker and worker running")
    def testRemoteTaskLogging(self):
        """
        make sure the worker log->event->client log mechanism is working.
        """
        setup_event_listening(celery_app)
        mock_handler = MockLoggingHandler()
        root_logger = logging.getLogger()
        root_logger.setLevel(logging.DEBUG)
        root_logger.addHandler(mock_handler)
        check = {logging.INFO, logging.WARNING, logging.ERROR}
        mock_handler.reset()
        mock_handler.setLevel(logging.INFO)
        result=bogus()

        for record in mock_handler.records:
            if record.name == 'tkp.distribute.celery.tasks':
                self.assertTrue(record.levelno in check)
                check.remove(record.levelno)
        self.assertFalse(len(check))

    def testLocalTaskLogging(self):
        """
        Logging should also work if you run it locally
        """
        setup_event_listening(celery_app)
        mock_handler = MockLoggingHandler()
        root_logger = logging.getLogger()
        root_logger.setLevel(logging.DEBUG)
        root_logger.addHandler(mock_handler)
        check = {logging.INFO, logging.WARNING, logging.ERROR, logging.DEBUG}
        mock_handler.reset()
        bogus()
        for record in mock_handler.records:
            if record.name == 'tkp.distribute.celery.tasks':
                self.assertTrue(record.levelno in check)
                check.remove(record.levelno)
        self.assertFalse(len(check))
@@ -48,216 +48,8 @@
 ism.
-%0A%0ARequires a running broker and a TKP worker accepting jobs and configured with%0AINFO level logging (-l INFO). Don't run with DEBUG, the worker will hang.%0AIf not ran with logging level INFO the test will fail.
 %0A%22%22%22

Update docstring for celery-logging test.