Beispiel #1
0
        public void Bulkhead_Queing_and_Parallel_Execution()
        {
            var maximumNumberOfActionsInParallel     = 0;
            var minumumNumberOfSlotsAvailableInQueue = 10;
            var parallelActionCount = 0;
            var policy = Policy.Bulkhead(4, 8);

            Parallel.ForEach(Enumerable.Range(0, 9), p =>
            {
                policy.Execute(() =>
                {
                    parallelActionCount++;
                    if (policy.QueueAvailableCount < minumumNumberOfSlotsAvailableInQueue)
                    {
                        minumumNumberOfSlotsAvailableInQueue = policy.QueueAvailableCount;
                    }
                    if (parallelActionCount > maximumNumberOfActionsInParallel)
                    {
                        maximumNumberOfActionsInParallel = parallelActionCount;
                    }
                    Thread.Sleep(100);
                    parallelActionCount--;
                }
                               );
            });
            Assert.That(FILL.__IN,
                        Is_.Equal_To(maximumNumberOfActionsInParallel + minumumNumberOfSlotsAvailableInQueue));
        }
Beispiel #2
0
        public void Bulkhead_when_Queue_is_Full()
        {
            var exceptionCount = 0;
            var policy         = Policy.Bulkhead(4, 2);

            try
            {
                Parallel.ForEach(Enumerable.Range(0, 9), p =>
                {
                    policy.Execute(() =>
                    {
                        Thread.Sleep(100);
                    }
                                   );
                });
            }
            catch (AggregateException ae)
            {
                foreach (var ex in ae.InnerExceptions)
                {
                    if (ex is BulkheadRejectedException)
                    {
                        exceptionCount++;
                    }
                }
            }

            Assert.That(FILL._IN, Is_.Equal_To(exceptionCount));
        }
Beispiel #3
0
        public async Task Optimistic_Timeout_with_Cancellation_Token()
        {
            var httpClient = new HttpClient();
            var cancellationTokenSource = new CancellationTokenSource();
            var timeoutPolicy           = Policy.TimeoutAsync(500, TimeoutStrategy.Optimistic);
            var whatHappened            = "nothing yet";

            cancellationTokenSource.CancelAfter(FILL.__IN + 1000);
            try
            {
                var httpResponse = await timeoutPolicy
                                   .ExecuteAsync(async ct =>
                {
                    var httpResponseMessage = await httpClient.GetAsync("http://tempuri.org", ct);
                    Thread.Sleep(100);
                    return(httpResponseMessage);
                }, cancellationTokenSource.Token);
            }
            catch (OperationCanceledException)
            {
                whatHappened = "cancelled";
            }
            catch (TimeoutException)
            {
                whatHappened = "timeout";
            }
            catch (Exception e)
            {
                var y = e.InnerException;
                whatHappened = "unexpected";
            }

            Assert.That(whatHappened, Is_.Equal_To("cancelled"));
        }
Beispiel #4
0
        public void Handle_Exception_and_Retry()
        {
            var count = 0;
            CircuitBreakerPolicy breaker = Policy
                                           .Handle <Exception>()
                                           .CircuitBreaker(
                exceptionsAllowedBeforeBreaking: 2,
                durationOfBreak: TimeSpan.FromMilliseconds(100)
                );

            new[] { 0, 1, 2 }.ToList().ForEach(i =>
            {
                try
                {
                    breaker.Execute(() =>
                    {
                        count++;
                        throw new ApplicationException();
                    });
                }
                catch (ApplicationException)
                {
                }
                catch (BrokenCircuitException)
                {
                }
            }
                                               );
            Assert.That(count, Is_.Equal_To(2));
        }
        public void Handle_Result_with_Fallback()
        {
            var policy = Policy
                         .HandleResult <HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.NotFound)
                         .Fallback(new HttpResponseMessage(FILL.IN_STATUS_CODE));
            var result = policy.Execute(() => new HttpResponseMessage(HttpStatusCode.NotFound));

            Assert.That(result.StatusCode, Is_.Equal_To(HttpStatusCode.OK));
        }
Beispiel #6
0
        public void Handle_Exception_and_Retry()
        {
            //Replace FILL.__IN with value to make the test pass
            var count = FILL.__IN - 2;

            Policy
            .Handle <DivideByZeroException>()
            .Retry(1)
            .Execute(() => 8 / count++);
            Assert.That(count, Is_.Equal_To(2));
        }
Beispiel #7
0
        public void Pessimistic_Timeout()
        {
            var result = "not timed out";
            var policy = Policy
                         .Timeout(TimeSpan.FromMilliseconds(FILL.__IN + 1000), TimeoutStrategy.Pessimistic);

            try
            {
                policy.Execute(() => Thread.Sleep(500));
            }
            catch
            {
                result = "timed out";
            }

            Assert.That(result, Is_.Equal_To("timed out"));
        }
Beispiel #8
0
        public void Handle_Exception_and_Retry_with_count()
        {
            var retries = 0;
            var policy  = Policy
                          .Handle <DivideByZeroException>()
                          .Retry(FILL.__IN, (exception, retryCount, context) => { retries = retryCount; });

            try
            {
                policy.Execute(() => 8 / new[] { 0 }.First());
            }
            catch
            {
                ++retries;
            }

            Assert.That(retries, Is_.Equal_To(4));
        }
Beispiel #9
0
        public void Handle_Exception_and_Wait_before_Retry()
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            var       count     = FILL.__IN + 6;

            Policy
            .Handle <DivideByZeroException>()
            .WaitAndRetry(
                new[] {
                TimeSpan.FromMilliseconds(100),
                TimeSpan.FromMilliseconds(100),
                TimeSpan.FromMilliseconds(100)
            }
                )
            .Execute(() => 8 / count++);
            var elapsed = stopwatch.ElapsedMilliseconds;

            Assert.That(elapsed > 99, Is_.Equal_To(true));
        }
        public void Retry_inside_Timeout()
        {
            var count        = 0;
            var whatHappened = "";
            var timeout      = Policy.Timeout(TimeSpan.FromMilliseconds(5));
            var retry        = Policy.Handle <Exception>()
                               .WaitAndRetryForever(attempt => TimeSpan.FromMilliseconds(4));
            var retryWithTimeout = timeout.Wrap(retry);

            try
            {
                retryWithTimeout.Execute(() => 8 / count++);
            }
            catch (DivideByZeroException)
            {
                whatHappened = "exception";
            }
            catch (Polly.Timeout.TimeoutRejectedException)
            {
                whatHappened = "timed out";
            }
            Assert.That(whatHappened, Is_.Equal_To(FILL._IN));
        }
Beispiel #11
0
        public void Handle_multipe_exceptions_with_Fallback()
        {
            var whatHappened = "nothing yet";
            var policy       = Policy
                               .Handle <DivideByZeroException>()
                               .Or <FILL_IN_EXEPTION>()
                               .Fallback(() => whatHappened = "fallback");

            try
            {
                policy.Execute(() =>
                {
                    var ignoreMe = new RestClient("should Url have protocol?")
                                   .Execute(new RestRequest("foo"));
                }
                               );
            }
            catch (Exception)
            {
                whatHappened = "unexpected";
            }

            Assert.That(whatHappened, Is_.Equal_To("fallback"));
        }
Beispiel #12
0
 public void Sample_Koan_Resolved()
 {
     //FILL.__IN was replaced with 2
     Assert.That(2, Is_.Equal_To(2));
 }
Beispiel #13
0
 public void Sample_Koan_Unresolved()
 {
     //Replace FILL.__IN with 2
     Assert.That(FILL.__IN, Is_.Equal_To(2));
 }