Skip to content

Commit

Permalink
Merge pull request #29 from zodb/issue23
Browse files Browse the repository at this point in the history
The FakeStatsDClient is now always true.
  • Loading branch information
jamadden committed Feb 4, 2021
2 parents 88a8ace + defda3f commit 0e2e391
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
- Move to GitHub Actions from Travis CI.
- Support PyHamcrest 1.10 and later. See `issue 26
<https://github.com/zodb/perfmetrics/issues/26>`_.
- The ``FakeStatsDClient`` for testing is now always true whether or
not any observations have been seen, like the normal clients. See
`issue <https://github.com/zodb/perfmetrics/issues/23>`_.

3.0.0 (2019-09-03)
==================
Expand All @@ -35,7 +38,7 @@
https://github.com/zodb/perfmetrics/issues/11.

- Include support for testing applications instrumented with
perfmetrics in ``perfmetrics.testing``. This was previously release
perfmetrics in ``perfmetrics.testing``. This was previously released
externally as ``nti.fakestatsd``. See https://github.com/zodb/perfmetrics/issues/9.

- Read the ``PERFMETRICS_DISABLE_DECORATOR`` environment variable when
Expand Down
11 changes: 9 additions & 2 deletions src/perfmetrics/testing/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class FakeStatsDClient(StatsdClient):
rather than pushing them over a socket. This class is a drop
in replacement for `perfmetrics.statsd.StatsdClient` that collects statsd
packets and `~.Observation` that are sent through the client.
.. versionchanged:: 3.1.0
Like the normal clients, this object is now always true, whether or
not any observations have been sent.
"""

def __init__(self, prefix=''):
Expand All @@ -58,12 +62,15 @@ def clear(self):
"""
self.udp_sock.clear()

def __bool__(self):
return True

__nonzero__ = __bool__ # Python 2

def __len__(self):
"""
The number of metrics sent. This accounts for multi metric packets
that may be sent.
Note that this object will be false if no metrics have been sent.
"""
return len(self.udp_sock.observations)

Expand Down
7 changes: 6 additions & 1 deletion src/perfmetrics/testing/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from hamcrest import has_properties
from hamcrest import has_property

from nti.testing.matchers import is_true

from .. import FakeStatsDClient as MockStatsDClient

Expand All @@ -31,6 +32,9 @@ class TestMockStatsDClient(unittest.TestCase):
def setUp(self):
self.client = MockStatsDClient()

def test_true_initially(self):
assert_that(self.client, is_true())

def test_tracks_metrics(self):
self.client.incr('mycounter')
self.client.gauge('mygauge', 5)
Expand All @@ -57,10 +61,11 @@ def test_tracks_metrics(self):
def test_clear(self):
self.client.incr('mycounter')
assert_that(self.client, has_length(1))
assert_that(self.client, is_true())

self.client.clear()
assert_that(self.client, has_length(0))

assert_that(self.client, is_true())

def test_tracks_multimetrics(self):
packet = 'gorets:1|c\nglork:320|ms\ngaugor:333|g\nuniques:765|s'
Expand Down
4 changes: 4 additions & 0 deletions src/perfmetrics/tests/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from hamcrest import assert_that
from nti.testing.matchers import validly_provides
from nti.testing.matchers import is_true

from perfmetrics.interfaces import IStatsdClient

Expand Down Expand Up @@ -42,6 +43,9 @@ def _makeOne(self, *args, **kwargs):
def test_implements(self):
assert_that(self._makeOne(), validly_provides(IStatsdClient))

def test_true(self):
assert_that(self._makeOne(), is_true())

class TestNullStatsdClient(TestBasics):

@property
Expand Down

0 comments on commit 0e2e391

Please sign in to comment.