<commit_before>﻿namespace FunctionalTestUtils
{
    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.ApplicationInsights.DataContracts;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using Xunit;
    using Xunit.Abstractions;
#if NET451 || NET461
    using System.Net;
    using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector;
    using Microsoft.ApplicationInsights.Extensibility;    
#endif

    public abstract class TelemetryTestsBase
    {
        protected const int TestTimeoutMs = 10000;
        private object noParallelism = new object();

        protected readonly ITestOutputHelper output;

        public TelemetryTestsBase(ITestOutputHelper output)
        {
            this.output = output;
        }

        [MethodImpl(MethodImplOptions.NoOptimization)]
        public void ValidateBasicRequest(InProcessServer server, string requestPath, RequestTelemetry expected)
        {
            lock (noParallelism)
            {
                // Subtract 50 milliseconds to hack around strange behavior on build server where the RequestTelemetry.Timestamp is somehow sometimes earlier than now by a few milliseconds.
                expected.Timestamp = DateTimeOffset.Now.Subtract(TimeSpan.FromMilliseconds(50));
                server.BackChannel.Buffer.Clear();
                Stopwatch timer = Stopwatch.StartNew();

                var httpClientHandler = new HttpClientHandler();
                httpClientHandler.UseDefaultCredentials = true;

                Task<HttpResponseMessage> task;
                using (HttpClient httpClient = new HttpClient(httpClientHandler, true))
                {
                    task = httpClient.GetAsync(server.BaseHost + requestPath);
                    task.Wait(TestTimeoutMs);
                }

                timer.Stop();
                server.Dispose();

                RequestTelemetry actual = server.BackChannel.Buffer.OfType<RequestTelemetry>().Where(t => t.Name == expected.Name).Single();
                server.BackChannel.Buffer.Clear();

                Assert.Equal(expected.ResponseCode, actual.ResponseCode);
                Assert.Equal(expected.Name, actual.Name);
                Assert.Equal(expected.Success, actual.Success);
                Assert.Equal(expected.Url, actual.Url);
                InRange(actual.Timestamp, expected.Timestamp, DateTimeOffset.Now);
                output.WriteLine("actual.Timestamp: " + actual.Timestamp);
                output.WriteLine("actual.Duration: " + actual.Duration);
                output.WriteLine("timer.Elapsed: " + timer.Elapsed);
                Assert.True(actual.Duration < timer.Elapsed, "duration");
            }
        }

        public void ValidateBasicException(InProcessServer server, string requestPath, ExceptionTelemetry expected)
        {
            var httpClientHandler = new HttpClientHandler();
            httpClientHandler.UseDefaultCredentials = true;

            Task<HttpResponseMessage> task;
            using (var httpClient = new HttpClient(httpClientHandler, true))
            {
                task = httpClient.GetAsync(server.BaseHost + requestPath);
                task.Wait(TestTimeoutMs);
            }
            var result = task.Result;
            server.Dispose();
            var actual = server.BackChannel.Buffer.OfType<ExceptionTelemetry>().Single();

            Assert.Equal(expected.Exception.GetType(), actual.Exception.GetType());
            Assert.NotEmpty(actual.Exception.StackTrace);
            Assert.NotEmpty(actual.Context.Operation.Name);<commit_msg>Minor time adjustment for random failing test fix<commit_after>﻿namespace FunctionalTestUtils
{
    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.ApplicationInsights.DataContracts;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using Xunit;
    using Xunit.Abstractions;
#if NET451 || NET461
    using System.Net;
    using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector;
    using Microsoft.ApplicationInsights.Extensibility;    
#endif

    public abstract class TelemetryTestsBase
    {
        protected const int TestTimeoutMs = 10000;
        private object noParallelism = new object();

        protected readonly ITestOutputHelper output;

        public TelemetryTestsBase(ITestOutputHelper output)
        {
            this.output = output;
        }

        [MethodImpl(MethodImplOptions.NoOptimization)]
        public void ValidateBasicRequest(InProcessServer server, string requestPath, RequestTelemetry expected)
        {
            lock (noParallelism)
            {
                // Subtract 50 milliseconds to hack around strange behavior on build server where the RequestTelemetry.Timestamp is somehow sometimes earlier than now by a few milliseconds.
                expected.Timestamp = DateTimeOffset.Now.Subtract(TimeSpan.FromMilliseconds(50));
                server.BackChannel.Buffer.Clear();
                Stopwatch timer = Stopwatch.StartNew();

                var httpClientHandler = new HttpClientHandler();
                httpClientHandler.UseDefaultCredentials = true;

                Task<HttpResponseMessage> task;
                using (HttpClient httpClient = new HttpClient(httpClientHandler, true))
                {
                    task = httpClient.GetAsync(server.BaseHost + requestPath);
                    task.Wait(TestTimeoutMs);
                }

                timer.Stop();
                server.Dispose();

                RequestTelemetry actual = server.BackChannel.Buffer.OfType<RequestTelemetry>().Where(t => t.Name == expected.Name).Single();
                server.BackChannel.Buffer.Clear();

                Assert.Equal(expected.ResponseCode, actual.ResponseCode);
                Assert.Equal(expected.Name, actual.Name);
                Assert.Equal(expected.Success, actual.Success);
                Assert.Equal(expected.Url, actual.Url);
                InRange(actual.Timestamp, expected.Timestamp, DateTimeOffset.Now);
                output.WriteLine("actual.Timestamp: " + actual.Timestamp);
                output.WriteLine("actual.Duration: " + actual.Duration);
                output.WriteLine("timer.Elapsed: " + timer.Elapsed);
                Assert.True(actual.Duration < timer.Elapsed.Add(TimeSpan.FromMilliseconds(20)), "duration");
            }
        }

        public void ValidateBasicException(InProcessServer server, string requestPath, ExceptionTelemetry expected)
        {
            var httpClientHandler = new HttpClientHandler();
            httpClientHandler.UseDefaultCredentials = true;

            Task<HttpResponseMessage> task;
            using (var httpClient = new HttpClient(httpClientHandler, true))
            {
                task = httpClient.GetAsync(server.BaseHost + requestPath);
                task.Wait(TestTimeoutMs);
            }
            var result = task.Result;
            server.Dispose();
            var actual = server.BackChannel.Buffer.OfType<ExceptionTelemetry>().Single();

            Assert.Equal(expected.Exception.GetType(), actual.Exception.GetType());
            Assert.NotEmpty(actual.Exception.StackTrace);
            Assert.NotEmpty(actual.Context.Operation.Name);
{'fraction_non_alphanumeric': 0.08409178310473016, 'fraction_numerical': 0.00410203820023074, 'mean_word_length': 2.3716508210890233, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6808881', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1805, 'n_words': 380}