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

            var invocationCounter = new ProbeInvocationCounter();
            var loggerFactory     = GetLoggerFactory();
            var host = new KestrelAvailabilityProbe(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();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting Server");


            var path        = Path.Combine(Environment.CurrentDirectory, ContainerEnvironment.IsLinux ? "my_contoso_local.pfx" : "TestValidationCertificate.pfx");
            var certificate = new X509Certificate2(path, "abc123");

            Console.WriteLine($"Certificate file is: {path}");
            Console.WriteLine($"Certificate null check: {certificate == null}");

            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);

            host.StartAsync(CancellationToken.None).GetAwaiter().GetResult();

            var task = MainAsync();

            task.GetAwaiter().GetResult();



            Console.WriteLine("Press <ENTER> to end");
            Console.ReadLine();
        }
        public void AllSuccessesHttp()
        {
            const int    Port        = 10580;
            const string HostAddress = "http://127.0.0.1";

            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 = 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.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");
            Assert.IsTrue(availabilityResponses.Count(item => !item) == 0, "Availability failures count incorrect");
            Assert.IsTrue(invocationCounter.AvailabiltyCount == 10, "Availability invocation count correct");
        }