Exemple #1
0
        public void IfNumberOfRecievedItemsEqualsToNumberOfAcceptedErrorsListIsIgnored()
        {
            var transmitter = new StubTransmitter();

            var policy = new PartialSuccessTransmissionPolicy();

            policy.Initialize(transmitter);

            var items = new List <ITelemetry> {
                new EventTelemetry(), new EventTelemetry()
            };
            Transmission transmission = new Transmission(new Uri("http://uri"), items, "type", "encoding");

            string response = BackendResponseHelper.CreateBackendResponse(
                itemsReceived: 2,
                itemsAccepted: 2,
                errorCodes: new[] { "408", "408" });

            var wrapper = new HttpWebResponseWrapper
            {
                StatusCode = 206,
                Content    = response
            };

            transmitter.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, null, wrapper));

            Assert.AreEqual(0, transmitter.BackoffLogicManager.ConsecutiveErrors);
        }
Exemple #2
0
            public void LogsAdditionalTracesIfResponseIsProvided()
            {
                using (var listener = new TestEventListener())
                {
                    // Arrange:
                    const long AllKeywords = -1;
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.LogAlways, (EventKeywords)AllKeywords);

                    Transmission enqueuedTransmission = null;
                    var          transmitter          = new StubTransmitter(new BackoffLogicManager(TimeSpan.FromMilliseconds(10)))
                    {
                        OnEnqueue = transmission => { enqueuedTransmission = transmission; }
                    };

                    var policy = new ErrorHandlingTransmissionPolicy();
                    policy.Initialize(transmitter);

                    var failedTransmission = new StubTransmission();
                    var response           = new HttpWebResponseWrapper {
                        Content = BackendResponseHelper.CreateBackendResponse(2, 1, new[] { "123" })
                    };

                    // Act:
                    transmitter.OnTransmissionSent(new TransmissionProcessedEventArgs(failedTransmission, CreateException(statusCode: 408), response));

                    // Assert:
                    var traces = listener.Messages.Where(item => item.Level == EventLevel.Warning).ToList();
                    Assert.AreEqual(2, traces.Count);
                    Assert.AreEqual(23, traces[0].EventId); // failed to send
                    Assert.AreEqual(7, traces[1].EventId);  // additional trace
                    Assert.AreEqual("Explanation", traces[1].Payload[0]);
                }
            }
Exemple #3
0
        public void DoesNotSendTransmissionForUnsupportedCodes()
        {
            IList <Transmission> enqueuedTransmissions = new List <Transmission>();
            var transmitter = new StubTransmitter
            {
                OnEnqueue = t => { enqueuedTransmissions.Add(t); }
            };

            var policy = new PartialSuccessTransmissionPolicy();

            policy.Initialize(transmitter);

            var items = new List <ITelemetry> {
                new EventTelemetry(), new EventTelemetry()
            };
            Transmission transmission = new Transmission(new Uri("http://uri"), items, "type", "encoding");

            string response = BackendResponseHelper.CreateBackendResponse(
                itemsReceived: 50,
                itemsAccepted: 45,
                errorCodes: new[] { "400", "402", "502", "409", "417" });

            var wrapper = new HttpWebResponseWrapper
            {
                StatusCode = 206,
                Content    = response
            };

            transmitter.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, null, wrapper));

            Assert.AreEqual(0, transmitter.BackoffLogicManager.ConsecutiveErrors);
            Assert.AreEqual(0, enqueuedTransmissions.Count);
        }
Exemple #4
0
        public void IfItemsAreRejectedTheyAreUploadedBackAsASingleTransmission()
        {
            IList <Transmission> enqueuedTransmissions = new List <Transmission>();
            var transmitter = new StubTransmitter
            {
                OnEnqueue = t => { enqueuedTransmissions.Add(t); }
            };

            var policy = new PartialSuccessTransmissionPolicy();

            policy.Initialize(transmitter);

            var items = new List <ITelemetry> {
                new EventTelemetry(), new EventTelemetry()
            };
            Transmission transmission = new Transmission(new Uri("http://uri"), items, "type", "encoding");

            string response = BackendResponseHelper.CreateBackendResponse(
                itemsReceived: 3,
                itemsAccepted: 0,
                errorCodes: new[] { "408", "503", "500" });

            var wrapper = new HttpWebResponseWrapper
            {
                StatusCode = 206,
                Content    = response
            };

            transmitter.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, null, wrapper));

            Assert.AreEqual(1, enqueuedTransmissions.Count);
        }
Exemple #5
0
        public void IfMultipleItemsAreRejectedNumberOfErrorsIsIncreasedByOne()
        {
            // Number of errors determine backoff timeout.
            // When we get several bad items in one batch we want to increase errors by 1 only since it is one attempt to access Breeze

            var transmitter = new StubTransmitter();

            var policy = new PartialSuccessTransmissionPolicy();

            policy.Initialize(transmitter);

            var items = new List <ITelemetry> {
                new EventTelemetry(), new EventTelemetry()
            };
            Transmission transmission = new Transmission(new Uri("http://uri"), items, "type", "encoding");

            string response = BackendResponseHelper.CreateBackendResponse(
                itemsReceived: 100,
                itemsAccepted: 95,
                errorCodes: new[] { "500", "500", "503", "503", "429" });

            var wrapper = new HttpWebResponseWrapper
            {
                StatusCode = 206,
                Content    = response
            };

            transmitter.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, null, wrapper));

            Assert.AreEqual(1, transmitter.BackoffLogicManager.ConsecutiveErrors);
        }
Exemple #6
0
        public void IfIndexMoreThanNumberOfItemsInTransmissionIgnoreError()
        {
            IList <Transmission> enqueuedTransmissions = new List <Transmission>();
            var transmitter = new StubTransmitter
            {
                OnEnqueue = t => { enqueuedTransmissions.Add(t); }
            };

            var policy = new PartialSuccessTransmissionPolicy();

            policy.Initialize(transmitter);

            var items = new List <ITelemetry> {
                new EventTelemetry()
            };
            Transmission transmission = new Transmission(new Uri("http://uri"), items, "type", "encoding");

            // Index is 0-based
            string response = BackendResponseHelper.CreateBackendResponse(
                itemsReceived: 2,
                itemsAccepted: 1,
                errorCodes: new[] { "408" },
                indexStartWith: 1);

            var wrapper = new HttpWebResponseWrapper
            {
                StatusCode = 206,
                Content    = response
            };

            transmitter.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, null, wrapper));

            Assert.AreEqual(0, transmitter.BackoffLogicManager.ConsecutiveErrors);
            Assert.AreEqual(0, enqueuedTransmissions.Count);
        }
            private static WebException CreateException(int statusCode)
            {
                string content        = BackendResponseHelper.CreateBackendResponse(3, 1, new [] { "500" });
                var    bytes          = Encoding.UTF8.GetBytes(content);
                var    responseStream = new MemoryStream();

                responseStream.Write(bytes, 0, bytes.Length);
                responseStream.Seek(0, SeekOrigin.Begin);

#if NETCOREAPP1_1
                System.Net.Http.HttpResponseMessage responseMessage = new System.Net.Http.HttpResponseMessage((HttpStatusCode)statusCode);
                responseMessage.Content = new System.Net.Http.StreamContent(responseStream);

                ConstructorInfo ctor        = typeof(HttpWebResponse).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[0];
                HttpWebResponse webResponse = (HttpWebResponse)ctor.Invoke(new object[] { responseMessage, null, null });

                return(new WebException("Transmitter Error", null, WebExceptionStatus.UnknownError, webResponse));
#else
                var mockWebResponse = new Moq.Mock <HttpWebResponse>();
                mockWebResponse.Setup(c => c.GetResponseStream()).Returns(responseStream);

                mockWebResponse.SetupGet <HttpStatusCode>((webRes) => webRes.StatusCode).Returns((HttpStatusCode)statusCode);

                return(new WebException("Transmitter Error", null, WebExceptionStatus.UnknownError, mockWebResponse.Object));
#endif
            }
Exemple #8
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);
            }
Exemple #9
0
            private static WebException CreateException(int statusCode)
            {
                string content        = BackendResponseHelper.CreateBackendResponse(3, 1, new [] { "500" });
                var    bytes          = Encoding.UTF8.GetBytes(content);
                var    responseStream = new MemoryStream();

                responseStream.Write(bytes, 0, bytes.Length);
                responseStream.Seek(0, SeekOrigin.Begin);

                var mockWebResponse = new Moq.Mock <HttpWebResponse>();

                mockWebResponse.Setup(c => c.GetResponseStream()).Returns(responseStream);

                mockWebResponse.SetupGet <HttpStatusCode>((webRes) => webRes.StatusCode).Returns((HttpStatusCode)statusCode);

                return(new WebException("Transmitter Error", null, WebExceptionStatus.UnknownError, mockWebResponse.Object));
            }
            public void RestoresOriginalComponentCapacityInCaseOfOnePolicyRunningByTimerTwoTimes()
            {
                IList <Transmission> enqueuedTransmissions = new List <Transmission>();

                var sender = new StubTransmissionSender {
                    Capacity = 2
                };
                var buffer = new StubTransmissionBuffer {
                    Capacity = 1
                };
                var storage = new StubTransmissionStorage {
                    Capacity = 100
                };

                var         policies    = new[] { new StubTransmissionScheduledPolicy() };
                Transmitter transmitter = CreateTransmitter(sender, buffer, storage, policies: policies);

                var items = new List <ITelemetry> {
                    new EventTelemetry(), new EventTelemetry()
                };
                Transmission transmission = new Transmission(new Uri("http://uri"), items, "type", "encoding");

                string response = BackendResponseHelper.CreateBackendResponse(
                    itemsReceived: 2,
                    itemsAccepted: 1,
                    errorCodes: new[] { "500" });

                var wrapper = new HttpWebResponseWrapper
                {
                    StatusCode = 500,
                    Content    = response
                };

                sender.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, null, wrapper));
                sender.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, null, wrapper));
                sender.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, null, wrapper));

                Assert.IsTrue(policies[0].ActionInvoked.Wait(3000));

                Assert.AreNotEqual(0, sender.Capacity);
                Assert.AreNotEqual(0, buffer.Capacity);
            }
Exemple #11
0
        public void NewTransmissionCreatedByIndexesFromResponse()
        {
            IList <Transmission> enqueuedTransmissions = new List <Transmission>();
            var transmitter = new StubTransmitter
            {
                OnEnqueue = t => { enqueuedTransmissions.Add(t); }
            };

            var policy = new PartialSuccessTransmissionPolicy();

            policy.Initialize(transmitter);

            var items = new List <ITelemetry>
            {
                new EventTelemetry("1"),
                new EventTelemetry("2"),
                new EventTelemetry("3"),
            };
            Transmission transmission = new Transmission(new Uri("http://uri"), items, "type", "encoding");

            string response = BackendResponseHelper.CreateBackendResponse(
                itemsReceived: 3,
                itemsAccepted: 1,
                errorCodes: new[] { "439", "439" });

            var wrapper = new HttpWebResponseWrapper
            {
                StatusCode = 206,
                Content    = response
            };

            transmitter.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, null, wrapper));

            string[] newItems = JsonSerializer
                                .Deserialize(enqueuedTransmissions[0].Content)
                                .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual(2, newItems.Length);
            Assert.IsTrue(newItems[0].Contains("\"name\":\"1\""));
            Assert.IsTrue(newItems[1].Contains("\"name\":\"2\""));
        }
Exemple #12
0
        public void ItemsAreEnqueuedOnFlushAsync()
        {
            IList <Transmission> enqueuedTransmissions = new List <Transmission>();
            var transmitter = new StubTransmitter
            {
                OnEnqueue = t => { enqueuedTransmissions.Add(t); }
            };

            var policy = new PartialSuccessTransmissionPolicy();

            policy.Initialize(transmitter);

            var items = new List <ITelemetry> {
                new EventTelemetry(), new EventTelemetry()
            };
            Transmission transmission = new Transmission(new Uri("http://uri"), items, "type", "encoding")
            {
                IsFlushAsyncInProgress = true
            };

            string response = BackendResponseHelper.CreateBackendResponse(
                itemsReceived: 2,
                itemsAccepted: 1,
                errorCodes: new[] { "429" });

            var wrapper = new HttpWebResponseWrapper
            {
                StatusCode = 206,
                Content    = response
            };

            transmitter.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, null, wrapper));

            Assert.AreEqual(1, enqueuedTransmissions.Count);
            Assert.IsTrue(transmission.IsFlushAsyncInProgress);
        }