Skip to content

Commit

Permalink
dh: Fix MetricAggregating handler for onProcessingError
Browse files Browse the repository at this point in the history
  • Loading branch information
rmandvikar committed Sep 22, 2023
1 parent be34bfc commit 9ce4529
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/rm.DelegatingHandlers/StatsAggregatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,14 @@ protected virtual void Dispose(bool disposing)
catch (Exception ex)
{
// swallow
onProcessingError?.Invoke(ex);
try
{
onProcessingError?.Invoke(ex);
}
catch
{
// swallow
}
}

timer?.Dispose();
Expand Down
36 changes: 36 additions & 0 deletions tests/rm.DelegatingHandlersTest/MetricAggregatingHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,40 @@ public async Task Verify_OnError()

Assert.AreEqual(1, onErrorCount);
}

[Test]
public async Task Verify_OnError_If_Throws()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());

Action<Exception> onError = (ex) =>
{
throw new Exception("boom!");
};
fixture.Register<Action<Exception>>(() => onError);
var intervalInSeconds = 1;
fixture.Register<IStatsAggregatorSettings>(() =>
new StatsAggregatorSettings
{
MetricName = "dep_metric_ms",
IntervalInSeconds = intervalInSeconds,
});
fixture.Register<IStatsCalculator>(() => fixture.Create<StatsCalculator>());
var statsAggregatorBaseMock = fixture.Create<Mock<StatsAggregatorBase>>();
statsAggregatorBaseMock.Protected()
.Setup(AggregateStatsMethodName)
.Callback(() => throw new Exception("boom!"));
fixture.Register<IStatsAggregator>(() => statsAggregatorBaseMock.Object);
var metricAggregatingHandler = fixture.Build<MetricAggregatingHandler>()
.Without(x => x.InnerHandler)
.Create();
using var http200 = new HttpResponseMessage(HttpStatusCode.OK);
var shortCircuitingCannedResponseHandler = new ShortCircuitingCannedResponseHandler(http200);
using var invoker = HttpMessageInvokerFactory.Create(
metricAggregatingHandler, shortCircuitingCannedResponseHandler);

using var request = new HttpRequestMessage();
var _ = await invoker.SendAsync(request, CancellationToken.None);
invoker.Dispose();
}
}

0 comments on commit 9ce4529

Please sign in to comment.