<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Logging.Xunit;
using Foundatio.Metrics;
using Foundatio.Tests.Utility;
using Foundatio.Utility;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;

namespace Foundatio.Tests.Metrics {
    public class StatsDMetricsTests : TestWithLoggingBase, IDisposable {
        private readonly int _port = new Random(12345).Next(10000, 15000);
        private readonly StatsDMetricsClient _client;
        private readonly UdpListener _listener;
        private Thread _listenerThread;

        public StatsDMetricsTests(ITestOutputHelper output) : base(output) {
            _listener = new UdpListener("127.0.0.1", _port);
            _client = new StatsDMetricsClient(new StatsDMetricsClientOptions { ServerName = "127.0.0.1", Port = _port, Prefix = "test" });
        }

        [Fact]
        public async Task CounterAsync() {
            await StartListeningAsync(1);
            _client.Counter("counter");
            var messages = GetMessages();
            Assert.Equal("test.counter:1|c", messages.FirstOrDefault());
        }

        [Fact]
        public async Task CounterAsyncWithValue() {
            await StartListeningAsync(1);

            _client.Counter("counter", 5);
            var messages = GetMessages();
            Assert.Equal("test.counter:5|c", messages.FirstOrDefault());
        }

        [Fact]
        public async Task GaugeAsync() {
            await StartListeningAsync(1);

            _client.Gauge("gauge", 1.1);
            var messages = GetMessages();
            Assert.Equal("test.gauge:1.1|g", messages.FirstOrDefault());
        }

        [Fact]
        public async Task TimerAsync() {
            await StartListeningAsync(1);

            _client.Timer("timer", 1);
            var messages = GetMessages();
            Assert.Equal("test.timer:1|ms", messages.FirstOrDefault());
        }

        [Fact]
        public void CanSendOffline() {
            _client.Counter("counter");
            var messages = GetMessages();
            Assert.Empty(messages);
        }

        [Fact(Skip = "Flakey")]
        public async Task CanSendMultithreaded() {
            const int iterations = 100;
            await StartListeningAsync(iterations);

            await Run.InParallelAsync(iterations, async i =>{
                await SystemClock.SleepAsync(50);
                _client.Counter("counter");
            });
            
            var messages = GetMessages();
            Assert.Equal(iterations, messages.Count);
        }

        [Fact(Skip = "Flakey")]
        public async Task CanSendMultiple() {
            const int iterations = 100000;
            await StartListeningAsync(iterations);

            var metrics = new InMemoryMetricsClient(new InMemoryMetricsClientOptions());
<commit_msg>Uncomment flakey tests so we can identify and fix issues<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Logging.Xunit;
using Foundatio.Metrics;
using Foundatio.Tests.Utility;
using Foundatio.Utility;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;

namespace Foundatio.Tests.Metrics {
    public class StatsDMetricsTests : TestWithLoggingBase, IDisposable {
        private readonly int _port = new Random(12345).Next(10000, 15000);
        private readonly StatsDMetricsClient _client;
        private readonly UdpListener _listener;
        private Thread _listenerThread;

        public StatsDMetricsTests(ITestOutputHelper output) : base(output) {
            _listener = new UdpListener("127.0.0.1", _port);
            _client = new StatsDMetricsClient(new StatsDMetricsClientOptions { ServerName = "127.0.0.1", Port = _port, Prefix = "test" });
        }

        [Fact]
        public async Task CounterAsync() {
            await StartListeningAsync(1);
            _client.Counter("counter");
            var messages = GetMessages();
            Assert.Equal("test.counter:1|c", messages.FirstOrDefault());
        }

        [Fact]
        public async Task CounterAsyncWithValue() {
            await StartListeningAsync(1);

            _client.Counter("counter", 5);
            var messages = GetMessages();
            Assert.Equal("test.counter:5|c", messages.FirstOrDefault());
        }

        [Fact]
        public async Task GaugeAsync() {
            await StartListeningAsync(1);

            _client.Gauge("gauge", 1.1);
            var messages = GetMessages();
            Assert.Equal("test.gauge:1.1|g", messages.FirstOrDefault());
        }

        [Fact]
        public async Task TimerAsync() {
            await StartListeningAsync(1);

            _client.Timer("timer", 1);
            var messages = GetMessages();
            Assert.Equal("test.timer:1|ms", messages.FirstOrDefault());
        }

        [Fact]
        public void CanSendOffline() {
            _client.Counter("counter");
            var messages = GetMessages();
            Assert.Empty(messages);
        }

        [Fact]
        public async Task CanSendMultithreaded() {
            const int iterations = 100;
            await StartListeningAsync(iterations);

            await Run.InParallelAsync(iterations, async i =>{
                await SystemClock.SleepAsync(50);
                _client.Counter("counter");
            });
            
            var messages = GetMessages();
            Assert.Equal(iterations, messages.Count);
        }

        [Fact]
        public async Task CanSendMultiple() {
            const int iterations = 100000;
            await StartListeningAsync(iterations);

            var metrics = new InMemoryMetricsClient(new InMemoryMetricsClientOptions());

{'fraction_non_alphanumeric': 0.10072427151760148, 'fraction_numerical': 0.017180394138453764, 'mean_word_length': 2.3062360801781736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2268905', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1536, 'n_words': 355}