def test_send_modifies_metric_values(self): interface.state.global_monitor = stubs.MockMonitor() interface.state.target = stubs.MockTarget() # pylint: disable=unused-argument def serialize_to(pb, start_time, fields, value, target): pb.data.add().name = 'foo' fake_metric = mock.create_autospec(metrics.Metric, spec_set=True) fake_metric.name = 'fake' fake_metric.serialize_to.side_effect = serialize_to interface.register(fake_metric) # Setting this will modify store._values in the middle of iteration. delayed_metric = metrics.CounterMetric('foo') def send(proto): delayed_metric.increment_by(1) interface.state.global_monitor.send.side_effect = send for i in xrange(1001): interface.state.store.set('fake', ('field', i), None, 123) # Shouldn't raise an exception. interface.flush()
def test_flush_many(self): interface.state.global_monitor = stubs.MockMonitor() interface.state.target = stubs.MockTarget() # pylint: disable=unused-argument def serialize_to(pb, start_time, fields, value, target): pb.data.add().name = 'foo' # We can't use the mock's call_args_list here because the same object is # reused as the argument to both calls and cleared inbetween. data_lengths = [] def send(proto): data_lengths.append(len(proto.data)) interface.state.global_monitor.send.side_effect = send fake_metric = mock.create_autospec(metrics.Metric, spec_set=True) fake_metric.name = 'fake' fake_metric.serialize_to.side_effect = serialize_to interface.register(fake_metric) for i in xrange(1001): interface.state.store.set('fake', ('field', i), None, 123) interface.flush() self.assertEquals(2, interface.state.global_monitor.send.call_count) self.assertEqual(1000, data_lengths[0]) self.assertEqual(1, data_lengths[1])
def test_flush(self): interface.state.global_monitor = stubs.MockMonitor() interface.state.target = stubs.MockTarget() # pylint: disable=unused-argument def serialize_to(pb, start_time, fields, value, target): pb.data.add().name = 'foo' fake_metric = mock.create_autospec(metrics.Metric, spec_set=True) fake_metric.name = 'fake' fake_metric.serialize_to.side_effect = serialize_to interface.register(fake_metric) interface.state.store.set('fake', (), None, 123) interface.flush() interface.state.global_monitor.send.assert_called_once() proto = interface.state.global_monitor.send.call_args[0][0] self.assertEqual(1, len(proto.data)) self.assertEqual('foo', proto.data[0].name)
def test_flush_disabled(self): interface.reset_for_unittest(disable=True) interface.state.global_monitor = stubs.MockMonitor() interface.state.target = stubs.MockTarget() interface.flush() self.assertFalse(interface.state.global_monitor.send.called)