Skip to content

Commit 84cd819

Browse files
committed
UseSingleOrDefault for test node state
1 parent 01f5c5c commit 84cd819

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

src/Platform/Microsoft.Testing.Extensions.AzureDevOps/AzureDevOpsReporter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
8585
return;
8686
}
8787

88-
TestNodeStateProperty nodeState = nodeUpdateMessage.TestNode.Properties.Single<TestNodeStateProperty>();
88+
TestNodeStateProperty? nodeState = nodeUpdateMessage.TestNode.Properties.SingleOrDefault<TestNodeStateProperty>();
8989

9090
switch (nodeState)
9191
{

src/Platform/Microsoft.Testing.Extensions.Retry/RetryDataConsumer.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ public RetryDataConsumer(IServiceProvider serviceProvider)
4141
public async Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken)
4242
{
4343
var testNodeUpdateMessage = (TestNodeUpdateMessage)value;
44-
TestNodeStateProperty nodeState = testNodeUpdateMessage.TestNode.Properties.Single<TestNodeStateProperty>();
44+
TestNodeStateProperty? nodeState = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<TestNodeStateProperty>();
45+
if (nodeState is null)
46+
{
47+
return;
48+
}
49+
4550
if (Array.IndexOf(TestNodePropertiesCategories.WellKnownTestNodeTestRunOutcomeFailedProperties, nodeState.GetType()) != -1)
4651
{
4752
ApplicationStateGuard.Ensure(_retryFailedTestsLifecycleCallbacks is not null);

src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxDataConsumer.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationTo
115115
switch (value)
116116
{
117117
case TestNodeUpdateMessage nodeChangedMessage:
118-
TestNodeStateProperty nodeState = nodeChangedMessage.TestNode.Properties.Single<TestNodeStateProperty>();
118+
TestNodeStateProperty? nodeState = nodeChangedMessage.TestNode.Properties.SingleOrDefault<TestNodeStateProperty>();
119+
if (nodeState is null)
120+
{
121+
return Task.CompletedTask;
122+
}
123+
119124
if (nodeState is PassedTestNodeStateProperty)
120125
{
121126
_tests.Add(nodeChangedMessage);

src/Platform/Microsoft.Testing.Platform/Extensions/AbortForMaxFailedTestsExtension.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
6363
RoslynDebug.Assert(_maxFailedTests is not null);
6464
RoslynDebug.Assert(_capability is not null);
6565

66-
TestNodeStateProperty testNodeStateProperty = node.TestNode.Properties.Single<TestNodeStateProperty>();
66+
TestNodeStateProperty? testNodeStateProperty = node.TestNode.Properties.SingleOrDefault<TestNodeStateProperty>();
67+
if (testNodeStateProperty is null)
68+
{
69+
return;
70+
}
71+
6772
if (TestNodePropertiesCategories.WellKnownTestNodeTestRunOutcomeFailedProperties.Any(t => t == testNodeStateProperty.GetType()) &&
6873
++_failCount >= _maxFailedTests.Value &&
6974
// If already triggered, don't do it again.

src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/DotnetTestDataConsumer.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
4747
{
4848
case TestNodeUpdateMessage testNodeUpdateMessage:
4949

50-
TestNodeDetails testNodeDetails = GetTestNodeDetails(testNodeUpdateMessage);
50+
TestNodeDetails? testNodeDetails = GetTestNodeDetails(testNodeUpdateMessage);
51+
if (testNodeDetails is null)
52+
{
53+
return;
54+
}
5155

5256
switch (testNodeDetails.State)
5357
{
@@ -172,13 +176,18 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
172176
}
173177
}
174178

175-
private static TestNodeDetails GetTestNodeDetails(TestNodeUpdateMessage testNodeUpdateMessage)
179+
private static TestNodeDetails? GetTestNodeDetails(TestNodeUpdateMessage testNodeUpdateMessage)
176180
{
177181
byte? state = null;
178182
long? duration = null;
179183
string? reason = string.Empty;
180184
ExceptionMessage[]? exceptions = null;
181-
TestNodeStateProperty nodeState = testNodeUpdateMessage.TestNode.Properties.Single<TestNodeStateProperty>();
185+
TestNodeStateProperty? nodeState = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<TestNodeStateProperty>();
186+
if (nodeState is null)
187+
{
188+
return null;
189+
}
190+
182191
string? standardOutput = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<StandardOutputProperty>()?.StandardOutput;
183192
string? standardError = testNodeUpdateMessage.TestNode.Properties.SingleOrDefault<StandardErrorProperty>()?.StandardError;
184193

0 commit comments

Comments
 (0)