Allow easy configuration of host/port settings

The monascastatsd client allowed the host and port to be set but only if you
constructed a Connection object yourself.  This allows you just to specify
host and port when constructing the client object.

Change-Id: Ib49e261b915c07bf8f7036164f1e4814d80ecc1f
This commit is contained in:
Joe Keen 2016-10-20 14:54:03 -06:00
parent aa4424c95e
commit 614a6b5068
3 changed files with 23 additions and 4 deletions

View File

@ -23,7 +23,8 @@ from monascastatsd.timer import Timer
class Client(object):
def __init__(self, name=None, connection=None, max_buffer_size=50, dimensions=None):
def __init__(self, name=None, host='localhost', port=8125,
connection=None, max_buffer_size=50, dimensions=None):
"""Initialize a Client object.
>>> monascastatsd = MonascaStatsd()
@ -35,10 +36,11 @@ class Client(object):
:param max_buffer_size: Maximum number of metric to buffer before
sending to the server if sending metrics in batch
"""
if connection is None:
self.connection = Connection(host='localhost',
port=8125,
max_buffer_size=50)
self.connection = Connection(host=host,
port=port,
max_buffer_size=max_buffer_size)
else:
self.connection = connection
self._dimensions = dimensions

View File

@ -4,3 +4,4 @@
hacking<0.12,>=0.11.0 # Apache-2.0
nose # LGPL
nosexcover # BSD
mock>=2.0 # BSD

View File

@ -23,6 +23,8 @@ import unittest
import monascastatsd as mstatsd
import mock
class FakeSocket(object):
@ -60,6 +62,20 @@ class TestMonascaStatsd(unittest.TestCase):
def recv(self, metric_obj):
return metric_obj._connection.socket.recv()
@mock.patch('monascastatsd.client.Connection')
def test_client_set_host_port(self, connection_mock):
mstatsd.Client(host='foo.bar', port=5213)
connection_mock.assert_called_once_with(host='foo.bar',
port=5213,
max_buffer_size=50)
@mock.patch('monascastatsd.client.Connection')
def test_client_default_host_port(self, connection_mock):
mstatsd.Client()
connection_mock.assert_called_once_with(host='localhost',
port=8125,
max_buffer_size=50)
def test_counter(self):
counter = self.client.get_counter(name='page.views')