Browse Source

[2225_statistics] add a helper method _concat and its test

And replace it with '/'.join(args)

Due to the review comment.
Naoki Kambe 12 years ago
parent
commit
652859d502

+ 11 - 6
src/lib/python/isc/statistics/counter.py

@@ -134,6 +134,11 @@ def _stop_timer(start_time, element, spec, identifier):
                     delta.microseconds * 1E-6, 6)
                     delta.microseconds * 1E-6, 6)
     _set_counter(element, spec, identifier, sec)
     _set_counter(element, spec, identifier, sec)
 
 
+def _concat(*args, sep='/'):
+    """Concatenates words in args with a separator('/')
+    """
+    return sep.join(args)
+
 class _Statistics():
 class _Statistics():
     """Statistics data set"""
     """Statistics data set"""
     # default statistics data
     # default statistics data
@@ -291,7 +296,7 @@ class Counters():
         caller. isc.cc.data.DataNotFoundError is raised when
         caller. isc.cc.data.DataNotFoundError is raised when
         incrementing the counter of the item undefined in the spec
         incrementing the counter of the item undefined in the spec
         file."""
         file."""
-        identifier = '/'.join(args)
+        identifier = _concat(*args)
         step = 1
         step = 1
         with self._rlock:
         with self._rlock:
             if self._disabled: return
             if self._disabled: return
@@ -305,7 +310,7 @@ class Counters():
         caller. isc.cc.data.DataNotFoundError is raised when
         caller. isc.cc.data.DataNotFoundError is raised when
         decrementing the counter of the item undefined in the spec
         decrementing the counter of the item undefined in the spec
         file."""
         file."""
-        identifier = '/'.join(args)
+        identifier = _concat(*args)
         step = -1
         step = -1
         with self._rlock:
         with self._rlock:
             if self._disabled: return
             if self._disabled: return
@@ -317,13 +322,13 @@ class Counters():
         """A getter method for counters. It returns the current number
         """A getter method for counters. It returns the current number
         of the specified counter.  isc.cc.data.DataNotFoundError is
         of the specified counter.  isc.cc.data.DataNotFoundError is
         raised when the counter doesn't have a number yet."""
         raised when the counter doesn't have a number yet."""
-        identifier = '/'.join(args)
+        identifier = _concat(*args)
         return _get_counter(self._statistics._data, identifier)
         return _get_counter(self._statistics._data, identifier)
 
 
     def start(self, *args):
     def start(self, *args):
         """Sets the value returned from _start_timer() as a value of
         """Sets the value returned from _start_timer() as a value of
         the identifier in the self._start_time which is dict-type"""
         the identifier in the self._start_time which is dict-type"""
-        identifier = '/'.join(args)
+        identifier = _concat(*args)
         with self._rlock:
         with self._rlock:
             if self._disabled: return
             if self._disabled: return
             isc.cc.data.set(self._start_time, identifier, _start_timer())
             isc.cc.data.set(self._start_time, identifier, _start_timer())
@@ -336,7 +341,7 @@ class Counters():
         timer which has never been started, it raises and does
         timer which has never been started, it raises and does
         nothing. But in case of stopping the time which isn't defined
         nothing. But in case of stopping the time which isn't defined
         in the spec file, it raises DataNotFoundError"""
         in the spec file, it raises DataNotFoundError"""
-        identifier = '/'.join(args)
+        identifier = _concat(*args)
         with self._rlock:
         with self._rlock:
             if self._disabled: return
             if self._disabled: return
             try:
             try:
@@ -355,7 +360,7 @@ class Counters():
                 # delete the started timer
                 # delete the started timer
                 del isc.cc.data.find(
                 del isc.cc.data.find(
                     self._start_time,
                     self._start_time,
-                    '/'.join(identifier.split('/')[0:-1]))\
+                    _concat(*identifier.split('/')[0:-1]))\
                     [identifier.split('/')[-1]]
                     [identifier.split('/')[-1]]
 
 
     def dump_statistics(self):
     def dump_statistics(self):

+ 16 - 0
src/lib/python/isc/statistics/tests/counter_test.py

@@ -142,6 +142,22 @@ class TestBasicMethods(unittest.TestCase):
             counter._get_counter(self.counters._statistics._data,
             counter._get_counter(self.counters._statistics._data,
                                  timer_name), 0)
                                  timer_name), 0)
 
 
+    def test_concat(self):
+        # only strings
+        a = ( 'a','b','c','d' )
+        self.assertEqual('a/b/c/d', counter._concat(*a))
+        self.assertEqual('aTbTcTd', counter._concat(*a, sep='T'))
+        self.assertEqual('a\\b\\c\\d', counter._concat(*a, sep='\\'))
+        # mixed with other types
+        b = a + (1,)
+        self.assertRaises(TypeError, counter._concat, *b)
+        b = a + (1.1,)
+        self.assertRaises(TypeError, counter._concat, *b)
+        b = a + ([],)
+        self.assertRaises(TypeError, counter._concat, *b)
+        b = a + ({},)
+        self.assertRaises(TypeError, counter._concat, *b)
+
 class BaseTestCounters():
 class BaseTestCounters():
 
 
     def setUp(self):
     def setUp(self):