def test_sample_rate_should_be_numeric(self): self.assertRaises( AssertionError, Gauge, 'string_sample_rate', 1.0, 's') gauge = Gauge('ok', 4) gauge.sample_rate = 0.3 self.assertEqual(gauge.sample_rate, 0.3) gauge.sample_rate = 2 self.assertEqual(gauge.sample_rate, 2)
def test_value_should_be_numeric(self): self.assertRaises(AssertionError, Gauge, 'string_val', '') gauge = Gauge('ok', 0.3) def set_value_as_string(): gauge.value = 'not float' self.assertRaises(AssertionError, set_value_as_string) gauge.value = 2.0 self.assertEqual(gauge.value, 2.0) gauge.value = 63 self.assertEqual(gauge.value, 63)
def test_to_request(self): gauge = Gauge('ok', 0.2) self.assertEqual(gauge.to_request(), 'ok:0.2|g') gauge2 = Gauge('another', 237) self.assertEqual(gauge2.to_request(), 'another:237|g') gauge3 = Gauge('again', 11.8, 0.4) self.assertEqual(gauge3.to_request(), 'again:11.8|g|@0.4')
def test_parse_gauge_metric_from_request(self): self.assertEqual( Gauge("cpu_usage", 45.3), parse_metric_from_request("cpu_usage:45.3|g") ) self.assertEqual( Gauge("mem usage?", 10240, 1), parse_metric_from_request("mem usage?:10240|g|@1") ) self.assertEqual( Gauge("weird.gauge.with.rate", 23.3, 0.5), parse_metric_from_request("weird.gauge.with.rate:23.3|g|@0.5") )
def test_gauges(self): shelf = StatsShelf() self.assertEqual(dict(), shelf.gauges()) shelf.add(Gauge("cpu%", 50)) shelf.add(Gauge("cpu%", 51)) shelf.add(Gauge("mem%", 20)) shelf.add(Gauge("mem%", 23)) shelf.add(Gauge("cpu%", 58)) expected = {"cpu%": 58, "mem%": 23} self.assertEqual(expected, shelf.gauges()) gauges = shelf.gauges() gauges["gauges should"] = "not change" self.assertEqual(expected, shelf.gauges())
def test_equality_check(self): gauge = Gauge('cpu', 10) with self.assertRaises(AssertionError): gauge != 'I am not a Gauge' different_name = Gauge('memory', 10) self.assertTrue(gauge != different_name) different_value = Gauge('cpu', 25) self.assertTrue(gauge != different_value) different_rate = Gauge('cpu', 10, 0.5) self.assertTrue(gauge != different_rate) same = Gauge('cpu', 10) self.assertTrue(same == gauge)
def test_value_should_not_be_negative(self): self.assertRaises(AssertionError, Gauge, 'test', -2) gauge = Gauge('ok', 0) def set_negative_value(): gauge.value = -4.5 self.assertRaises(AssertionError, set_negative_value)
def test_gauge_deltas(self): shelf = StatsShelf() self.assertEqual(dict(), shelf.gauges()) shelf.add(GaugeDelta("cpu%", 10)) shelf.add(Gauge("mem%", 10)) shelf.add(GaugeDelta("cpu%", 10)) shelf.add(GaugeDelta("cpu%", -5)) shelf.add(GaugeDelta("mem%", -2)) shelf.add(GaugeDelta("mem%", 4)) expected = {"cpu%": 15, "mem%": 12} self.assertEqual(expected, shelf.gauges())
def test_clear_all_metrics(self): shelf = StatsShelf() shelf.add(Set("users", "me")) shelf.add(Counter("mymetric", 3)) shelf.add(Timer("query", 4.12)) shelf.add(Gauge("cpu%", 38)) shelf.clear() self.assertEqual(dict(), shelf.counters()) self.assertEqual(dict(), shelf.sets()) self.assertEqual(dict(), shelf.timers_data()) self.assertEqual(dict(), shelf.gauges())
def test_default_sample_rate_is_one(self): gauge = Gauge('test', 3) self.assertEqual(gauge.sample_rate, 1)
def test_constructor(self): gauge = Gauge('test', 5, 0.2) self.assertEqual(gauge.name, 'test') self.assertEqual(gauge.value, 5) self.assertEqual(gauge.sample_rate, 0.2)