public void does_not_send_metric_immediately()
            {
                var metric = new Counting() { Name = "testMetric", ValueAsInt = 5 };
                var sender = new BatchSender();
                sender.StatsdUDP = _udp;
                sender.Send(metric);

                _udp.AssertWasNotCalled(x => x.Send(Arg<string>.Is.Anything));
            }
 public void does_not_blow_up_if_udp_send_throws()
 {
     var udpStub = MockRepository.GenerateStub<IStatsdUDP>();
     udpStub.Stub(x => x.Send(Arg<string>.Is.Anything)).Throw(new Exception());
     var metric = new Counting() { Name = "testMetric", ValueAsInt = 1 };
     var sender = new BatchSender    ();
     sender.StatsdUDP = udpStub;
     sender.Send(metric);
     sender.Flush();
     Assert.Pass();
 }
            public void does_not_blow_up_if_metric_command_throws()
            {
                var metric = MockRepository.GenerateStub<Counting>();
                metric.Stub(x => x.Command).Throw(new Exception());

                var sender = new BatchSender();
                sender.StatsdUDP = _udp;
                sender.Send(metric);
                sender.Flush();
                Assert.Pass();
            }
            public void sends_metric_on_flush()
            {
                var metric = new Counting() { Name = "testMetric", ValueAsInt = 5 };
                var sender = new BatchSender();
                sender.StatsdUDP = _udp;
                sender.Send(metric);
                sender.Flush();

                IList<object[]> argsPerCall = _udp.GetArgumentsForCallsMadeOn(x => x.Send(Arg<string>.Is.Anything));
                Assert.That(argsPerCall.Count, Is.EqualTo(1));
                Assert.That(((string)argsPerCall[0][0]), Is.EqualTo(metric.Command));
            }
        public void send_oversized_udp_packets_are_split_if_possible_with_multiple_messages_in_one_packet()
        {
            var msg = new String('f', MetricsConfig.DefaultStatsdMaxUDPPacketSize / 2);
            listenThread.Start(3);

            var sender = new BatchSender();
            var statsd = new Statsd(new Statsd.Configuration() { Udp = udp, Sender = sender });

            statsd.Send<Statsd.Counting>("counter", 1);
            statsd.Send<Statsd.Counting>(msg, 2);
            statsd.Send<Statsd.Counting>(msg, 3);
            sender.Flush();

            // Make sure that a split packet can contain mulitple metrics
            AssertWasReceived(String.Format("counter:1|c\n{0}:2|c", msg), 0);
            AssertWasReceived(String.Format("{0}:3|c", msg), 1);
            AssertWasReceived(null, 2);
        }
        public void send_oversized_udp_packets_are_split_if_possible()
        {
            var msg = new String('f', MetricsConfig.DefaultStatsdMaxUDPPacketSize - 15);
            listenThread.Start(3); // Listen for 3 messages

            var sender = new BatchSender();
            var statsd = new Statsd(new Statsd.Configuration() { Udp = udp, Sender = sender });

            statsd.Send<Statsd.Counting>(msg, 1);
            statsd.Send<Statsd.Timing>(msg, 2);
            sender.Flush();

            // These two metrics should be split as their combined lengths exceed the maximum packet size
            AssertWasReceived(String.Format("{0}:1|c", msg), 0);
            AssertWasReceived(String.Format("{0}:2|ms", msg), 1);
            // No extra metric should be sent at the end
            AssertWasReceived(null, 2);
        }
            public void sends_multiple_metrics_at_once()
            {
                var metric1 = new Counting() { Name = "testMetric", ValueAsInt = 5 };
                var metric2 = new Timing() { Name = "testtimer", ValueAsInt = 10 };

                var sender = new BatchSender();
                sender.StatsdUDP = _udp;
                sender.Send(metric1);
                sender.Send(metric2);
                sender.Flush();

                IList<object[]> argsPerCall = _udp.GetArgumentsForCallsMadeOn(x => x.Send(Arg<string>.Is.Anything));
                Assert.That(argsPerCall.Count, Is.EqualTo(1));
                var packet = (string)argsPerCall[0][0];
                var lines = packet.Split('\n');
                Assert.That(lines.Length, Is.EqualTo(2));
                Assert.That(lines[0], Is.EqualTo(metric1.Command));
                Assert.That(lines[1], Is.EqualTo(metric2.Command));
            }
        public void set_max_udp_packet_size()
        {
            // Make sure that we can set the max UDP packet size
            udp = new StatsdUDP(serverName, serverPort, 10);
            var msg = new String('f', 5);
            listenThread.Start(2);

            var sender = new BatchSender();
            var statsd = new Statsd(new Statsd.Configuration() { Udp = udp, Sender = sender });
            statsd.Send<Statsd.Counting>(msg, 1);
            statsd.Send<Statsd.Timing>(msg, 2);
            sender.Flush();

            // Since our packet size limit is now 10, this (short) message should still be split
            AssertWasReceived(String.Format("{0}:1|c", msg), 0);
            AssertWasReceived(String.Format("{0}:2|ms", msg), 1);
        }