[tor-commits] [nyx/master] Basic deduplication unit tests

atagar at torproject.org atagar at torproject.org
Tue May 5 05:42:06 UTC 2015


commit ebaf46e6e310301bc31bb4c45a0430fd52a8439f
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Apr 12 15:22:21 2015 -0700

    Basic deduplication unit tests
    
    Just some simple unit tests to start with. No suprise, deduplication was
    completley borked. :P
---
 nyx/util/log.py                |    8 ++++----
 test/util/log/__init__.py      |    1 +
 test/util/log/deduplication.py |   27 +++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/nyx/util/log.py b/nyx/util/log.py
index a753390..6e28e6c 100644
--- a/nyx/util/log.py
+++ b/nyx/util/log.py
@@ -34,7 +34,7 @@ def _common_log_messages():
 
   for conf_key in nyx_config.keys():
     if conf_key.startswith('dedup.'):
-      event_type = conf_key[4:]
+      event_type = conf_key[6:]
       messages[event_type] = nyx_config.get(conf_key, [])
 
   return messages
@@ -69,10 +69,10 @@ class LogEntry(object):
     :returns: **True** if the given log message is a duplicate of us and **False** otherwise
     """
 
-    if self.message == entry.message:
-      return True
-    elif self.type != entry.type:
+    if self.type != entry.type:
       return False
+    elif self.message == entry.message:
+      return True
 
     for common_msg in _common_log_messages().get(self.type, []):
       # if it starts with an asterisk then check the whole message rather
diff --git a/test/util/log/__init__.py b/test/util/log/__init__.py
index b2fb6f7..9896955 100644
--- a/test/util/log/__init__.py
+++ b/test/util/log/__init__.py
@@ -3,5 +3,6 @@ Unit tests for nyx's log utilities.
 """
 
 __all__ = [
+  'deduplication',
   'read_tor_log',
 ]
diff --git a/test/util/log/deduplication.py b/test/util/log/deduplication.py
new file mode 100644
index 0000000..fdd97f2
--- /dev/null
+++ b/test/util/log/deduplication.py
@@ -0,0 +1,27 @@
+import unittest
+
+from nyx.util.log import LogEntry
+
+
+class TestLogDeduplication(unittest.TestCase):
+  def test_matches_identical_messages(self):
+    # Simple case is that we match the same message but different timestamp.
+
+    entry = LogEntry(1333738434, 'INFO', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"')
+    self.assertTrue(entry.is_duplicate(LogEntry(1333738457, 'INFO', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"')))
+
+    # ... but we shouldn't match if the runlevel differs.
+
+    self.assertFalse(entry.is_duplicate(LogEntry(1333738457, 'DEBUG', 'tor_lockfile_lock(): Locking "/home/atagar/.tor/lock"')))
+
+  def test_matches_based_on_prefix(self):
+    # matches using a prefix specified in dedup.cfg
+
+    entry = LogEntry(1333738434, 'NYX_DEBUG', 'GETCONF MyFamily (runtime: 0.0007)')
+    self.assertTrue(entry.is_duplicate(LogEntry(1333738457, 'NYX_DEBUG', 'GETCONF MyFamily (runtime: 0.0015)')))
+
+  def test_matches_with_wildcard(self):
+    # matches using a wildcard specified in dedup.cfg
+
+    entry = LogEntry(1333738434, 'NOTICE', 'Bootstrapped 72%: Loading relay descriptors.')
+    self.assertTrue(entry.is_duplicate(LogEntry(1333738457, 'NOTICE', 'Bootstrapped 55%: Loading relay descriptors.')))





More information about the tor-commits mailing list