Skip to content

Commit

Permalink
Switch counter.inc => counter.incr - fixes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
schmichael committed Nov 19, 2012
1 parent 8d67696 commit f48e948
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
15 changes: 14 additions & 1 deletion mmstats/fields.py
Expand Up @@ -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"""

Expand Down Expand Up @@ -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):
Expand Down
34 changes: 29 additions & 5 deletions tests/test_types.py
@@ -1,6 +1,7 @@
import ctypes
import struct
import time
import warnings

from . import base

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f48e948

Please sign in to comment.