Skip to content

Commit 66b23c3

Browse files
Add InstanceId to communication with dotnet test (#5279)
1 parent 1ba0a58 commit 66b23c3

File tree

11 files changed

+65
-17
lines changed

11 files changed

+65
-17
lines changed

src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/DotnetTestConnection.cs

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ internal sealed class DotnetTestConnection : IPushOnlyProtocol,
2727

2828
private NamedPipeClient? _dotnetTestPipeClient;
2929

30+
public static string InstanceId { get; } = Guid.NewGuid().ToString("N");
31+
3032
public DotnetTestConnection(CommandLineHandler commandLineHandler, IProcessHandler processHandler, IEnvironment environment, ITestApplicationModuleInfo testApplicationModuleInfo, ITestApplicationCancellationTokenSource cancellationTokenSource)
3133
{
3234
_commandLineHandler = commandLineHandler;
@@ -103,6 +105,7 @@ public async Task<bool> IsCompatibleProtocolAsync(string hostType)
103105
{ HandshakeMessagePropertyNames.HostType, hostType },
104106
{ HandshakeMessagePropertyNames.ModulePath, _testApplicationModuleInfo?.GetCurrentTestApplicationFullPath() ?? string.Empty },
105107
{ HandshakeMessagePropertyNames.ExecutionId, _environment.GetEnvironmentVariable(EnvironmentVariableConstants.TESTINGPLATFORM_DOTNETTEST_EXECUTIONID) ?? string.Empty },
108+
{ HandshakeMessagePropertyNames.InstanceId, InstanceId },
106109
});
107110

108111
HandshakeMessage response = await _dotnetTestPipeClient.RequestReplyAsync<HandshakeMessage, HandshakeMessage>(handshakeMessage, _cancellationTokenSource.CancellationToken);

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

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ internal static class HandshakeMessagePropertyNames
3030
internal const byte HostType = 5;
3131
internal const byte ModulePath = 6;
3232
internal const byte ExecutionId = 7;
33+
internal const byte InstanceId = 8;
3334
}
3435

3536
internal static class ProtocolConstants

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
6060
new DiscoveredTestMessage(
6161
testNodeUpdateMessage.TestNode.Uid.Value,
6262
testNodeUpdateMessage.TestNode.DisplayName),
63-
});
63+
},
64+
DotnetTestConnection.InstanceId);
6465

6566
await _dotnetTestConnection.SendMessageAsync(discoveredTestMessages);
6667
break;
@@ -81,7 +82,8 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
8182
testNodeDetails.StandardError ?? string.Empty,
8283
testNodeUpdateMessage.SessionUid.Value),
8384
},
84-
Array.Empty<FailedTestResultMessage>());
85+
Array.Empty<FailedTestResultMessage>(),
86+
DotnetTestConnection.InstanceId);
8587

8688
await _dotnetTestConnection.SendMessageAsync(testResultMessages);
8789
break;
@@ -105,7 +107,8 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
105107
testNodeDetails.StandardOutput ?? string.Empty,
106108
testNodeDetails.StandardError ?? string.Empty,
107109
testNodeUpdateMessage.SessionUid.Value),
108-
});
110+
},
111+
DotnetTestConnection.InstanceId);
109112

110113
await _dotnetTestConnection.SendMessageAsync(testResultMessages);
111114
break;
@@ -125,7 +128,8 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
125128
testNodeFileArtifact.TestNode.Uid.Value,
126129
testNodeFileArtifact.TestNode.DisplayName,
127130
testNodeFileArtifact.SessionUid.Value),
128-
});
131+
},
132+
DotnetTestConnection.InstanceId);
129133

130134
await _dotnetTestConnection.SendMessageAsync(fileArtifactMessages);
131135
break;
@@ -142,7 +146,8 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
142146
string.Empty,
143147
string.Empty,
144148
sessionFileArtifact.SessionUid.Value),
145-
});
149+
},
150+
DotnetTestConnection.InstanceId);
146151

147152
await _dotnetTestConnection.SendMessageAsync(fileArtifactMessages);
148153
break;
@@ -159,7 +164,8 @@ public async Task ConsumeAsync(IDataProducer dataProducer, IData value, Cancella
159164
string.Empty,
160165
string.Empty,
161166
string.Empty),
162-
});
167+
},
168+
DotnetTestConnection.InstanceId);
163169

164170
await _dotnetTestConnection.SendMessageAsync(fileArtifactMessages);
165171
break;

src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/Models/DiscoveredTestMessages.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ namespace Microsoft.Testing.Platform.IPC.Models;
55

66
internal sealed record DiscoveredTestMessage(string? Uid, string? DisplayName);
77

8-
internal sealed record DiscoveredTestMessages(string? ExecutionId, DiscoveredTestMessage[] DiscoveredMessages) : IRequest;
8+
internal sealed record DiscoveredTestMessages(string? ExecutionId, DiscoveredTestMessage[] DiscoveredMessages, string? InstanceId) : IRequest;

src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/Models/FileArtifactMessages.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ namespace Microsoft.Testing.Platform.IPC.Models;
55

66
internal sealed record FileArtifactMessage(string? FullPath, string? DisplayName, string? Description, string? TestUid, string? TestDisplayName, string? SessionUid);
77

8-
internal sealed record FileArtifactMessages(string? ExecutionId, FileArtifactMessage[] FileArtifacts) : IRequest;
8+
internal sealed record FileArtifactMessages(string? ExecutionId, FileArtifactMessage[] FileArtifacts, string? InstanceId) : IRequest;

src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/Models/TestResultMessages.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ internal sealed record SuccessfulTestResultMessage(string? Uid, string? DisplayN
77

88
internal sealed record FailedTestResultMessage(string? Uid, string? DisplayName, byte? State, long? Duration, string? Reason, ExceptionMessage[]? Exceptions, string? StandardOutput, string? ErrorOutput, string? SessionUid);
99

10-
internal sealed record TestResultMessages(string? ExecutionId, SuccessfulTestResultMessage[] SuccessfulTestMessages, FailedTestResultMessage[] FailedTestMessages) : IRequest;
11-
1210
internal sealed record ExceptionMessage(string? ErrorMessage, string? ErrorType, string? StackTrace);
11+
12+
internal sealed record TestResultMessages(string? ExecutionId, SuccessfulTestResultMessage[] SuccessfulTestMessages, FailedTestResultMessage[] FailedTestMessages, string? InstanceId) : IRequest;

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

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ internal static class DiscoveredTestMessagesFieldsId
4444

4545
public const ushort ExecutionId = 1;
4646
public const ushort DiscoveredTestMessageList = 2;
47+
public const ushort InstanceId = 3;
4748
}
4849

4950
internal static class DiscoveredTestMessageFieldsId
@@ -59,6 +60,7 @@ internal static class TestResultMessagesFieldsId
5960
public const ushort ExecutionId = 1;
6061
public const ushort SuccessfulTestMessageList = 2;
6162
public const ushort FailedTestMessageList = 3;
63+
public const ushort InstanceId = 4;
6264
}
6365

6466
internal static class SuccessfulTestResultMessageFieldsId
@@ -99,6 +101,7 @@ internal static class FileArtifactMessagesFieldsId
99101

100102
public const ushort ExecutionId = 1;
101103
public const ushort FileArtifactMessageList = 2;
104+
public const ushort InstanceId = 3;
102105
}
103106

104107
internal static class FileArtifactMessageFieldsId

src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/Serializers/DiscoveredTestMessagesSerializer.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ namespace Microsoft.Testing.Platform.IPC.Serializers;
2626
|---DiscoveredTestMessageList[0].DisplayName Id---| (2 bytes)
2727
|---DiscoveredTestMessageList[0].DisplayName Size---| (4 bytes)
2828
|---DiscoveredTestMessageList[0].DisplayName Value---| (n bytes)
29+
30+
|---InstanceId---| (2 bytes)
31+
|---InstanceId Size---| (4 bytes)
32+
|---InstanceId Value---| (n bytes)
2933
*/
3034

3135
internal sealed class DiscoveredTestMessagesSerializer : BaseSerializer, INamedPipeSerializer
@@ -36,6 +40,7 @@ public object Deserialize(Stream stream)
3640
{
3741
string? executionId = null;
3842
List<DiscoveredTestMessage>? discoveredTestMessages = null;
43+
string? instanceId = null;
3944

4045
ushort fieldCount = ReadShort(stream);
4146

@@ -54,14 +59,18 @@ public object Deserialize(Stream stream)
5459
discoveredTestMessages = ReadDiscoveredTestMessagesPayload(stream);
5560
break;
5661

62+
case DiscoveredTestMessagesFieldsId.InstanceId:
63+
instanceId = ReadStringValue(stream, fieldSize);
64+
break;
65+
5766
default:
5867
// If we don't recognize the field id, skip the payload corresponding to that field
5968
SetPosition(stream, stream.Position + fieldSize);
6069
break;
6170
}
6271
}
6372

64-
return new DiscoveredTestMessages(executionId, discoveredTestMessages is null ? [] : [.. discoveredTestMessages]);
73+
return new DiscoveredTestMessages(executionId, discoveredTestMessages is null ? [] : [.. discoveredTestMessages], instanceId);
6574
}
6675

6776
private static List<DiscoveredTestMessage> ReadDiscoveredTestMessagesPayload(Stream stream)
@@ -112,6 +121,7 @@ public void Serialize(object objectToSerialize, Stream stream)
112121

113122
WriteField(stream, DiscoveredTestMessagesFieldsId.ExecutionId, discoveredTestMessages.ExecutionId);
114123
WriteDiscoveredTestMessagesPayload(stream, discoveredTestMessages.DiscoveredMessages);
124+
WriteField(stream, DiscoveredTestMessagesFieldsId.InstanceId, discoveredTestMessages.InstanceId);
115125
}
116126

117127
private static void WriteDiscoveredTestMessagesPayload(Stream stream, DiscoveredTestMessage[]? discoveredTestMessageList)
@@ -144,7 +154,8 @@ private static void WriteDiscoveredTestMessagesPayload(Stream stream, Discovered
144154

145155
private static ushort GetFieldCount(DiscoveredTestMessages discoveredTestMessages) =>
146156
(ushort)((discoveredTestMessages.ExecutionId is null ? 0 : 1) +
147-
(IsNullOrEmpty(discoveredTestMessages.DiscoveredMessages) ? 0 : 1));
157+
(IsNullOrEmpty(discoveredTestMessages.DiscoveredMessages) ? 0 : 1) +
158+
(discoveredTestMessages.InstanceId is null ? 0 : 1));
148159

149160
private static ushort GetFieldCount(DiscoveredTestMessage discoveredTestMessage) =>
150161
(ushort)((discoveredTestMessage.Uid is null ? 0 : 1) +

src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/Serializers/FileArtifactMessagesSerializer.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ namespace Microsoft.Testing.Platform.IPC.Serializers;
4242
|---FileArtifactMessageList[0].SessionUid Id---| (2 bytes)
4343
|---FileArtifactMessageList[0].SessionUid Size---| (4 bytes)
4444
|---FileArtifactMessageList[0].SessionUid Value---| (n bytes)
45+
46+
|---InstanceId---| (2 bytes)
47+
|---InstanceId Size---| (4 bytes)
48+
|---InstanceId Value---| (n bytes)
4549
*/
4650

4751
internal sealed class FileArtifactMessagesSerializer : BaseSerializer, INamedPipeSerializer
@@ -52,6 +56,7 @@ public object Deserialize(Stream stream)
5256
{
5357
string? executionId = null;
5458
List<FileArtifactMessage>? fileArtifactMessages = null;
59+
string? instanceId = null;
5560

5661
ushort fieldCount = ReadShort(stream);
5762

@@ -70,14 +75,18 @@ public object Deserialize(Stream stream)
7075
fileArtifactMessages = ReadFileArtifactMessagesPayload(stream);
7176
break;
7277

78+
case FileArtifactMessagesFieldsId.InstanceId:
79+
instanceId = ReadStringValue(stream, fieldSize);
80+
break;
81+
7382
default:
7483
// If we don't recognize the field id, skip the payload corresponding to that field
7584
SetPosition(stream, stream.Position + fieldSize);
7685
break;
7786
}
7887
}
7988

80-
return new FileArtifactMessages(executionId, fileArtifactMessages is null ? [] : [.. fileArtifactMessages]);
89+
return new FileArtifactMessages(executionId, fileArtifactMessages is null ? [] : [.. fileArtifactMessages], instanceId);
8190
}
8291

8392
private static List<FileArtifactMessage> ReadFileArtifactMessagesPayload(Stream stream)
@@ -144,6 +153,7 @@ public void Serialize(object objectToSerialize, Stream stream)
144153

145154
WriteField(stream, FileArtifactMessagesFieldsId.ExecutionId, fileArtifactMessages.ExecutionId);
146155
WriteFileArtifactMessagesPayload(stream, fileArtifactMessages.FileArtifacts);
156+
WriteField(stream, FileArtifactMessagesFieldsId.InstanceId, fileArtifactMessages.InstanceId);
147157
}
148158

149159
private static void WriteFileArtifactMessagesPayload(Stream stream, FileArtifactMessage[]? fileArtifactMessageList)
@@ -180,7 +190,8 @@ private static void WriteFileArtifactMessagesPayload(Stream stream, FileArtifact
180190

181191
private static ushort GetFieldCount(FileArtifactMessages fileArtifactMessages) =>
182192
(ushort)((fileArtifactMessages.ExecutionId is null ? 0 : 1) +
183-
(IsNullOrEmpty(fileArtifactMessages.FileArtifacts) ? 0 : 1));
193+
(IsNullOrEmpty(fileArtifactMessages.FileArtifacts) ? 0 : 1) +
194+
(fileArtifactMessages.InstanceId is null ? 0 : 1));
184195

185196
private static ushort GetFieldCount(FileArtifactMessage fileArtifactMessage) =>
186197
(ushort)((fileArtifactMessage.FullPath is null ? 0 : 1) +

src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/Serializers/TestResultMessagesSerializer.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ namespace Microsoft.Testing.Platform.IPC.Serializers;
9797
|---FailedTestMessageList[0].SessionUid Id---| (2 bytes)
9898
|---FailedTestMessageList[0].SessionUid Size---| (4 bytes)
9999
|---FailedTestMessageList[0].SessionUid Value---| (n bytes)
100+
101+
|---InstanceId Id---| (2 bytes)
102+
|---InstanceId Size---| (4 bytes)
103+
|---InstanceId Value---| (n bytes)
104+
100105
*/
101106

102107
internal sealed class TestResultMessagesSerializer : BaseSerializer, INamedPipeSerializer
@@ -108,6 +113,7 @@ public object Deserialize(Stream stream)
108113
string? executionId = null;
109114
List<SuccessfulTestResultMessage>? successfulTestResultMessages = null;
110115
List<FailedTestResultMessage>? failedTestResultMessages = null;
116+
string? instanceId = null;
111117

112118
ushort fieldCount = ReadShort(stream);
113119

@@ -126,6 +132,10 @@ public object Deserialize(Stream stream)
126132
successfulTestResultMessages = ReadSuccessfulTestMessagesPayload(stream);
127133
break;
128134

135+
case TestResultMessagesFieldsId.InstanceId:
136+
instanceId = ReadStringValue(stream, fieldSize);
137+
break;
138+
129139
case TestResultMessagesFieldsId.FailedTestMessageList:
130140
failedTestResultMessages = ReadFailedTestMessagesPayload(stream);
131141
break;
@@ -140,7 +150,8 @@ public object Deserialize(Stream stream)
140150
return new TestResultMessages(
141151
executionId,
142152
successfulTestResultMessages is null ? [] : [.. successfulTestResultMessages],
143-
failedTestResultMessages is null ? [] : [.. failedTestResultMessages]);
153+
failedTestResultMessages is null ? [] : [.. failedTestResultMessages],
154+
instanceId);
144155
}
145156

146157
private static List<SuccessfulTestResultMessage> ReadSuccessfulTestMessagesPayload(Stream stream)
@@ -327,6 +338,7 @@ public void Serialize(object objectToSerialize, Stream stream)
327338
WriteField(stream, TestResultMessagesFieldsId.ExecutionId, testResultMessages.ExecutionId);
328339
WriteSuccessfulTestMessagesPayload(stream, testResultMessages.SuccessfulTestMessages);
329340
WriteFailedTestMessagesPayload(stream, testResultMessages.FailedTestMessages);
341+
WriteField(stream, TestResultMessagesFieldsId.InstanceId, testResultMessages.InstanceId);
330342
}
331343

332344
private static void WriteSuccessfulTestMessagesPayload(Stream stream, SuccessfulTestResultMessage[]? successfulTestResultMessages)
@@ -430,7 +442,8 @@ private static void WriteExceptionMessagesPayload(Stream stream, ExceptionMessag
430442
private static ushort GetFieldCount(TestResultMessages testResultMessages) =>
431443
(ushort)((testResultMessages.ExecutionId is null ? 0 : 1) +
432444
(IsNullOrEmpty(testResultMessages.SuccessfulTestMessages) ? 0 : 1) +
433-
(IsNullOrEmpty(testResultMessages.FailedTestMessages) ? 0 : 1));
445+
(IsNullOrEmpty(testResultMessages.FailedTestMessages) ? 0 : 1) +
446+
(testResultMessages.InstanceId is null ? 0 : 1));
434447

435448
private static ushort GetFieldCount(SuccessfulTestResultMessage successfulTestResultMessage) =>
436449
(ushort)((successfulTestResultMessage.Uid is null ? 0 : 1) +

test/UnitTests/Microsoft.Testing.Platform.UnitTests/IPC/ProtocolTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void TestResultMessagesSerializeDeserialize()
1414
{
1515
var success = new SuccessfulTestResultMessage("uid", "displayName", 1, 100, "reason", "standardOutput", "errorOutput", "sessionUid");
1616
var fail = new FailedTestResultMessage("uid", "displayName", 2, 200, "reason", [new ExceptionMessage("errorMessage", "errorType", "stackTrace")], "standardOutput", "errorOutput", "sessionUid");
17-
var message = new TestResultMessages("executionId", new[] { success }, new[] { fail });
17+
var message = new TestResultMessages("executionId", new[] { success }, new[] { fail }, "instanceId");
1818

1919
var stream = new MemoryStream();
2020
new TestResultMessagesSerializer().Serialize(message, stream);

0 commit comments

Comments
 (0)