mirror of
https://github.com/sprockets/sprockets.mixins.metrics.git
synced 2024-11-25 11:19:53 +00:00
Move FakeStatsdServer into s.m.m.testing.
This commit is contained in:
parent
a125c4ddeb
commit
1c6a7fb857
3 changed files with 63 additions and 38 deletions
10
docs/api.rst
10
docs/api.rst
|
@ -20,3 +20,13 @@ Statsd Implementation
|
|||
---------------------
|
||||
.. autoclass:: sprockets.mixins.metrics.StatsdMixin
|
||||
:members:
|
||||
|
||||
Testing Helpers
|
||||
---------------
|
||||
*So who actually tests that their metrics are emitted as they expect?*
|
||||
|
||||
Usually the answer is *no one*. Why is that? The ``testing`` module
|
||||
contains some helper that make testing a little easier.
|
||||
|
||||
.. autoclass:: sprockets.mixins.metrics.testing.FakeStatsdServer
|
||||
:members:
|
||||
|
|
52
sprockets/mixins/metrics/testing.py
Normal file
52
sprockets/mixins/metrics/testing.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
import socket
|
||||
|
||||
|
||||
class FakeStatsdServer(object):
|
||||
"""
|
||||
Implements something resembling a statsd server.
|
||||
|
||||
:param tornado.ioloop.IOLoop iol: the loop to attach to
|
||||
|
||||
Create an instance of this class in your asynchronous test case
|
||||
attached to the IOLoop and configure your application to send
|
||||
metrics to it. The received datagrams are available in the
|
||||
``datagrams`` attribute for validation in your tests.
|
||||
|
||||
.. attribute:: sockaddr
|
||||
|
||||
The socket address that the server is listening on. This is
|
||||
a tuple returned from :meth:`socket.socket.getsockname`.
|
||||
|
||||
.. attribute:: datagrams
|
||||
|
||||
A list of datagrams that have been received by the server.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, iol):
|
||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
|
||||
socket.IPPROTO_UDP)
|
||||
self.socket.bind(('127.0.0.1', 0))
|
||||
self.sockaddr = self.socket.getsockname()
|
||||
self.datagrams = []
|
||||
|
||||
iol.add_handler(self.socket, self._handle_events, iol.READ)
|
||||
self._iol = iol
|
||||
|
||||
def close(self):
|
||||
if self.socket is not None:
|
||||
if self._iol is not None:
|
||||
self._iol.remove_handler(self.socket)
|
||||
self._iol = None
|
||||
self.socket.close()
|
||||
self.socket = None
|
||||
|
||||
def _handle_events(self, fd, events):
|
||||
if fd != self.socket:
|
||||
return
|
||||
if self._iol is None:
|
||||
raise RuntimeError
|
||||
|
||||
if events & self._iol.READ:
|
||||
data, _ = self.socket.recvfrom(4096)
|
||||
self.datagrams.append(data)
|
39
tests.py
39
tests.py
|
@ -3,47 +3,10 @@ import socket
|
|||
from tornado import testing, web
|
||||
|
||||
from sprockets.mixins import metrics
|
||||
from sprockets.mixins.metrics.testing import FakeStatsdServer
|
||||
import examples.statsd
|
||||
|
||||
|
||||
class FakeStatsdServer(object):
|
||||
"""
|
||||
Implements something resembling a statsd server.
|
||||
|
||||
Received datagrams are saved off in the ``datagrams`` attribute
|
||||
for later examination.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, iol):
|
||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
|
||||
socket.IPPROTO_UDP)
|
||||
self.socket.bind(('127.0.0.1', 0))
|
||||
self.sockaddr = self.socket.getsockname()
|
||||
self.datagrams = []
|
||||
|
||||
iol.add_handler(self.socket, self._handle_events, iol.READ)
|
||||
self._iol = iol
|
||||
|
||||
def close(self):
|
||||
if self.socket is not None:
|
||||
if self._iol is not None:
|
||||
self._iol.remove_handler(self.socket)
|
||||
self._iol = None
|
||||
self.socket.close()
|
||||
self.socket = None
|
||||
|
||||
def _handle_events(self, fd, events):
|
||||
if fd != self.socket:
|
||||
return
|
||||
if self._iol is None:
|
||||
raise RuntimeError
|
||||
|
||||
if events & self._iol.READ:
|
||||
data, _ = self.socket.recvfrom(4096)
|
||||
self.datagrams.append(data)
|
||||
|
||||
|
||||
class StatsdMethodTimingTests(testing.AsyncHTTPTestCase):
|
||||
|
||||
def get_app(self):
|
||||
|
|
Loading…
Reference in a new issue