Skip to content

Commit

Permalink
Merge pull request #3 from Netuitive/0.0.7
Browse files Browse the repository at this point in the history
fix samples not being cleared properly
  • Loading branch information
shawnbutts committed Jun 7, 2016
2 parents e816798 + cc0bb02 commit 0578ec5
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 6 deletions.
8 changes: 5 additions & 3 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
History
-------

Version 0.0.7 - Jun 07 2016
---------------------------
* fix samples not being cleared properly

Version 0.0.6 - May 11 2016
---------------------------
* fix element type
* improve logging


Version 0.0.5 - May 10 2016
---------------------------
* fix element type configuration
Expand All @@ -21,19 +24,18 @@ Version 0.0.3 - Mar 12 2016
---------------------------
* improve hostname detection


Version 0.0.2 - Mar 11 2016
---------------------------

* Fix memory usage in error condition


Version 0.0.1 - Feb 29 2016
---------------------------

* First release



Copyright and License
---------------------

Expand Down
2 changes: 1 addition & 1 deletion libs/poster.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def flush(self):

# do the post
if self.api.post(element):
self.elements.clear_samples(ename)
logger.debug(
"Successfully sent {0} elements with "
"{1} samples total".format(ec, sc))
Expand Down Expand Up @@ -156,7 +157,6 @@ def flush(self):
"titled {0}".format(event.title))

# reset
self.elements.clear_samples(ename)
self.sample_count = 0
self.packet_count = 0
self.event_count = 0
Expand Down
2 changes: 1 addition & 1 deletion netuitive-statsd
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ from setproctitle import setproctitle
import libs

# Constants
__version__ = "0.0.6"
__version__ = "0.0.7"
__author__ = "Netuitive, Inc."
__license__ = "Apache 2.0"

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

setup(
name='netuitive_statsd',
version='0.0.6',
version='0.0.7',
description="Netuitive StatsD server",
long_description='Netuitive StatsD server\n',
author="Netuitive",
Expand Down
122 changes: 122 additions & 0 deletions tests/test_poster.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import unittest
import mock
import json
import logging
import threading
Expand All @@ -20,6 +21,34 @@
logger = logging.getLogger(__name__)


class MockResponse(object):

def __init__(self,
resp_data='',
headers={'content-type': 'text/plain; charset=utf-8'},
code=200,
msg='OK',
resp_headers=None):

self.resp_data = resp_data
self.code = code
self.msg = msg
self.headers = headers
self.resp_headers = resp_headers

def read(self):
return self.resp_data

def info(self):
return dict(self.resp_headers)

def getcode(self):
return self.code

def close(self):
return True


class Test_Poster(unittest.TestCase):
maxDiff = None

Expand Down Expand Up @@ -69,6 +98,11 @@ def setUp(self):
self.poster3 = libs.Poster(self.config2, self.myElement2)
self.poster3.start()

self.myElement3 = libs.Element(
self.config['hostname'], self.config['element_type'])
self.poster4 = libs.Poster(self.config, self.myElement3)
self.poster4.start()

self.lock = threading.Lock()

def test_Poster_config(self):
Expand Down Expand Up @@ -881,10 +915,98 @@ def test_element_type(self):
self.poster3.elements.delete('testelement2')
self.assertEqual(j, f)

@mock.patch('netuitive.client.urllib2.urlopen')
@mock.patch('netuitive.client.logging')
def test_sample_cleared(self, mock_logging, mock_post):

mock_post.return_value = MockResponse(code=200)

with self.lock:
self.poster4.submit('counter:1|c', self.timestamp)
self.poster4.submit('counter:1|c|#h:host1', self.timestamp)
self.poster4.submit('counter:1|c|#h:host2', self.timestamp)
self.poster4.submit('counter:1|c|#h:host3', self.timestamp)

self.poster4.submit('counter:1|c', self.timestamp)
self.poster4.submit('counter:1|c|#h:host1', self.timestamp)
self.poster4.submit('counter:1|c|#h:host2', self.timestamp)
self.poster4.submit('counter:1|c|#h:host3', self.timestamp)

for ename in self.poster4.elements.elements:
e = self.poster4.elements.elements[ename]
e.prepare()

j = json.loads(json.dumps(
self.poster4.elements, default=lambda o: o.__dict__,
sort_keys=True))

# everything should have 1 sample
self.assertEqual(
len(j['elements']['testelement']['element']['samples']), 1)

self.assertEqual(
len(j['elements']['host1']['element']['samples']), 1)

self.assertEqual(
len(j['elements']['host2']['element']['samples']), 1)

self.assertEqual(
len(j['elements']['host3']['element']['samples']), 1)

# everything should have 1 metric
self.assertEqual(
len(j['elements']['testelement']['element']['metrics']), 1)

self.assertEqual(
len(j['elements']['host1']['element']['metrics']), 1)

self.assertEqual(
len(j['elements']['host2']['element']['metrics']), 1)

self.assertEqual(
len(j['elements']['host3']['element']['metrics']), 1)

self.poster4.flush()

j = json.loads(json.dumps(
self.poster4.elements, default=lambda o: o.__dict__,
sort_keys=True))

self.assertEqual(
len(j['elements']['testelement']['element']['samples']), 0)

self.assertEqual(
len(j['elements']['host1']['element']['samples']), 0)

self.assertEqual(
len(j['elements']['host2']['element']['samples']), 0)

self.assertEqual(
len(j['elements']['host3']['element']['samples']), 0)

# everything should have 0 metric
self.assertEqual(
len(j['elements']['testelement']['element']['metrics']), 0)

self.assertEqual(
len(j['elements']['host1']['element']['metrics']), 0)

self.assertEqual(
len(j['elements']['host2']['element']['metrics']), 0)

self.assertEqual(
len(j['elements']['host3']['element']['metrics']), 0)

self.poster4.elements.delete('testelement')
self.poster4.elements.delete('host1')
self.poster4.elements.delete('host2')
self.poster4.elements.delete('host3')

def tearDown(self):
self.poster.stop()
self.poster2.stop()
self.poster3.stop()
self.poster4.stop()

if __name__ == '__main__':
unittest.main()

0 comments on commit 0578ec5

Please sign in to comment.