Ejemplo n.º 1
0
        public void send_unsplittable_oversized_udp_packets_are_not_split_or_sent_and_no_exception_is_raised()
        {
            // This message will be one byte longer than the theoretical limit of a UDP packet
            var msg = new string('f', 65508);

            listenThread.Start();
            var batch = statsd.CreateBatch();

            batch.Add <Statsd.Counting>(msg.AsSpan(), 1);
            batch.Send();
            // It shouldn't be split or sent, and no exceptions should be raised.
            AssertWasReceived(null);
        }
            public void add_one_counter_and_send_one_timer_sends_only_sends_the_last()
            {
                var s = new Statsd(_udp, _randomGenerator, _stopwatch);
                var b = s.CreateBatch();

                b.Add <Statsd.Counting>("counter", 1);
                s.Send <Statsd.Timing>("timer", 1);

                _udp.Received().SendAsync("timer:1|ms");
            }
            public void time_add()
            {
                var statsd = new Statsd(new StatsdUDPClient(_localhostAddress, _randomUnusedLocalPort));

                var batch = statsd.CreateBatch();

                batch.Add(() => Thread.Sleep(MultiSecondSleepDelay), "time".AsSpan());
                batch.Send();
                Assert.That(LastPacketMessageReceived(), Does.Match(_expectedMultiSecondTimeRegEx));
            }
            public void add_counter_sets_prefix_on_name()
            {
                var s = new Statsd(_udp, _randomGenerator, _stopwatch, "another.prefix.".AsMemory());
                var b = s.CreateBatch();

                b.Add <Statsd.Counting>("counter", 1, 0.1);
                b.Add <Statsd.Timing>("timer", 1);
                b.Send();

                _udp.Received().SendAsync("another.prefix.counter:1|c|@0.1\nanother.prefix.timer:1|ms");
            }
            public void add_one_counter_and_one_timer_sends_and_removes_commands()
            {
                var s = new Statsd(_udp, _randomGenerator, _stopwatch);
                var b = s.CreateBatch();

                b.Add <Statsd.Counting>("counter", 1, 0.1);
                b.Add <Statsd.Timing>("timer", 1);
                b.Send();

                Assert.That(b.Commands.Count, Is.EqualTo(0));
            }
            public void add_one_counter_and_one_timer_sends_in_one_go()
            {
                var s = new Statsd(_udp, _randomGenerator, _stopwatch);
                var b = s.CreateBatch();

                b.Add <Statsd.Counting>("counter", 1, 0.1);
                b.Add <Statsd.Timing>("timer", 1);
                b.Send();

                _udp.Received().SendAsync("counter:1|c|@0.1\ntimer:1|ms");
            }
            public void add_one_counter_and_one_gauge_with_no_sample_rate_shows_in_commands()
            {
                var s     = new Statsd(_udp, _randomGenerator, _stopwatch);
                var batch = s.CreateBatch();

                batch.Add <Statsd.Counting>("counter", 1);
                batch.Add <Statsd.Timing>("timer", 1);

                Assert.That(batch.Commands.Count, Is.EqualTo(2));
                Assert.That(batch.Commands[0].ToString(), Is.EqualTo("counter:1|c"));
                Assert.That(batch.Commands[1].ToString(), Is.EqualTo("timer:1|ms"));
            }
            public void add_timer_with_lamba()
            {
                const string statName = "name";

                var stopwatch = Substitute.For <IStopwatch>();

                stopwatch.ElapsedMilliseconds.Returns(500);
                _stopwatch.Get().Returns(stopwatch);

                var statsd = new Statsd(_udp, _randomGenerator, _stopwatch);
                var batch  = statsd.CreateBatch();

                batch.Add(() => TestMethod(), statName);

                Assert.That(batch.Commands.Single().ToString(), Is.EqualTo("name:500|ms"));
            }
            public void add_timer_with_lamba_still_records_on_error_and_still_bubbles_up_exception()
            {
                const string statName = "name";

                var stopwatch = Substitute.For <IStopwatch>();

                stopwatch.ElapsedMilliseconds.Returns(500);
                _stopwatch.Get().Returns(stopwatch);

                var s     = new Statsd(_udp, _randomGenerator, _stopwatch);
                var batch = s.CreateBatch();

                Assert.Throws <InvalidOperationException>(() => batch.Add(() => { throw new InvalidOperationException(); }, statName));

                Assert.That(batch.Commands.Count, Is.EqualTo(1));
                Assert.That(batch.Commands[0].ToString(), Is.EqualTo("name:500|ms"));
            }
Ejemplo n.º 10
0
        public void set_max_udp_packet_size()
        {
            // Make sure that we can set the max UDP packet size
            udp    = new StatsdUDPClient(serverName, serverPort, 10);
            statsd = new Statsd(udp);
            var msg = new string('f', 5);

            listenThread.Start(2);
            var batch = statsd.CreateBatch();

            batch.Add <Statsd.Counting>(msg.AsSpan(), 1);
            batch.Add <Statsd.Timing>(msg.AsSpan(), 2);
            batch.Send();
            // Since our packet size limit is now 10, this (short) message should still be split
            AssertWasReceived($"{msg}:1|c", 0);
            AssertWasReceived($"{msg}:2|ms", 1);
        }
Ejemplo n.º 11
0
            public void add_timer_with_lamba_and_sampleRate_fails()
            {
                const string statName = "name";

                var stopwatch = Substitute.For <IStopwatch>();

                stopwatch.ElapsedMilliseconds.Returns(500);
                _stopwatch.Get().Returns(stopwatch);
                _randomGenerator = Substitute.For <IRandomGenerator>();
                _randomGenerator.ShouldSend(0).ReturnsForAnyArgs(false);

                var s     = new Statsd(_udp, _randomGenerator, _stopwatch);
                var batch = s.CreateBatch();

                batch.Add(() => TestMethod(), statName, 0.1);

                Assert.That(batch.Commands.Count, Is.EqualTo(0));
            }