public void TestRetryStrategyRetriableFailure(CallType callType, int httpErrorCode)
        {
            var           searchConfig  = new SearchConfig("appId", "apiKey");
            RetryStrategy retryStrategy = new RetryStrategy(searchConfig);

            var hosts = retryStrategy.GetTryableHost(callType);

            Assert.True(hosts.Count(h => h.Up) == 4);

            var decision = retryStrategy.Decide(hosts.ElementAt(0), new AlgoliaHttpResponse {
                HttpStatusCode = httpErrorCode
            });

            Assert.True(decision.HasFlag(RetryOutcomeType.Retry));

            var updatedHosts = retryStrategy.GetTryableHost(callType);

            Assert.True(updatedHosts.Count(h => h.Up) == 3);

            var decisionAfterNetworkError = retryStrategy.Decide(hosts.ElementAt(0), new AlgoliaHttpResponse {
                IsNetworkError = true
            });

            Assert.True(decisionAfterNetworkError.HasFlag(RetryOutcomeType.Retry));

            var updatedHostsAfterNetworkError = retryStrategy.GetTryableHost(callType);

            Assert.True(updatedHostsAfterNetworkError.Count(h => h.Up) == 2);
        }
        public void TestRetryStrategyMultiThread(CallType callType)
        {
            var           searchConfig  = new SearchConfig("appId", "apiKey");
            RetryStrategy retryStrategy = new RetryStrategy(searchConfig);

            var initialHosts = retryStrategy.GetTryableHost(callType);

            Assert.That(initialHosts, Has.Exactly(4).Items);

            Task task1 = Task.Run(() =>
            {
                var hosts = retryStrategy.GetTryableHost(callType);
                retryStrategy.Decide(hosts.ElementAt(0), new AlgoliaHttpResponse {
                    HttpStatusCode = 200
                });
                Console.WriteLine(Thread.CurrentThread.Name);
            });

            Task task2 = Task.Run(() =>
            {
                var hosts = retryStrategy.GetTryableHost(callType);
                retryStrategy.Decide(hosts.ElementAt(0), new AlgoliaHttpResponse {
                    HttpStatusCode = 500
                });
            });

            Task.WaitAll(task1, task2);

            var updatedHosts = retryStrategy.GetTryableHost(callType);

            Assert.That(updatedHosts, Has.Exactly(3).Items);
        }
        public void TestRetryStrategyMultiThread(CallType callType)
        {
            var           searchConfig  = new SearchConfig("appId", "apiKey");
            RetryStrategy retryStrategy = new RetryStrategy(searchConfig);

            var initialHosts = retryStrategy.GetTryableHost(callType);

            Assert.True(initialHosts.Count() == 4);

            Task task1 = Task.Run(() =>
            {
                var hosts = retryStrategy.GetTryableHost(callType);
                retryStrategy.Decide(hosts.ElementAt(0), 200, false);
                Console.WriteLine(Thread.CurrentThread.Name);
            });

            Task task2 = Task.Run(() =>
            {
                var hosts = retryStrategy.GetTryableHost(callType);
                retryStrategy.Decide(hosts.ElementAt(0), 500, false);
            });

            Task.WaitAll(task1, task2);

            var updatedHosts = retryStrategy.GetTryableHost(callType);

            Assert.True(updatedHosts.Count() == 3);
        }
        public void TestRetryStrategyRetriableFailure(CallType callType, int httpErrorCode)
        {
            var           searchConfig  = new SearchConfig("appId", "apiKey");
            RetryStrategy retryStrategy = new RetryStrategy(searchConfig);

            var hosts = retryStrategy.GetTryableHost(callType);

            Assert.True(hosts.Count(h => h.Up) == 4);

            var decision = retryStrategy.Decide(hosts.ElementAt(0), httpErrorCode, false);

            Assert.True(decision.HasFlag(RetryOutcomeType.Retry));

            var updatedHosts = retryStrategy.GetTryableHost(callType);

            Assert.True(updatedHosts.Count(h => h.Up) == 3);
        }
        public void TestRetryStrategyFailureDecision(CallType callType, int httpErrorCode)
        {
            var           searchConfig  = new SearchConfig("appId", "apiKey");
            RetryStrategy retryStrategy = new RetryStrategy(searchConfig);

            var hosts = retryStrategy.GetTryableHost(callType);

            var decision = retryStrategy.Decide(hosts.ElementAt(0), new AlgoliaHttpResponse {
                HttpStatusCode = httpErrorCode
            });

            Assert.True(decision.HasFlag(RetryOutcomeType.Failure));
        }
        public void TestRetryStrategyResetExpired(CallType callType)
        {
            var commonHosts = new List <StatefulHost>
            {
                new StatefulHost
                {
                    Url     = "-1.algolianet.com",
                    Up      = true,
                    LastUse = DateTime.UtcNow,
                    Accept  = CallType.Read | CallType.Write,
                },
                new StatefulHost
                {
                    Url     = "-2.algolianet.com",
                    Up      = true,
                    LastUse = DateTime.UtcNow,
                    Accept  = CallType.Read | CallType.Write,
                },
                new StatefulHost
                {
                    Url     = "-3.algolianet.com",
                    Up      = false,
                    LastUse = DateTime.UtcNow,
                    Accept  = CallType.Read | CallType.Write,
                }
            };

            SearchConfig config = new SearchConfig(TestHelper.ApplicationId1, TestHelper.AdminKey1)
            {
                CustomHosts = commonHosts
            };

            // TODO
            RetryStrategy retryStrategy = new RetryStrategy(config);
            var           hosts         = retryStrategy.GetTryableHost(callType);

            Assert.True(hosts.Count(h => h.Up) == 2);
        }