diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2011-11-21 16:51:43 +0000 |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2011-11-21 16:51:43 +0000 |
commit | 465f7a7f2b30f233b0cfc611a6411f68f58c2732 (patch) | |
tree | 2e9ddeac830fd714573c957f89e34f6db9aca166 /tests | |
parent | 71908d6d93695bddee1c2a363aafbb0c530e893c (diff) | |
download | logutils-465f7a7f2b30f233b0cfc611a6411f68f58c2732.tar.gz |
Made ready for 0.3.2.0.3.2
Diffstat (limited to 'tests')
-rw-r--r-- | tests/logutil_tests.py | 4 | ||||
-rw-r--r-- | tests/mytest.py | 9 | ||||
-rw-r--r-- | tests/test_dictconfig.py | 39 | ||||
-rw-r--r-- | tests/test_redis.py | 96 |
4 files changed, 148 insertions, 0 deletions
diff --git a/tests/logutil_tests.py b/tests/logutil_tests.py index 8f1bfb6..330594c 100644 --- a/tests/logutil_tests.py +++ b/tests/logutil_tests.py @@ -4,6 +4,10 @@ from test_dictconfig import ConfigDictTest from test_queue import QueueTest from test_formatter import FormatterTest from test_messages import MessageTest +try: + from test_redis import RedisQueueTest +except ImportError: + pass # The adapter won't work in < 2.5 because the "extra" parameter used by it # only appeared in 2.5 :-( diff --git a/tests/mytest.py b/tests/mytest.py new file mode 100644 index 0000000..1c3fdd6 --- /dev/null +++ b/tests/mytest.py @@ -0,0 +1,9 @@ +from __future__ import absolute_import + +from logutils.testing import TestHandler, Matcher + +class MyTestHandler(TestHandler): + def __init__(self): + TestHandler.__init__(self, Matcher()) + + diff --git a/tests/test_dictconfig.py b/tests/test_dictconfig.py index d333808..42647a2 100644 --- a/tests/test_dictconfig.py +++ b/tests/test_dictconfig.py @@ -483,6 +483,39 @@ class ConfigDictTest(unittest.TestCase): }, } + # As config10, but declaring a handler in a module using + # absolute imports + config11 = { + 'version': 1, + 'formatters': { + 'form1' : { + 'format' : '%(levelname)s ++ %(message)s', + }, + }, + 'filters' : { + 'filt1' : { + 'name' : 'compiler.parser', + }, + }, + 'handlers' : { + 'hand1' : { + '()': 'mytest.MyTestHandler', + 'formatter': 'form1', + 'filters' : ['filt1'], + } + }, + 'loggers' : { + 'compiler.parser' : { + 'level' : 'DEBUG', + 'filters' : ['filt1'], + }, + }, + 'root' : { + 'level' : 'WARNING', + 'handlers' : ['hand1'], + }, + } + def apply_config(self, conf): dictConfig(conf) @@ -660,3 +693,9 @@ class ConfigDictTest(unittest.TestCase): dict(levelname='ERROR', message='4'), ])) + def test_config_11_ok(self): + self.apply_config(self.config11) + h = logging.getLogger().handlers[0] + self.assertEqual(h.__module__, 'mytest') + self.assertEqual(h.__class__.__name__, 'MyTestHandler') + diff --git a/tests/test_redis.py b/tests/test_redis.py new file mode 100644 index 0000000..cc319cb --- /dev/null +++ b/tests/test_redis.py @@ -0,0 +1,96 @@ +import logging +from logutils.testing import TestHandler, Matcher +from logutils.redis import RedisQueueHandler, RedisQueueListener +from redis import Redis +import socket +import subprocess +import time +import unittest + +class QueueListener(RedisQueueListener): + def dequeue(self, block): + record = RedisQueueListener.dequeue(self, block) + if record: + record = logging.makeLogRecord(record) + return record + +class RedisQueueTest(unittest.TestCase): + def setUp(self): + self.handler = h = TestHandler(Matcher()) + self.logger = l = logging.getLogger() + self.server = subprocess.Popen(['redis-server'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + self.wait_for_server() + self.queue = q = Redis() + self.qh = qh = RedisQueueHandler(redis=q) + self.ql = ql = QueueListener(h, redis=q) + ql.start() + l.addHandler(qh) + + def tearDown(self): + self.logger.removeHandler(self.qh) + self.qh.close() + self.handler.close() + self.server.terminate() + + def wait_for_server(self): + maxtime = time.time() + 2 # 2 seconds to wait for server + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + while time.time() < maxtime: + try: + sock.connect(('localhost', 6379)) + break + except socket.error: + pass + if time.time() >= maxtime: + raise Exception('unable to connect to Redis server') + sock.close() + + def test_simple(self): + "Simple test of queue handling and listening." + # Just as a demo, let's log some messages. + # Only one should show up in the log. + self.logger.debug("This won't show up.") + self.logger.info("Neither will this.") + self.logger.warning("But this will.") + self.ql.stop() #ensure all records have come through. + h = self.handler + #import pdb; pdb.set_trace() + self.assertTrue(h.matches(levelno=logging.WARNING)) + self.assertFalse(h.matches(levelno=logging.DEBUG)) + self.assertFalse(h.matches(levelno=logging.INFO)) + + def test_partial(self): + "Test of partial matching through queues." + # Just as a demo, let's log some messages. + # Only one should show up in the log. + self.logger.debug("This won't show up.") + self.logger.info("Neither will this.") + self.logger.warning("But this will.") + self.ql.stop() #ensure all records have come through. + h = self.handler + self.assertTrue(h.matches(msg="ut th")) # from "But this will" + self.assertTrue(h.matches(message="ut th")) # from "But this will" + self.assertFalse(h.matches(message="either")) + self.assertFalse(h.matches(message="won't")) + + def test_multiple(self): + "Test of matching multiple values through queues." + # Just as a demo, let's log some messages. + # Only one should show up in the log. + self.logger.debug("This won't show up.") + self.logger.info("Neither will this.") + self.logger.warning("But this will.") + self.logger.error("And so will this.") + self.ql.stop() #ensure all records have come through. + h = self.handler + self.assertTrue(h.matches(levelno=logging.WARNING, + message='ut thi')) + self.assertTrue(h.matches(levelno=logging.ERROR, + message='nd so wi')) + self.assertFalse(h.matches(levelno=logging.INFO)) + +if __name__ == '__main__': + unittest.main() + |