public void ProbingCompleted_EmptyProbingResultList_DoNothing()
        {
            var options = Options.Create(new ConsecutiveFailuresHealthPolicyOptions {
                DefaultThreshold = 2
            });
            var policy  = new ConsecutiveFailuresHealthPolicy(options, new DestinationHealthUpdaterStub());
            var cluster = GetClusterInfo("cluster0", destinationCount: 2);

            var probingResults = new[] {
                new DestinationProbingResult(cluster.Destinations.Values.First(), new HttpResponseMessage(HttpStatusCode.InternalServerError), null),
                new DestinationProbingResult(cluster.Destinations.Values.Skip(1).First(), new HttpResponseMessage(HttpStatusCode.OK), null)
            };

            for (var i = 0; i < 2; i++)
            {
                policy.ProbingCompleted(cluster, probingResults);
            }

            Assert.Equal(DestinationHealth.Unhealthy, cluster.Destinations.Values.First().Health.Active);
            Assert.Equal(DestinationHealth.Healthy, cluster.Destinations.Values.Skip(1).First().Health.Active);

            policy.ProbingCompleted(cluster, new DestinationProbingResult[0]);

            Assert.Equal(DestinationHealth.Unhealthy, cluster.Destinations.Values.First().Health.Active);
            Assert.Equal(DestinationHealth.Healthy, cluster.Destinations.Values.Skip(1).First().Health.Active);
        }
        public void ProbingCompleted_SuccessfulResponse_MarkDestinationHealthy()
        {
            var options = Options.Create(new ConsecutiveFailuresHealthPolicyOptions {
                DefaultThreshold = 2
            });
            var policy  = new ConsecutiveFailuresHealthPolicy(options, new DestinationHealthUpdaterStub());
            var cluster = GetClusterInfo("cluster0", destinationCount: 2);

            var probingResults = new[] {
                new DestinationProbingResult(cluster.DestinationManager.Items[0], new HttpResponseMessage(HttpStatusCode.InternalServerError), null),
                new DestinationProbingResult(cluster.DestinationManager.Items[1], new HttpResponseMessage(HttpStatusCode.OK), null)
            };

            for (var i = 0; i < 2; i++)
            {
                policy.ProbingCompleted(cluster, probingResults);
            }

            Assert.Equal(DestinationHealth.Unhealthy, cluster.DestinationManager.Items[0].Health.Active);
            Assert.Equal(DestinationHealth.Healthy, cluster.DestinationManager.Items[1].Health.Active);

            policy.ProbingCompleted(cluster, new[] { new DestinationProbingResult(cluster.DestinationManager.Items[0], new HttpResponseMessage(HttpStatusCode.OK), null) });

            Assert.Equal(DestinationHealth.Healthy, cluster.DestinationManager.Items[0].Health.Active);
            Assert.Equal(DestinationHealth.Healthy, cluster.DestinationManager.Items[1].Health.Active);

            Assert.All(cluster.DestinationManager.Items, d => Assert.Equal(DestinationHealth.Unknown, d.Health.Passive));
        }
        public void ProbingCompleted_FailureThresholdExceeded_MarkDestinationUnhealthy()
        {
            var options = Options.Create(new ConsecutiveFailuresHealthPolicyOptions {
                DefaultThreshold = 2
            });
            var policy   = new ConsecutiveFailuresHealthPolicy(options, new DestinationHealthUpdaterStub());
            var cluster0 = GetClusterInfo("cluster0", destinationCount: 2);
            var cluster1 = GetClusterInfo("cluster0", destinationCount: 2, failureThreshold: 3);

            var probingResults0 = new[] {
                new DestinationProbingResult(cluster0.Destinations.Values.First(), new HttpResponseMessage(HttpStatusCode.InternalServerError), null),
                new DestinationProbingResult(cluster0.Destinations.Values.Skip(1).First(), new HttpResponseMessage(HttpStatusCode.OK), null)
            };
            var probingResults1 = new[] {
                new DestinationProbingResult(cluster1.Destinations.Values.First(), new HttpResponseMessage(HttpStatusCode.OK), null),
                new DestinationProbingResult(cluster1.Destinations.Values.Skip(1).First(), null, new InvalidOperationException())
            };

            Assert.Equal(HealthCheckConstants.ActivePolicy.ConsecutiveFailures, policy.Name);

            // Initial state
            Assert.All(cluster0.Destinations.Values, d => Assert.Equal(DestinationHealth.Unknown, d.Health.Active));
            Assert.All(cluster1.Destinations.Values, d => Assert.Equal(DestinationHealth.Unknown, d.Health.Active));

            // First probing attempt
            policy.ProbingCompleted(cluster0, probingResults0);
            Assert.All(cluster0.Destinations.Values, d => Assert.Equal(DestinationHealth.Healthy, d.Health.Active));
            policy.ProbingCompleted(cluster1, probingResults1);
            Assert.All(cluster1.Destinations.Values, d => Assert.Equal(DestinationHealth.Healthy, d.Health.Active));

            // Second probing attempt
            policy.ProbingCompleted(cluster0, probingResults0);
            Assert.Equal(DestinationHealth.Unhealthy, cluster0.Destinations.Values.First().Health.Active);
            Assert.Equal(DestinationHealth.Healthy, cluster0.Destinations.Values.Skip(1).First().Health.Active);
            policy.ProbingCompleted(cluster1, probingResults1);
            Assert.All(cluster1.Destinations.Values, d => Assert.Equal(DestinationHealth.Healthy, d.Health.Active));

            // Third probing attempt
            policy.ProbingCompleted(cluster0, probingResults0);
            Assert.Equal(DestinationHealth.Unhealthy, cluster0.Destinations.Values.First().Health.Active);
            Assert.Equal(DestinationHealth.Healthy, cluster0.Destinations.Values.Skip(1).First().Health.Active);
            policy.ProbingCompleted(cluster1, probingResults1);
            Assert.Equal(DestinationHealth.Healthy, cluster1.Destinations.Values.First().Health.Active);
            Assert.Equal(DestinationHealth.Unhealthy, cluster1.Destinations.Values.Skip(1).First().Health.Active);

            Assert.All(cluster0.Destinations.Values, d => Assert.Equal(DestinationHealth.Unknown, d.Health.Passive));
            Assert.All(cluster1.Destinations.Values, d => Assert.Equal(DestinationHealth.Unknown, d.Health.Passive));
        }