class TestBase(unittest.TestCase): def setUp(self): # set up eve self.app = Eve(settings=settings) self.app.config['TESTING'] = True StatsD(self.app) self.test_client = self.app.test_client() # set up statsd mock self.server = StatsdMockServer() self.server.start() def tearDown(self): # Hook self.server.stop() del self.server super(TestBase, self).tearDown() def test_counter(self): r = self.test_client.get('/foo') # let's check statsd mock self.assertEqual(r.status_code, 200) self.server.wait('foo.counter.get', 1) self.server.wait('foo.counter.all', 1) data = list(self.server.metrics['foo.counter.get']) self.assertEqual(len(data), 1) self.assertEqual(data[0]['value'], '1') self.assertEqual(data[0]['type'], 'counter') data = list(self.server.metrics['foo.counter.all']) self.assertEqual(len(data), 1) self.assertEqual(data[0]['value'], '1') self.assertEqual(data[0]['type'], 'counter') self.test_client.get('/foo') self.server.wait('foo.counter.get', 2) data = list(self.server.metrics['foo.counter.get']) self.assertEqual(len(data), 2) def test_timer(self): r = self.test_client.get('/foo') # let's check statsd mock self.assertEqual(r.status_code, 200) self.server.wait('foo.timing.get', 1) data = list(self.server.metrics['foo.timing.get']) self.assertEqual(len(data), 1) self.assertEqual(data[0]['type'], 'timer')
class StatsdMockServerTestCase(TestCase): def setUp(self): # Hook method for setting up the test fixture before exercising it self.server = StatsdMockServer() self.server.start() self.statsd_client = statsd.StatsClient(prefix="bigtag") super(StatsdMockServerTestCase, self).setUp() def tearDown(self): # Hook self.server.stop() del self.server super(StatsdMockServerTestCase, self).tearDown() def test_default_port(self): eq_(STATSD_DEFAULT_PORT, 8125) def test_normal(self): self.statsd_client.gauge('subtag', 1) self.statsd_client.gauge('subtag', 2) self.server.wait('bigtag.subtag', 2) ok_('bigtag.subtag' in self.server.metrics) eq_(len(self.server.metrics), 1) data1 = list(self.server.metrics['bigtag.subtag']) eq_(len(data1), 2) eq_(data1[0]['value'], '1') eq_(data1[0]['type'], 'gauge') eq_(data1[0]['timestamp'], None) # for only raw eq_(data1[0]['rate'], 1.0) eq_(data1[1]['value'], '2') eq_(data1[1]['type'], 'gauge') eq_(data1[1]['timestamp'], None) # for only raw eq_(data1[1]['rate'], 1.0) # test counter self.statsd_client.incr('subtag2', 100) self.statsd_client.incr('subtag2', 200) self.server.wait('bigtag.subtag2', 2) data2 = list(self.server.metrics['bigtag.subtag2']) eq_(len(data2), 2) eq_(data2[0]['value'], '100') eq_(data2[0]['type'], 'counter') eq_(data2[0]['timestamp'], None) # for only raw eq_(data2[0]['rate'], 1.0) eq_(data2[1]['value'], '200') eq_(data2[1]['type'], 'counter') eq_(data2[1]['timestamp'], None) # for only raw eq_(data2[1]['rate'], 1.0) # test timer self.statsd_client.timing('subtag3', 100) self.server.wait('bigtag.subtag3', 1) data3 = list(self.server.metrics['bigtag.subtag3']) eq_(len(data3), 1) ok_(100.0 == float(data3[0]['value'])) eq_(data3[0]['type'], 'timer') eq_(data3[0]['timestamp'], None) # for only raw eq_(data3[0]['rate'], 1.0) @raises(StatsdTimeOutError) def test_timeout(self): self.server.wait('bigtag.subtag', n=1, timeout_msec=100)