Esempio n. 1
0
        public async Task should_call_action_on_every_exception_async()
        {
            ILog logger = A.Fake <ILog>();
            await Retry.ExecuteAsync(() => AddOneAsync(3), TimeSpan.FromMilliseconds(100), 9, logger.WarnAsync);

            A.CallTo(() => logger.WarnAsync(A <Exception> .That.Matches(x => x.GetType() == typeof(NotSupportedException)))).MustHaveHappened(Repeated.Exactly.Times(2));
        }
Esempio n. 2
0
        public async Task should_execute_once_and_then_retry_once_async_and_return()
        {
            int returnValue = await Retry.ExecuteAsync(async() => await AddOneTimeAsync(2), TimeSpan.FromMilliseconds(100), 1);

            returnValue.Should().Be(1);
            _timesCalled.Should().Be(2);
        }
Esempio n. 3
0
 public async Task RetryAsyncWithReturn()
 {
     // Execute the DoWorkAsync
     int i = await Retry.ExecuteAsync(async() =>
     {
         return(await DummyMethods.DoWorkAsyncAndReturn());
     }, TimeSpan.FromMilliseconds(100), 2);
 }
Esempio n. 4
0
 public async Task RetryAsync()
 {
     // Execute the DoWorkAsync
     await Retry.ExecuteAsync(async() =>
     {
         await DummyMethods.DoWorkAsync();
     }, TimeSpan.FromMilliseconds(100), 2);
 }
Esempio n. 5
0
        protected async override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var intervals = intervalsFactory();

            return(await Retry.ExecuteAsync(
                       async ct => await base.SendAsync(request, ct).ConfigureAwait(false),
                       intervals,
                       cancellationToken)
                   .ConfigureAwait(false));
        }
Esempio n. 6
0
        public async Task <CoapMessage> PostAsync(CoapMessage message)
        {
            try
            {
                CoapUri             uri      = new CoapUri(message.ResourceUri.ToString());
                ResponseMessageType rmt      = message.MessageType == CoapMessageType.Confirmable ? ResponseMessageType.Acknowledgement : ResponseMessageType.NonConfirmable;
                EventMetadata       metadata = await GraphManager.GetPiSystemMetadataAsync(uri.Resource);

                if (!await adapter.CanPublishAsync(metadata, channel.IsEncrypted))
                {
                    if (metadata.Audit)
                    {
                        await auditor?.WriteAuditRecordAsync(new MessageAuditRecord("XXXXXXXXXXXX", session.Identity, this.channel.TypeId, "COAP", message.Payload.Length, MessageDirectionType.In, false, DateTime.UtcNow, "Not authorized, missing resource metadata, or channel encryption requirements"));
                    }

                    return(new CoapResponse(message.MessageId, rmt, ResponseCodeType.Unauthorized, message.Token));
                }

                string contentType = message.ContentType.HasValue ? message.ContentType.Value.ConvertToContentType() : "application/octet-stream";

                EventMessage msg = new EventMessage(contentType, uri.Resource, ProtocolType.COAP, message.Encode(), DateTime.UtcNow, metadata.Audit);

                if (!string.IsNullOrEmpty(uri.CacheKey))
                {
                    msg.CacheKey = uri.CacheKey;
                }

                if (uri.Indexes == null)
                {
                    await adapter.PublishAsync(msg);
                }
                else
                {
                    List <KeyValuePair <string, string> > indexes = new List <KeyValuePair <string, string> >(uri.Indexes);

                    Task task = Retry.ExecuteAsync(async() =>
                    {
                        await adapter.PublishAsync(msg, indexes);
                    });

                    task.LogExceptions();
                }


                return(new CoapResponse(message.MessageId, rmt, ResponseCodeType.Created, message.Token));
            }
            catch (Exception ex)
            {
                Trace.TraceError("{0} - CoAP publish error on channel '{1}'", DateTime.UtcNow.ToString("yyyy-MM-ddTHH-MM-ss.fffff"), channel.Id);
                throw ex;
            }
        }
Esempio n. 7
0
        public async Task should_only_catch_given_exceptions_async()
        {
            bool success = false;

            try
            {
                await Retry.ExecuteAsync(() => AddOneAsync(100), TimeSpan.FromMilliseconds(1), 10, null, null, typeof(DivideByZeroException));
            }
            catch (Exception exception)
            {
                exception.Should().BeOfType <NotSupportedException>();
                success = true;
            }

            success.Should().BeTrue();
        }
Esempio n. 8
0
        public async Task should_call_action_on_final_exception_async()
        {
            bool succes = false;
            ILog logger = A.Fake <ILog>();

            try
            {
                await Retry.ExecuteAsync(() => AddOneAsync(10), TimeSpan.FromMilliseconds(100), 3, null, logger.ErrorAsync);
            }
            catch (Exception exception)
            {
                exception.Should().BeOfType <AggregateException>();
                A.CallTo(() => logger.ErrorAsync(A <Exception> .That.Matches(x => x.GetType() == typeof(AggregateException)))).MustHaveHappened(Repeated.Exactly.Times(1));
                succes = true;
            }

            succes.Should().BeTrue();
        }
Esempio n. 9
0
 public async Task RetryAsyncWithAllFeatures()
 {
     // Execute the DoWorkAsync
     await Retry.ExecuteAsync(async() => await DummyMethods.DoWorkAsync(), TimeSpan.FromMilliseconds(100), 2, DummyMethods.ExecuteOnExceptionAsync, DummyMethods.ExecuteOnExceptionAsync, typeof(ArgumentException));
 }
Esempio n. 10
0
 public async Task should_execute_once_and_then_retry_once_async_without_async_specification()
 {
     Action action = async() => { await AddOneAsync(2); };
     await Retry.ExecuteAsync(action, TimeSpan.FromMilliseconds(100), 1);
 }
Esempio n. 11
0
        public async Task should_execute_once_and_then_retry_once_async()
        {
            int count = await Retry.ExecuteAsync(async() => await AddOneAsync(2), TimeSpan.FromMilliseconds(100), 1);

            count.Should().Be(2);
        }