diff --git a/src/JustEat.StatsD.Tests/BufferBasedStatsDPublisherTests.cs b/src/JustEat.StatsD.Tests/BufferBasedStatsDPublisherTests.cs new file mode 100644 index 00000000..4f9d2c82 --- /dev/null +++ b/src/JustEat.StatsD.Tests/BufferBasedStatsDPublisherTests.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Text; +using JustEat.StatsD.Buffered; +using Shouldly; +using Xunit; + +namespace JustEat.StatsD +{ + public class BufferBasedStatsDPublisherTests + { + private readonly FakeTransport _transport = new FakeTransport(); + private readonly StatsDConfiguration _configuration = new StatsDConfiguration { Prefix = "test" }; + private readonly BufferBasedStatsDPublisher _sut; + + public BufferBasedStatsDPublisherTests() + { + _sut = new BufferBasedStatsDPublisher(_configuration, _transport); + } + + [Fact] + public void TestTiming_TimeSpan() + { + _sut.Timing(TimeSpan.FromSeconds(1.234), "timing"); + _transport.Messages.ShouldHaveSingleItem("test.timing:1234|ms"); + } + + [Fact] + public void TestTimingSampled_TimeSpan() + { + for (int i = 0; _transport.Messages.Count == 0 && i < 10000; i++) + { + _sut.Timing(TimeSpan.FromSeconds(1.234), 0.99, "timing"); + } + _transport.Messages.ShouldHaveSingleItem("test.timing:1234|ms|@0.99"); + } + + [Fact] + public void TestTiming_Long() + { + _sut.Timing(1234, "timing"); + _transport.Messages.ShouldHaveSingleItem("test.timing:1234|ms"); + } + + [Fact] + public void TestTimingSampled_Long() + { + for (int i = 0; _transport.Messages.Count == 0 && i < 10000; i++) + { + _sut.Timing(1234, 0.99, "timing"); + } + _transport.Messages.ShouldHaveSingleItem("test.timing:1234|ms|@0.99"); + } + + private class FakeTransport : IStatsDBufferedTransport + { + public List Messages { get; } = new List(); + + public void Send(in ArraySegment metric) + { + Messages.Add(Encoding.UTF8.GetString(metric.Array, metric.Offset, metric.Count)); + } + } + } +} diff --git a/src/JustEat.StatsD/Buffered/BufferBasedStatsDPublisher.cs b/src/JustEat.StatsD/Buffered/BufferBasedStatsDPublisher.cs index b5b9fc37..2c057686 100644 --- a/src/JustEat.StatsD/Buffered/BufferBasedStatsDPublisher.cs +++ b/src/JustEat.StatsD/Buffered/BufferBasedStatsDPublisher.cs @@ -110,12 +110,12 @@ public void Gauge(long value, string bucket, DateTime timestamp) public void Timing(TimeSpan duration, string bucket) { - Timing(duration.Ticks, bucket); + Timing((long)duration.TotalMilliseconds, bucket); } public void Timing(TimeSpan duration, double sampleRate, string bucket) { - Timing(duration.Ticks, sampleRate, bucket); + Timing((long)duration.TotalMilliseconds, sampleRate, bucket); } public void Timing(long duration, string bucket)