Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Switch counter.inc => counter.incr - fixes #26

  • Loading branch information...
commit f48e948f84ab77ab968191c4e1f0a3556a9643c6 1 parent 8d67696
@schmichael authored
Showing with 43 additions and 6 deletions.
  1. +14 −1 mmstats/fields.py
  2. +29 −5 tests/test_types.py
View
15 mmstats/fields.py
@@ -2,10 +2,15 @@
import ctypes
import math
import time
+import warnings
from . import defaults
+# >=2.7 ignores DeprecationWarning by default, mimic that behavior here
+warnings.filterwarnings('ignore', category=DeprecationWarning)
+
+
class DuplicateFieldName(Exception):
"""Cannot add 2 fields with the same name to MmStat instances"""
@@ -214,7 +219,15 @@ class CounterField(ComplexDoubleBufferedField):
class InternalClass(_InternalFieldInterface):
"""Internal counter class used by CounterFields"""
def inc(self, n=1):
- self._set(self.value + n)
+ warnings.warn(
+ "inc(n=...) is deprecated. Use incr(amount=...)",
+ DeprecationWarning
+ )
+ self.incr(n)
+
+ def incr(self, amount=1):
+ """Increment Counter by `amount` (defaults to 1)"""
+ self._set(self.value + amount)
class AverageField(ComplexDoubleBufferedField):
View
34 tests/test_types.py
@@ -1,6 +1,7 @@
import ctypes
import struct
import time
+import warnings
from . import base
@@ -122,23 +123,45 @@ def test_counter_sz(self):
struct.calcsize(mmstats.CounterField.type_signature), 8)
def test_counter(self):
+ """Test counter field"""
+ # Make sure this test only hits non-deprecated APIs
+ warnings.filterwarnings('error', category=DeprecationWarning)
class SimpleCounter(mmstats.MmStats):
counter = mmstats.CounterField()
s = SimpleCounter(filename='mmstats-test_counter')
self.assertEqual(s.counter.value, 0)
- s.counter.inc()
+ s.counter.incr()
self.assertEqual(s.counter.value, 1)
- s.counter.inc()
+ s.counter.incr()
self.assertEqual(s.counter.value, 2)
- s.counter.inc()
- self.assertEqual(s.counter.value, 3)
- s.counter.inc(-4)
+ s.counter.incr(amount=8)
+ self.assertEqual(s.counter.value, 10)
+ s.counter.incr(-11)
self.assertNotEqual(s.counter.value, -1)
self.assertNotEqual(s.counter.value, 0)
s.counter.value = 0
self.assertEqual(s.counter.value, 0)
+ def test_deprecated_inc(self):
+ class SimpleCounter(mmstats.MmStats):
+ counter = mmstats.CounterField()
+
+ s = SimpleCounter(filename='mmstats-deprecated_inc')
+ self.assertEqual(s.counter.value, 0)
+
+ # Make sure the warning is in place
+ warnings.filterwarnings('error', category=DeprecationWarning)
+ self.assertRaises(DeprecationWarning, s.counter.inc)
+ self.assertEqual(s.counter.value, 0)
+
+ # Make sure that the function still works despite being deprecated
+ warnings.filterwarnings('ignore', category=DeprecationWarning)
+ s.counter.inc()
+ self.assertEqual(s.counter.value, 1)
+ s.counter.inc(n=99)
+ self.assertEqual(s.counter.value, 100)
+
def test_floats(self):
class FloatTest(mmstats.MmStats):
f = mmstats.FloatField()
@@ -262,6 +285,7 @@ class TTest(mmstats.MmStats):
self.assertTrue(timer.elapsed > 0.0)
e = timer.elapsed
# Any later elapsed check should be > than the former
+ time.sleep(0.001)
self.assertTrue(timer.elapsed > e)
self.assertNotEqual(stats.t1.value, stats.t2.value)
self.assertEqual(stats.t1.value, stats.t1.last)
Please sign in to comment.
Something went wrong with that request. Please try again.