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>(); ProtocolDiagnosticInProgressHttpEvent inProgressEvent = CreateInProgressEvent(); var stopwatch = Stopwatch.StartNew(); ProtocolDiagnosticHttpEvent completedEvent = null; void GotEvent(ProtocolDiagnosticHttpEvent @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); ProtocolDiagnosticInProgressHttpEvent inProgressEvent = CreateInProgressEvent(); var stopwatch = Stopwatch.StartNew(); ProtocolDiagnosticHttpEvent completedEvent = null; void GotEvent(ProtocolDiagnosticHttpEvent @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); }
internal static void AddHttpData(ProtocolDiagnosticHttpEvent pdEvent, IReadOnlyDictionary <string, Data> allData) { if (!allData.TryGetValue(pdEvent.Source, out Data data)) { return; } lock (data._lock) { var httpData = data.Http; httpData.Requests++; httpData.TotalDuration += pdEvent.EventDuration; // If any one event header duration is null, we want the HttpData value to be null, // since the request count would otherwise be incorrect. C# nullable does this automatically for us. httpData.HeaderDuration += pdEvent.HeaderDuration; if (pdEvent.IsSuccess) { httpData.Successful++; } if (pdEvent.IsRetry) { httpData.Retries++; } if (pdEvent.IsCancelled) { httpData.Cancelled++; } if (pdEvent.IsLastAttempt && !pdEvent.IsSuccess && !pdEvent.IsCancelled) { httpData.Failed++; } if (pdEvent.Bytes > 0) { httpData.TotalBytes += pdEvent.Bytes; } if (pdEvent.HttpStatusCode.HasValue) { if (!httpData.StatusCodes.TryGetValue(pdEvent.HttpStatusCode.Value, out var count)) { count = 0; } httpData.StatusCodes[pdEvent.HttpStatusCode.Value] = count + 1; } } }
public void Dispose_IncompleteStream_SendsSuccessfulEvent() { // Arrange var memoryStream = new MemoryStream(capacity: 100); ProtocolDiagnosticInProgressHttpEvent inProgressEvent = CreateInProgressEvent(); var stopwatch = Stopwatch.StartNew(); ProtocolDiagnosticHttpEvent completedEvent = null; void GotEvent(ProtocolDiagnosticHttpEvent @event) { Assert.Null(completedEvent); completedEvent = @event; } // Act using (var target = new ProtocolDiagnosticsStream(memoryStream, inProgressEvent, stopwatch, GotEvent)) { } // Assert Assert.NotNull(completedEvent); Assert.True(completedEvent.IsSuccess); }
private void ProtocolDiagnostics_HttpEvent(ProtocolDiagnosticHttpEvent pdEvent) { AddHttpData(pdEvent, _data); }
internal static void RaiseEvent(ProtocolDiagnosticHttpEvent pdEvent) { HttpEvent?.Invoke(pdEvent); }