public void Dispose_ExceptionDuringStreamRead_SendsFailedEvent()
        {
            // Arrange
            var stream = new Mock <Stream>();

            stream.Setup(s => s.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()))
            .Throws <TimeoutException>();
            ProtocolDiagnosticInProgressEvent inProgressEvent = CreateInProgressEvent();
            var stopwatch = Stopwatch.StartNew();
            ProtocolDiagnosticEvent completedEvent = null;

            void GotEvent(ProtocolDiagnosticEvent @event)
            {
                Assert.Null(completedEvent);
                completedEvent = @event;
            }

            // Act
            Action action = () =>
            {
                using (var target = new ProtocolDiagnosticsStream(stream.Object, inProgressEvent, stopwatch, GotEvent))
                {
                    ReadStream(target);
                }
            };

            Assert.Throws <TimeoutException>(action);

            // Assert
            Assert.NotNull(completedEvent);
            Assert.False(completedEvent.IsSuccess);
        }
        public void Dispose_ExceptionDuringStreamProcessing_SendsSuccessfulEvent()
        {
            // Arrange
            var memoryStream = new MemoryStream(capacity: 100);
            ProtocolDiagnosticInProgressEvent inProgressEvent = CreateInProgressEvent();
            var stopwatch = Stopwatch.StartNew();
            ProtocolDiagnosticEvent completedEvent = null;

            void GotEvent(ProtocolDiagnosticEvent @event)
            {
                Assert.Null(completedEvent);
                completedEvent = @event;
            }

            // Act
            Action action = () =>
            {
                using (var target = new ProtocolDiagnosticsStream(memoryStream, inProgressEvent, stopwatch, GotEvent))
                {
                    throw new InvalidOperationException();
                }
            };

            Assert.Throws <InvalidOperationException>(action);

            // Assert
            Assert.NotNull(completedEvent);
            Assert.True(completedEvent.IsSuccess);
        }
        public void Dispose_IncompleteStream_SendsSuccessfulEvent()
        {
            // Arrange
            var memoryStream = new MemoryStream(capacity: 100);
            ProtocolDiagnosticInProgressEvent inProgressEvent = CreateInProgressEvent();
            var stopwatch = Stopwatch.StartNew();
            ProtocolDiagnosticEvent completedEvent = null;

            void GotEvent(ProtocolDiagnosticEvent @event)
            {
                Assert.Null(completedEvent);
                completedEvent = @event;
            }

            // Act
            using (var target = new ProtocolDiagnosticsStream(memoryStream, inProgressEvent, stopwatch, GotEvent))
            {
            }

            // Assert
            Assert.NotNull(completedEvent);
            Assert.True(completedEvent.IsSuccess);
        }
Ejemplo n.º 4
0
        internal static void AddAggregateData(ProtocolDiagnosticEvent pdEvent, ConcurrentDictionary <string, Data> allData)
        {
            var data = allData.GetOrAdd(pdEvent.Source, _ => new Data());

            var resourceData = pdEvent.Url.OriginalString.EndsWith(".nupkg", StringComparison.OrdinalIgnoreCase)
                ? data.Nupkg
                : data.Metadata;

            lock (resourceData.Lock)
            {
                ApplyTiming(resourceData.EventTiming, pdEvent.EventDuration);

                if (pdEvent.HeaderDuration.HasValue)
                {
                    if (resourceData.HeaderTiming == null)
                    {
                        resourceData.HeaderTiming = new ResourceTimingData();
                    }

                    ApplyTiming(resourceData.HeaderTiming, pdEvent.HeaderDuration.Value);
                }

                if (pdEvent.IsSuccess)
                {
                    resourceData.Successful++;
                }

                if (pdEvent.IsRetry)
                {
                    resourceData.Retries++;
                }

                if (pdEvent.IsCancelled)
                {
                    resourceData.Cancelled++;
                }

                if (pdEvent.IsLastAttempt && !pdEvent.IsSuccess && !pdEvent.IsCancelled)
                {
                    resourceData.Failed++;
                }

                if (pdEvent.Bytes > 0)
                {
                    resourceData.TotalBytes += pdEvent.Bytes;
                    if (pdEvent.Bytes > resourceData.MaxBytes)
                    {
                        resourceData.MaxBytes = pdEvent.Bytes;
                    }
                }

                if (pdEvent.HttpStatusCode.HasValue)
                {
                    if (!resourceData.StatusCodes.TryGetValue(pdEvent.HttpStatusCode.Value, out var count))
                    {
                        count = 0;
                    }
                    resourceData.StatusCodes[pdEvent.HttpStatusCode.Value] = count + 1;
                }
            }
        }
Ejemplo n.º 5
0
 private void ProtocolDiagnostics_Event(ProtocolDiagnosticEvent pdEvent)
 {
     AddAggregateData(pdEvent, _data);
 }
Ejemplo n.º 6
0
 internal static void RaiseEvent(ProtocolDiagnosticEvent pdEvent)
 {
     Event?.Invoke(pdEvent);
 }