public void StartStopHost()
        {
            const int Port = 10580;

            var invocationCounter = new ProbeInvocationCounter();
            var loggerFactory     = GetLoggerFactory();
            var host = new KestrelHealthProbe(new KestrelHostConfiguration
            {
                Address     = IPAddress.Any,
                Port        = Port,
                Certificate = null,
                KeepAlive   = TimeSpan.FromSeconds(180),
                MaximumConcurrentConnections = 100,
                UseNagle = false
            }, loggerFactory, invocationCounter);


            host.StartAsync(CancellationToken.None).GetAwaiter().GetResult();
            Task.Delay(3000).GetAwaiter().GetResult();
            host.StopAsync(CancellationToken.None).GetAwaiter().GetResult();
            host.Task.GetAwaiter().GetResult();
        }
        public void AllSuccessesHttp()
        {
            const int    Port        = 10580;
            const string HostAddress = "http://127.0.0.1";

            var testComplete      = new TaskCompletionSource <bool>();
            var healthResponses   = new List <bool>();
            var isHostStarted     = false;
            var invocationCounter = new ProbeInvocationCounter();
            var loggerFactory     = GetLoggerFactory();
            var host = new KestrelHealthProbe(new KestrelHostConfiguration
            {
                Address     = IPAddress.Any,
                Port        = Port,
                Certificate = null,
                KeepAlive   = TimeSpan.FromSeconds(180),
                MaximumConcurrentConnections = 100,
                UseNagle = false
            }, loggerFactory, invocationCounter);

            var collector = new Thread(new ThreadStart(() =>
            {
                var messageHandler = new HttpClientHandler();
                messageHandler.ServerCertificateCustomValidationCallback += (httpRequest, certificate, chain, policyErrors) => true;

                var client = new HttpClient(messageHandler);

                var maximumWaitTime = DateTime.UtcNow.AddSeconds(10);

                // Wait for the host to start
                while (DateTime.UtcNow < maximumWaitTime && !isHostStarted)
                {
                    Thread.Sleep(100);
                }

                for (var index = 0; index < 10; index++)
                {
                    try
                    {
                        var response = client.GetAsync($"{HostAddress}:{ Port }{ DualProbeLogic.HealthEndpoint }").GetAwaiter().GetResult();
                        healthResponses.Add(response.IsSuccessStatusCode);
                    }
                    catch (Exception)
                    {
                    }

                    Thread.Sleep(1000);
                }

                testComplete.TrySetResult(true);
            }));

            collector.Start();
            host.StartAsync(CancellationToken.None).GetAwaiter().GetResult();
            isHostStarted = true;

            // Wait up to 30 seconds for the test to complete
            Task.WhenAny(testComplete.Task, Task.Delay(30000)).GetAwaiter().GetResult();

            host.StopAsync(CancellationToken.None).GetAwaiter().GetResult();
            host.Task.GetAwaiter().GetResult();

            Assert.IsTrue(healthResponses.Count(item => item) == 10, "Health success count incorrect");
            Assert.IsTrue(healthResponses.Count(item => !item) == 0, "Health failures count incorrect");
            Assert.IsTrue(invocationCounter.HealthCount == 10, "Health invocation count correct");
        }
        public void AllSuccessesHttps()
        {
            const int    Port        = 10580;
            const string HostAddress = "https://127.0.0.1";

            var path                  = Path.Combine(Environment.CurrentDirectory, ContainerEnvironment.IsLinux ? "my_contoso_local.pfx" : "TestValidationCertificate.pfx");
            var certificate           = new X509Certificate2(path, "abc123");
            var testComplete          = new TaskCompletionSource <bool>();
            var availabilityResponses = new List <bool>();
            var isHostStarted         = false;
            var invocationCounter     = new ProbeInvocationCounter();
            var loggerFactory         = GetLoggerFactory();
            var host                  = new KestrelAvailabilityProbe(new KestrelHostConfiguration
            {
                Address     = IPAddress.Any,
                Port        = Port,
                Certificate = certificate,
                KeepAlive   = TimeSpan.FromSeconds(180),
                MaximumConcurrentConnections = 100,
                UseNagle = false
            }, loggerFactory, invocationCounter);



            var collector = new Thread(new ThreadStart(() =>
            {
                var messageHandler          = new HttpClientHandler();
                messageHandler.SslProtocols = SslProtocols.Tls12;

                messageHandler.ServerCertificateCustomValidationCallback += (httpRequest, certificate, chain, policyErrors) => true;

                var client          = new HttpClient(messageHandler);
                var maximumWaitTime = DateTime.UtcNow.AddSeconds(10);

                // Wait for the host to start
                while (DateTime.UtcNow < maximumWaitTime && !isHostStarted)
                {
                    Thread.Sleep(100);
                }

                for (var index = 0; index < 10; index++)
                {
                    try
                    {
                        var response = client.GetAsync($"{HostAddress}:{ Port }{ DualProbeLogic.AvailabilityEndpoint }").GetAwaiter().GetResult();
                        availabilityResponses.Add(response.IsSuccessStatusCode);
                    }
                    catch (Exception)
                    {
                    }

                    Thread.Sleep(1000);
                }

                testComplete.TrySetResult(true);
            }));

            collector.Start();
            host.StartAsync(CancellationToken.None).GetAwaiter().GetResult();
            isHostStarted = true;

            // Wait up to 30 seconds for the test to complete
            Task.WhenAny(testComplete.Task, Task.Delay(30000)).GetAwaiter().GetResult();

            host.StopAsync(CancellationToken.None).GetAwaiter().GetResult();
            host.Task.GetAwaiter().GetResult();

            Assert.IsTrue(availabilityResponses.Count(item => item) == 10, $"Availability success count incorrect expected 10 but received {availabilityResponses.Count(item => item)}");
            Assert.IsTrue(availabilityResponses.Count(item => !item) == 0, "Availability failures count incorrect");
            Assert.IsTrue(invocationCounter.AvailabiltyCount == 10, "Availability invocation count correct");
        }