Skip to content

Commit 4690398

Browse files
authored
Fix OTel review comments: typo, IDisposable, deduplicate completed co… (#7510)
2 parents da4a4b0 + 2146183 commit 4690398

File tree

4 files changed

+388
-9
lines changed

4 files changed

+388
-9
lines changed

src/Platform/Microsoft.Testing.Platform/Services/TestApplicationResult.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Microsoft.Testing.Platform.Services;
1414

15-
internal sealed class TestApplicationResult : ITestApplicationProcessExitCode, IOutputDeviceDataProducer
15+
internal sealed class TestApplicationResult : ITestApplicationProcessExitCode, IOutputDeviceDataProducer, IDisposable
1616
{
1717
private readonly IOutputDevice _outputService;
1818
private readonly ICommandLineOptions _commandLineOptions;
@@ -171,4 +171,7 @@ public async Task SetTestAdapterTestSessionFailureAsync(string errorMessage, Can
171171

172172
public Statistics GetStatistics()
173173
=> new() { TotalRanTests = _totalRanTests, TotalFailedTests = _failedTestsCount };
174+
175+
public void Dispose()
176+
=> _openTelemetryResultHandler?.Dispose();
174177
}

src/Platform/Microsoft.Testing.Platform/Telemetry/OpenTelemetryResultHandler.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Microsoft.Testing.Platform.Telemetry;
88

9-
internal sealed class OpenTelemetryResultHandler
9+
internal sealed class OpenTelemetryResultHandler : IDisposable
1010
{
1111
private readonly IPlatformOpenTelemetryService _otelService;
1212
private readonly IEnvironment _environment;
@@ -16,9 +16,10 @@ internal sealed class OpenTelemetryResultHandler
1616
private readonly ICounter<int> _totalPassedTests;
1717
private readonly ICounter<int> _totalFailedTests;
1818
private readonly ICounter<int> _totalSkippedTests;
19-
private readonly ICounter<int> _totalUnknownedTests;
19+
private readonly ICounter<int> _totalUnknownTests;
2020
private readonly IHistogram<double> _totalDuration;
2121
private readonly Dictionary<TestNodeUid, IPlatformActivity> _testActivities = [];
22+
private bool _disposed;
2223

2324
public OpenTelemetryResultHandler(IPlatformOpenTelemetryService otelService, IEnvironment environment)
2425
{
@@ -30,7 +31,7 @@ public OpenTelemetryResultHandler(IPlatformOpenTelemetryService otelService, IEn
3031
_totalPassedTests = otelService.CreateCounter<int>("tests.passed");
3132
_totalFailedTests = otelService.CreateCounter<int>("tests.failed");
3233
_totalSkippedTests = otelService.CreateCounter<int>("tests.skipped");
33-
_totalUnknownedTests = otelService.CreateCounter<int>("tests.unknown");
34+
_totalUnknownTests = otelService.CreateCounter<int>("tests.unknown");
3435
_totalDuration = otelService.CreateHistogram<double>("tests.duration");
3536
}
3637

@@ -40,21 +41,18 @@ internal void NotifyDiscovered()
4041
internal void NotifyPassed(TestNode testNode, TestNodeStateProperty stateProperty)
4142
{
4243
_totalPassedTests.Add(1);
43-
_totalCompletedTests.Add(1);
4444
HandleTestResult(testNode, stateProperty);
4545
}
4646

4747
internal void NotifyFailed(TestNode testNode, TestNodeStateProperty stateProperty)
4848
{
4949
_totalFailedTests.Add(1);
50-
_totalCompletedTests.Add(1);
5150
HandleTestResult(testNode, stateProperty);
5251
}
5352

5453
internal void NotifySkipped(TestNode testNode, TestNodeStateProperty stateProperty)
5554
{
5655
_totalSkippedTests.Add(1);
57-
_totalCompletedTests.Add(1);
5856
HandleTestResult(testNode, stateProperty);
5957
}
6058

@@ -73,7 +71,23 @@ internal void NotifyInProgress(TestNode testNode, TestNodeUid? parentUid)
7371
}
7472

7573
internal void NotifyUnknown()
76-
=> _totalUnknownedTests.Add(1);
74+
=> _totalUnknownTests.Add(1);
75+
76+
public void Dispose()
77+
{
78+
if (_disposed)
79+
{
80+
return;
81+
}
82+
83+
_disposed = true;
84+
foreach (IPlatformActivity activity in _testActivities.Values)
85+
{
86+
activity.Dispose();
87+
}
88+
89+
_testActivities.Clear();
90+
}
7791

7892
private static IEnumerable<KeyValuePair<string, object?>> GetTestInitialInfo(TestNode testNode, TestNodeUid? parentUid)
7993
{
@@ -107,6 +121,8 @@ internal void NotifyUnknown()
107121

108122
private void HandleTestResult(TestNode testNode, TestNodeStateProperty stateProperty)
109123
{
124+
_totalCompletedTests.Add(1);
125+
110126
if (!_testActivities.TryGetValue(testNode.Uid, out IPlatformActivity? activity))
111127
{
112128
return;

test/UnitTests/Microsoft.Testing.Platform.UnitTests/Services/TestApplicationResultTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
namespace Microsoft.Testing.Platform.UnitTests;
1313

1414
[TestClass]
15-
public sealed class TestApplicationResultTests
15+
public sealed class TestApplicationResultTests : IDisposable
1616
{
1717
private readonly TestApplicationResult _testApplicationResult
1818
= new(new Mock<IOutputDevice>().Object, new Mock<ICommandLineOptions>().Object, new Mock<IEnvironment>().Object, new Mock<IStopPoliciesService>().Object, null);
1919

20+
public void Dispose() => _testApplicationResult.Dispose();
21+
2022
[TestMethod]
2123
public async Task GetProcessExitCodeAsync_If_All_Skipped_Returns_ZeroTestsRan()
2224
{

0 commit comments

Comments
 (0)