コード例 #1
0
        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();
        }
コード例 #2
0
        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");
        }