def test_increment(self): client = TCPClient("localhost") client._socket = self.mock_socket client.increment("event") self.mock_sendall.assert_called_with("event:1|c\n".encode()) client.increment("region.event name", 2, 0.5) self.mock_sendall.assert_called_with("region.event_name:2|c|@0.5\n".encode())
def test_client_creates_stopwatch(self): test_start_timestamp = time() one_minute_before_test = test_start_timestamp - 60 client = TCPClient("localhost") client._socket = self.mock_socket stopwatch = client.stopwatch("event") self.assertIsInstance(stopwatch, Stopwatch) self.assertEqual(stopwatch.client, client) self.assertEqual(stopwatch.rate, 1) self.assertGreaterEqual(stopwatch.reference, test_start_timestamp) stopwatch_low_rate = client.stopwatch("low_rate", rate=0.001) self.assertEqual(stopwatch_low_rate.rate, 0.001) self.assertGreaterEqual(stopwatch.reference, test_start_timestamp) stopwatch_1min_ref = client.stopwatch("low_rate", reference=one_minute_before_test) self.assertGreaterEqual(test_start_timestamp, stopwatch_1min_ref.reference) with client.stopwatch("something"): sleep(0.01) self.assertEqual(self.mock_sendall.call_count, 1) request_args = self.mock_sendall.call_args[0] self.assertEqual(len(request_args), 1) request = request_args[0] self.assertRegex(request.decode(), "something:[1-9]\d{0,3}\|ms")
def test_gauge_delta(self): client = TCPClient("localhost") client._socket = self.mock_socket client.gauge_delta("memory!", 128) self.mock_sendall.assert_called_with("memory:+128|g\n".encode()) client.prefix = "region." client.gauge_delta("cpu percentage%", rate=0.9, delta=-12) self.mock_sendall.assert_called_with( "region.cpu_percentage:-12|g|@0.9\n".encode() ) self.mock_sendall.reset_mock() client.gauge_delta("low.rate", 10, 0.1) self.assertEqual(self.mock_sendall.call_count, 0)
def test_timing_since_with_datetime_timestamp(self): start_time = datetime.now() client = TCPClient("localhost") client._socket = self.mock_socket sleep(0.01) client.timing_since("event", start_time) self.assertEqual(self.mock_sendall.call_count, 1) socket_sendall_args = self.mock_sendall.call_args[0] self.assertEqual(len(socket_sendall_args), 1) request = socket_sendall_args[0] self.assertRegex(request.decode(), "event:[1-9]\d*\|ms") self.mock_sendall.reset_mock() client.timing_since("low.rate", start_time, rate=0.01) self.assertEqual(self.mock_sendall.call_count, 0)
def test_set(self): client = TCPClient("localhost") client._socket = self.mock_socket client.set("ip address", "10.10.10.1") self.mock_sendall.assert_called_with( "ip_address:10.10.10.1|s\n".encode() ) client.prefix = "region." client.set("~username*", rate=0.9, value='first') self.mock_sendall.assert_called_with( "region.username:first|s|@0.9\n".encode() ) self.mock_sendall.reset_mock() client.set("low.rate", 256, 0.1) self.assertEqual(self.mock_sendall.call_count, 0)
def test_timing_since_with_timestamp_as_number(self): start_time = time() client = TCPClient("localhost") client._socket = self.mock_socket self.assertRaises(AssertionError, client.timing_since, "negative", -1) sleep(0.01) client.timing_since("event", start_time) self.assertEqual(self.mock_sendall.call_count, 1) socket_sendall_args = self.mock_sendall.call_args[0] self.assertEqual(len(socket_sendall_args), 1) request = socket_sendall_args[0] self.assertRegex(request.decode(), "event:[1-9]+\d*\|ms") self.mock_sendall.reset_mock() client.timing_since("low.rate", start_time, rate=0.1) self.assertEqual(self.mock_sendall.call_count, 0)
def test_gauge(self): client = TCPClient("localhost") client._socket = self.mock_socket client.gauge("memory", 10240) self.mock_sendall.assert_called_with( "memory:10240|g\n".encode() ) client.prefix = "region." client.gauge("cpu percentage%", rate=0.9, value=98.3) self.mock_sendall.assert_called_with( "region.cpu_percentage:98.3|g|@0.9\n".encode() ) self.mock_sendall.reset_mock() client.gauge("low.rate", 128, 0.1) self.assertEqual(self.mock_sendall.call_count, 0) self.assertRaises(AssertionError, client.gauge, "negative", -5)
def test_timing(self): client = TCPClient("localhost") client._socket = self.mock_socket client.timing("event", 10) self.mock_sendall.assert_called_with( "event:10|ms\n".encode() ) client.timing("db.event name", 34.5, 0.5) self.mock_sendall.assert_called_with( "db.event_name:34|ms|@0.5\n".encode(), ) client.prefix = "region.c_" client.timing("db/query", rate=0.7, milliseconds=22.22) self.mock_sendall.assert_called_with( "region.c_db-query:22|ms|@0.7\n".encode(), ) self.mock_sendall.reset_mock() client.timing("low.rate", 12, rate=0.1) self.assertEqual(self.mock_sendall.call_count, 0) self.assertRaises(AssertionError, client.timing, "negative", -2)