Esempio n. 1
0
            public void UpperBoundOfDelayIsMaxDelay()
            {
                var manager = new BackoffLogicManager(TimeSpan.Zero, TimeSpan.Zero);

                PrivateObject wrapper = new PrivateObject(manager);

                wrapper.SetField("consecutiveErrors", int.MaxValue);

                manager.GetBackOffTimeInterval(string.Empty);

                AssertEx.InRange(manager.CurrentDelay, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(3600));
            }
Esempio n. 2
0
        public override void Initialize(Transmitter transmitter)
        {
            if (transmitter == null)
            {
                throw new ArgumentNullException(nameof(transmitter));
            }

            this.backoffLogicManager = transmitter.BackoffLogicManager;

            base.Initialize(transmitter);
            transmitter.TransmissionSent += this.HandleTransmissionSentEvent;
        }
Esempio n. 3
0
            public void UpperBoundOfDelayIsMaxDelay()
            {
                var manager = new BackoffLogicManager(TimeSpan.Zero, TimeSpan.Zero);

                PrivateObject wrapper = new PrivateObject(manager);

                wrapper.SetField("consecutiveErrors", int.MaxValue);

                manager.ScheduleRestore(string.Empty, () => null);

                Assert.InRange(manager.CurrentDelay, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(3600));
            }
Esempio n. 4
0
            public void RetryAfterFromHeadersHasMorePriorityThanExponentialRetry()
            {
                WebHeaderCollection headers = new WebHeaderCollection();

                headers.Add("Retry-After", DateTimeOffset.UtcNow.AddSeconds(30).ToString("O"));

                var manager = new BackoffLogicManager(TimeSpan.Zero);

                manager.ScheduleRestore(headers, () => null);

                Xunit.Assert.InRange(manager.CurrentDelay, TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(30));
            }
Esempio n. 5
0
            public void BackendResponseIsReturnedForCorrectContent()
            {
                string content = BackendResponseHelper.CreateBackendResponse(itemsReceived: 100, itemsAccepted: 1, errorCodes: new[] { "206" }, indexStartWith: 84);

                var backendResponse = BackoffLogicManager.GetBackendResponse(content);

                Assert.AreEqual(1, backendResponse.ItemsAccepted);
                Assert.AreEqual(100, backendResponse.ItemsReceived);
                Assert.AreEqual(1, backendResponse.Errors.Length); // Even though accepted number of items is 1 out of 99 we get only 1 error back. We do not expect same in production but SDK should handle it correctly.
                Assert.AreEqual(84, backendResponse.Errors[0].Index);
                Assert.AreEqual(206, backendResponse.Errors[0].StatusCode);
                Assert.AreEqual("Explanation", backendResponse.Errors[0].Message);
            }
Esempio n. 6
0
            public void RetryAfterOlderThanNowCausesDefaultDelay()
            {
                // An old date
                string retryAfterDateString = DateTime.Now.AddMinutes(-1).ToString("R", CultureInfo.InvariantCulture);

                WebHeaderCollection headers = new WebHeaderCollection();

                headers.Add("Retry-After", retryAfterDateString);

                var manager = new BackoffLogicManager(TimeSpan.Zero);

                manager.ScheduleRestore(headers, () => null);
                Assert.Equal(TimeSpan.FromSeconds(10), manager.CurrentDelay);
            }
            public void DoNotIncrementConsecutiveErrorsMoreOftenThanOnceInminIntervalToUpdateConsecutiveErrors()
            {
                BackoffLogicManager manager = new BackoffLogicManager(TimeSpan.Zero, TimeSpan.FromDays(1));

                Task[] tasks = new Task[10];
                for (int i = 0; i < 10; ++i)
                {
                    tasks[i] = TaskEx.Run(() => manager.ReportBackoffEnabled(500));
                }

                Task.WaitAll(tasks);

                Assert.Equal(1, manager.ConsecutiveErrors);
            }
Esempio n. 8
0
            public void DisableDoesNotLogMessageIfEnabledWasNotCalled()
            {
                using (var listener = new TestEventListener())
                {
                    const long AllKeywords = -1;
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.Error, (EventKeywords)AllKeywords);

                    var manager = new BackoffLogicManager(TimeSpan.Zero);

                    manager.ReportBackoffDisabled();
                    manager.ReportBackoffDisabled();

                    var traces = listener.Messages.ToList();
                    Assert.Equal(0, traces.Count);
                }
            }
            public void ReportBackoffWriteMessageOnce()
            {
                using (var listener = new TestEventListener())
                {
                    const long AllKeywords = -1;
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.Error, (EventKeywords)AllKeywords);

                    var manager = new BackoffLogicManager(TimeSpan.Zero);
                    manager.ReportBackoffEnabled(200);
                    manager.ReportBackoffEnabled(200);

                    var traces = listener.Messages.ToList();

                    Assert.Equal(1, traces.Count);
                    Assert.Equal(2, traces[0].EventId);
                }
            }
            public void DisableDoesNotLogMessageIfEnabledWasNotCalled()
            {
                // this test may fail when other tests running in parallel
                using (var listener = new TestEventListener(waitForDelayedEvents: false))
                {
                    const long AllKeywords = -1;
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.Error, (EventKeywords)AllKeywords);

                    var manager = new BackoffLogicManager(TimeSpan.Zero);

                    manager.ReportBackoffDisabled();
                    manager.ReportBackoffDisabled();

                    var traces = listener.Messages.ToList();
                    Assert.Equal(0, traces.Count);
                }
            }
Esempio n. 11
0
            public void ReportBackoffWriteDoesNotLogMessagesBeforeIntervalPasses()
            {
                using (var listener = new TestEventListener())
                {
                    const long AllKeywords = -1;
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.Error, (EventKeywords)AllKeywords);

                    var manager = new BackoffLogicManager(TimeSpan.FromSeconds(20));

                    manager.ReportBackoffEnabled(200);
                    manager.ReportBackoffEnabled(200);

                    var traces = listener.Messages.ToList();

                    Assert.Equal(0, traces.Count);
                }
            }
            public void ReportBackoffWriteDoesNotLogMessagesBeforeIntervalPasses()
            {
                // this test fails when run in parallel with other tests
                using (var listener = new TestEventListener(waitForDelayedEvents: false))
                {
                    const long AllKeywords = -1;
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.Error, (EventKeywords)AllKeywords);

                    var manager = new BackoffLogicManager(TimeSpan.FromSeconds(20));

                    manager.ReportBackoffEnabled(200);
                    manager.ReportBackoffEnabled(200);

                    var traces = listener.Messages.ToList();

                    Assert.AreEqual(0, traces.Count);
                }
            }
            public void ReportBackoffWritesLogMessagesAfterIntervalPasses()
            {
                using (var listener = new TestEventListener())
                {
                    const long AllKeywords = -1;
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.Error, (EventKeywords)AllKeywords);

                    var manager = new BackoffLogicManager(TimeSpan.FromMilliseconds(10));

                    System.Threading.Thread.Sleep(10);

                    manager.ReportBackoffEnabled(200);
                    manager.ReportBackoffEnabled(200);

                    var traces = listener.Messages.ToList();

                    Assert.Equal(1, traces.Count);
                }
            }
Esempio n. 14
0
        private static void AdditionalVerboseTracing(HttpWebResponse httpResponse)
        {
            // For perf reason deserialize only when verbose tracing is enabled
            if (TelemetryChannelEventSource.IsVerboseEnabled && httpResponse != null)
            {
                try
                {
                    var stream = httpResponse.GetResponseStream();
                    if (stream != null)
                    {
                        using (StreamReader content = new StreamReader(stream))
                        {
                            string response = content.ReadToEnd();

                            if (!string.IsNullOrEmpty(response))
                            {
                                BackendResponse backendResponse = BackoffLogicManager.GetBackendResponse(response);

                                if (backendResponse != null && backendResponse.Errors != null)
                                {
                                    foreach (var error in backendResponse.Errors)
                                    {
                                        if (error != null)
                                        {
                                            TelemetryChannelEventSource.Log.ItemRejectedByEndpointWarning(error.Message);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // This code is for tracing purposes only; it cannot not throw
                }
            }
        }
            public void IfContentCannotBeParsedNullIsReturned()
            {
                var manager = new BackoffLogicManager(TimeSpan.Zero);

                Assert.Null(manager.GetBackendResponse("ab}{"));
            }
            public void IfContentIsUnexpectedJsonNullIsReturned()
            {
                var manager = new BackoffLogicManager(TimeSpan.Zero);

                Assert.Null(manager.GetBackendResponse("[1,2]"));
            }
            public void ReturnNullIfArgumentIsNull()
            {
                var manager = new BackoffLogicManager(TimeSpan.Zero);

                Assert.Null(manager.GetBackendResponse(null));
            }
            public void ReturnNullIfArgumentEmpty()
            {
                var manager = new BackoffLogicManager(TimeSpan.Zero);

                Assert.Null(manager.GetBackendResponse(string.Empty));
            }
 public StubTransmitter(BackoffLogicManager backoffLogicManager)
     : base(new StubTransmissionSender(), new StubTransmissionBuffer(), new StubTransmissionStorage(), Enumerable.Empty <TransmissionPolicy>(), backoffLogicManager)
 {
 }
Esempio n. 20
0
 public void ReturnNullIfArgumentIsNull()
 {
     Assert.IsNull(BackoffLogicManager.GetBackendResponse(null));
 }
Esempio n. 21
0
 public void ReturnNullIfArgumentEmpty()
 {
     Assert.IsNull(BackoffLogicManager.GetBackendResponse(string.Empty));
 }
Esempio n. 22
0
 public void IfContentCannotBeParsedNullIsReturned()
 {
     Assert.IsNull(BackoffLogicManager.GetBackendResponse("ab}{"));
 }
Esempio n. 23
0
 public void IfContentIsUnexpectedJsonNullIsReturned()
 {
     Assert.IsNull(BackoffLogicManager.GetBackendResponse("[1,2]"));
 }