def test_too_many_spools(self): tcp_receiver = MetricReceiverTcp(self.config) # create too many spools, sleep to ensure new ts for t in range(1000, 1011): write_spool(t) my_spool = tcp_receiver.spool tcp_receiver._sock.set_metric(self.test_metric, 20) setup_tcp_receiver(tcp_receiver) time.sleep(1) reciever_run_shutdown(tcp_receiver, 1) all_spools = my_spool.lookup_spools() for ts in all_spools: self.assertIsInstance(ts, int) self.assertEqual(len(all_spools), 10)
def test_too_many_spools_rotate_bytes(self): conf = self.config conf['spool_rotatesize'] = 10 tcp_receiver = MetricReceiverTcp(conf) # create too many spools, sleep to ensure new ts for t in range(1000, 1011): write_spool(t) my_spool = tcp_receiver.spool [tcp_receiver._sock.set_metric(self.test_metric, 20) for _ in range(4)] setup_tcp_receiver(tcp_receiver) while len(my_spool.lookup_spools()) > 10: tcp_receiver._sock.set_metric(self.test_metric, 20) time.sleep(0.1) reciever_run_shutdown(tcp_receiver, 1) self.assertEqual(len(my_spool.lookup_spools()), 10)
def test_processing_metrics(self): filename = write_spool() forwarder = MetricForwarder(self.config, self.shutdown) forwarder.request_session = FakeSession() forwarder.start() while len(forwarder.request_session.metrics_posted) < 10: if forwarder.should_send_batch(): forwarder.forward() time.sleep(0.1) self.shutdown.set() shutdown(forwarder) # we only have 10 valid metrics. self.assertEqual(len(forwarder.request_session.metrics_posted), 10) self.remove_spool(filename)
def test_empty_post(self, sleep): filename = write_spool(empty=True) forwarder = MetricForwarder(self.config, self.shutdown) forwarder.request_session = FakeSession() forwarder.start() if forwarder.should_send_batch(): forwarder.forward() time.sleep(0.1) self.shutdown.set() shutdown(forwarder) metrics_posted = forwarder.request_session.metrics_posted invalid_posts = forwarder.request_session.invalid_posts self.assertFalse(forwarder.request_session.is_called) self.assertEqual(len(metrics_posted), 0) self.assertEqual(len(invalid_posts), 0) self.remove_spool(filename)
def test_post_failure(self, sleep): sleep.return_value = True filename = write_spool(size=10) forwarder = MetricForwarder(self.config, self.shutdown) forwarder.request_session = FakeSession() forwarder.request_session.should_fail = True forwarder.start() while len(forwarder.request_session.metrics_posted) < 10: if forwarder.should_send_batch(): forwarder.forward() time.sleep(1) self.shutdown.set() shutdown(forwarder) metrics_posted = forwarder.request_session.metrics_posted invalid_posts = forwarder.request_session.invalid_posts self.assertTrue(forwarder.request_session.is_called) self.assertEqual(len(metrics_posted), 10) self.assertEqual(len(invalid_posts), 10) self.assertIn(invalid_posts[0], metrics_posted) self.remove_spool(filename)