Ejemplo n.º 1
0
        public void ExecuteShouldWaitBetweenTries()
        {
            var expectedCallCount = 4;
            var expectedMinWait   = 1000;
            var expectedMaxWait   = 1000;
            var methodRetry       = new MethodRetry(500, expectedMinWait, expectedMaxWait, 5);

            var actualCallCount = 0;
            var stopWatch       = System.Diagnostics.Stopwatch.StartNew();
            var result          = methodRetry.Execute(() => actualCallCount++, (callCount) => callCount == expectedCallCount);

            stopWatch.Stop();

            var minExpectedRuntime = expectedMinWait * (expectedCallCount - 1);

            if (minExpectedRuntime > stopWatch.ElapsedMilliseconds)
            {
                Assert.That.Fail($"Execute did not take long enough to run. Expected a minimum of {minExpectedRuntime}ms, but only for {stopWatch.ElapsedMilliseconds}ms");
            }

            var maxExpectedRuntime = (expectedMaxWait * expectedCallCount);

            // Add 5% fudge factor.
            maxExpectedRuntime += Convert.ToInt32(Math.Round(maxExpectedRuntime * .05));
            if (stopWatch.ElapsedMilliseconds > maxExpectedRuntime)
            {
                Assert.That.Fail($"Execute took too long to run. Expected a maximum of {maxExpectedRuntime}ms, but ran for {stopWatch.ElapsedMilliseconds}ms");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sends the email asynchronous.
        /// </summary>
        /// <param name="rockEmailMessage">The rock email message.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        /// <remarks>
        /// This class will call this method and pass the post processed data in a rock email message which
        /// can then be used to send the implementation specific message.
        /// </remarks>
        protected override async Task <EmailSendResponse> SendEmailAsync(RockEmailMessage rockEmailMessage)
        {
            var restRequest = GetRestRequestFromRockEmailMessage(rockEmailMessage);

            var restClient = new RestClient
            {
                BaseUrl       = new Uri(GetAttributeValue("BaseURL")),
                Authenticator = new HttpBasicAuthenticator("api", GetAttributeValue("APIKey"))
            };

            var retriableStatusCode = new List <HttpStatusCode>()
            {
                HttpStatusCode.InternalServerError,
                HttpStatusCode.BadGateway,
                HttpStatusCode.ServiceUnavailable,
                HttpStatusCode.GatewayTimeout,
                (HttpStatusCode)429
            };

            var methodRetry = new MethodRetry();

            // Call the API and get the response
            Response = await methodRetry.ExecuteAsync(() => restClient.ExecuteTaskAsync(restRequest), (response) => !retriableStatusCode.Contains(response.StatusCode)).ConfigureAwait(false);

            if (Response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception(Response.ErrorMessage ?? Response.StatusDescription);
            }

            return(new EmailSendResponse
            {
                Status = Response.StatusCode == HttpStatusCode.OK ? CommunicationRecipientStatus.Delivered : CommunicationRecipientStatus.Failed,
                StatusNote = $"HTTP Status Code: {Response.StatusCode} \r\n Status Description: {Response.StatusDescription}"
            });
        }
Ejemplo n.º 3
0
        public async Task ExecuteAsyncShouldStopCorrectly()
        {
            var expectedCallCount = 1;
            var methodRetry       = new MethodRetry(10, 10, 5000, 5);

            var actualCallCount = 0;
            var result          = await methodRetry.ExecuteAsync(() => Task.FromResult(actualCallCount++), (callCount) => true);

            Assert.That.AreEqual(expectedCallCount, actualCallCount);
        }
Ejemplo n.º 4
0
        public async Task ExecuteAsyncShouldReturnAfterMaxNumberOfTries()
        {
            var expectedCallCount = 5;
            var methodRetry       = new MethodRetry(10, 10, 5000, expectedCallCount);

            var actualCallCount = 0;
            var result          = await methodRetry.ExecuteAsync(async() => await Task.FromResult(actualCallCount++), (callCount) => false).ConfigureAwait(false);

            Assert.That.AreEqual(expectedCallCount, actualCallCount);
        }
Ejemplo n.º 5
0
        public void ExecuteShouldRunTheCorrectNumberOfTimes()
        {
            var expectedCallCount = 3;
            var methodRetry       = new MethodRetry(10, 10, 5000, 5);

            var actualCallCount = 0;
            var result          = methodRetry.Execute(() => actualCallCount++, (callCount) => callCount == (expectedCallCount - 1));

            Assert.That.AreEqual(expectedCallCount, actualCallCount);
        }
Ejemplo n.º 6
0
        public void ExecuteShouldStopCorrectly()
        {
            var expectedCallCount = 1;
            var methodRetry       = new MethodRetry(10, 10, 5000, 5);

            var actualCallCount = 0;
            var result          = methodRetry.Execute(() => actualCallCount++, (callCount) => true);

            Assert.That.AreEqual(expectedCallCount, actualCallCount);
        }
Ejemplo n.º 7
0
        public void ExecuteShouldReturnAfterMaxNumberOfTries()
        {
            var expectedCallCount = 5;
            var methodRetry       = new MethodRetry(10, 10, 5000, expectedCallCount);

            var actualCallCount = 0;
            var result          = methodRetry.Execute(() => actualCallCount++, (callCount) => false);

            Assert.That.AreEqual(expectedCallCount, actualCallCount);
        }
Ejemplo n.º 8
0
        public async Task ExecuteAsyncShouldRunTheCorrectNumberOfTimes()
        {
            var expectedCallCount = 3;
            var methodRetry       = new MethodRetry(10, 10, 5000, 5);

            var actualCallCount = 0;
            var result          = await methodRetry.ExecuteAsync <int>(() => Task.FromResult(actualCallCount++), (callCount) => callCount == (expectedCallCount - 1));

            Assert.That.AreEqual(expectedCallCount, actualCallCount);
        }