def test_sending_metrics(self):
        start = datetime.now()
        start_timestamp = time()
        client = Client("localhost", self.__class__.port)
        client.increment("1.test", 5)
        client.increment("2.login")
        client.timing("3.query", 3600)
        client.gauge("4.memory", 102400)
        client.gauge_delta("5.memory", 256)
        client.gauge_delta("6.memory", -128)
        client.set("7.ip", "127.0.0.1")

        expected = [
                "1.test:5|c",
                "2.login:1|c",
                "3.query:3600|ms",
                "4.memory:102400|g",
                "5.memory:+256|g",
                "6.memory:-128|g",
                "7.ip:127.0.0.1|s",
        ]

        self.assert_server_received_expected_requests(expected)

        self.__class__.server.requests.clear()
        client.timing_since("1.query", start_timestamp)
        client.timing_since("2.other_query", start)
        chronometer = client.chronometer()
        chronometer.time_callable("3.sleepy", sleep, 1, (0.02,))

        @chronometer.wrap("4.wait_a_sec")
        def wait_a_sec():
            sleep(0.01)

        wait_a_sec()

        with client.stopwatch("5.my_with_block"):
            sleep(0.02)

        expected_patterns = [
            "1.query:[1-9]\d{0,4}\|ms",
            "2.other_query:[1-9]\d{0,4}\|ms",
            "3.sleepy:[1-9]\d{0,4}\|ms",
            "4.wait_a_sec:[1-9]\d{0,4}\|ms",
            "5.my_with_block:[1-9]\d{0,4}\|ms",
        ]
        self.assert_server_received_expected_request_regex(expected_patterns)
    def test_sending_metrics(self):
        start = datetime.now()
        start_timestamp = time()
        client = Client("localhost", self.__class__.port)
        client.increment("1.test", 5)
        client.increment("2.login")
        client.timing("3.query", 3600)
        client.gauge("4.memory", 102400)
        client.gauge_delta("5.memory", 256)
        client.gauge_delta("6.memory", -128)
        client.set("7.ip", "127.0.0.1")

        expected = [
            "1.test:5|c",
            "2.login:1|c",
            "3.query:3600|ms",
            "4.memory:102400|g",
            "5.memory:+256|g",
            "6.memory:-128|g",
            "7.ip:127.0.0.1|s",
        ]

        self.assert_server_received_expected_requests(expected)

        self.__class__.server.requests.clear()
        client.timing_since("1.query", start_timestamp)
        client.timing_since("2.other_query", start)
        chronometer = client.chronometer()
        chronometer.time_callable("3.sleepy", sleep, 1, (0.02, ))

        @chronometer.wrap("4.wait_a_sec")
        def wait_a_sec():
            sleep(0.01)

        wait_a_sec()

        with client.stopwatch("5.my_with_block"):
            sleep(0.02)

        expected_patterns = [
            "1.query:[1-9]\d{0,4}\|ms",
            "2.other_query:[1-9]\d{0,4}\|ms",
            "3.sleepy:[1-9]\d{0,4}\|ms",
            "4.wait_a_sec:[1-9]\d{0,4}\|ms",
            "5.my_with_block:[1-9]\d{0,4}\|ms",
        ]
        self.assert_server_received_expected_request_regex(expected_patterns)
    def test_gauge(self):
        client = Client("localhost")
        client._socket = self.mock_socket
        client.gauge("memory", 10240)
        self.mock_sendto.assert_called_with(
            "memory:10240|g".encode(),
            ("127.0.0.2", 8125)
        )

        client.prefix = "region."
        client.gauge("cpu percentage%", rate=0.9, value=98.3)
        self.mock_sendto.assert_called_with(
            "region.cpu_percentage:98.3|g|@0.9".encode(),
            ("127.0.0.2", 8125)
        )

        self.mock_sendto.reset_mock()
        client.gauge("low.rate", 128, 0.1)
        self.assertEqual(self.mock_sendto.call_count, 0)

        self.assertRaises(AssertionError, client.gauge, "negative", -5)
    def test_gauge(self):
        client = Client("localhost")
        client._socket = self.mock_socket
        client.gauge("memory", 10240)
        self.mock_sendto.assert_called_with(
            "memory:10240|g".encode(),
            ("127.0.0.2", 8125)
        )

        client.prefix = "region."
        client.gauge("cpu percentage%", rate=0.9, value=98.3)
        self.mock_sendto.assert_called_with(
            "region.cpu_percentage:98.3|g|@0.9".encode(),
            ("127.0.0.2", 8125)
        )

        self.mock_sendto.reset_mock()
        client.gauge("low.rate", 128, 0.1)
        self.assertEqual(self.mock_sendto.call_count, 0)

        self.assertRaises(AssertionError, client.gauge, "negative", -5)