Skip to content

Commit 3444490

Browse files
EvangelinkCopilot
andauthored
Stabilize MTP disconnect and startup regression tests (#12198)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent fe8a985 commit 3444490

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

‎test/UnitTests/Microsoft.Testing.Platform.ServerMode.Client.Sources.UnitTests/MtpServerProcessTests.cs‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,19 @@ public async Task StartAsyncWhenManagedAssemblyIsMissingReportsEarlyExit()
206206
{
207207
using var temp = TempDirectory.Create();
208208
string source = Path.Combine(temp.Path, "Missing.dll");
209-
var connectionTimeout = TimeSpan.FromSeconds(30);
210-
// Allow process startup and stderr-drain delays on loaded agents while retaining a ten-second gap that
211-
// distinguishes early-exit detection from waiting for the full connection timeout.
212-
var maximumExpectedDuration = TimeSpan.FromSeconds(20);
213-
var options = new MtpServerClientOptions { ConnectionTimeout = connectionTimeout };
214-
var stopwatch = Stopwatch.StartNew();
209+
var options = new MtpServerClientOptions { ConnectionTimeout = TimeSpan.FromMinutes(5) };
210+
using var cancellation = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
211+
// Bound a missing exit probe, not startup performance: allow a minute for muxer startup and stderr
212+
// draining on loaded agents, well below the five-minute connection timeout.
213+
var startupHangTimeout = TimeSpan.FromMinutes(1);
214+
cancellation.CancelAfter(startupHangTimeout);
215215

216216
MtpServerConnectionClosedException exception = await Assert.ThrowsExactlyAsync<MtpServerConnectionClosedException>(
217-
() => MtpServerProcess.StartAsync(source, options, TestContext.CancellationToken));
218-
stopwatch.Stop();
217+
() => MtpServerProcess.StartAsync(source, options, cancellation.Token));
219218

220219
Assert.Contains("exited with code", exception.Message);
221220
Assert.Contains("before connecting back", exception.Message);
222-
Assert.IsLessThan(
223-
maximumExpectedDuration,
224-
stopwatch.Elapsed,
225-
"A process that exits during startup must be reported immediately instead of waiting for the connection timeout.");
221+
Assert.DoesNotContain("did not connect back within", exception.Message);
226222
}
227223

228224
[TestMethod]

‎test/UnitTests/Microsoft.Testing.Platform.UnitTests/IPC/IPCTests.cs‎

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using System.IO.Pipes;
5+
46
using Microsoft.Testing.Platform.Helpers;
57
using Microsoft.Testing.Platform.IPC;
68
using Microsoft.Testing.Platform.IPC.Models;
@@ -301,7 +303,7 @@ await namedPipeClient.RequestReplyAsync<TestHostCompletedRequest, VoidResponse>(
301303
public async Task SingleConnectionNamedPipeServer_ClientDisconnectsBeforeReply_LogsConciseClosure()
302304
{
303305
PipeNameDescription pipeNameDescription = NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N"));
304-
TaskCompletionSource<bool> requestReceived = new(TaskCreationOptions.RunContinuationsAsynchronously);
306+
TaskCompletionSource<IRequest> requestReceived = new(TaskCreationOptions.RunContinuationsAsynchronously);
305307
TaskCompletionSource<bool> releaseResponse = new(TaskCreationOptions.RunContinuationsAsynchronously);
306308
TaskCompletionSource<string> loggedMessage = new(TaskCreationOptions.RunContinuationsAsynchronously);
307309
Mock<ILogger> logger = new();
@@ -319,35 +321,42 @@ public async Task SingleConnectionNamedPipeServer_ClientDisconnectsBeforeReply_L
319321

320322
NamedPipeServer server = new(
321323
pipeNameDescription,
322-
async _ =>
324+
async request =>
323325
{
324-
requestReceived.TrySetResult(true);
326+
requestReceived.TrySetResult(request);
325327
await releaseResponse.Task.ConfigureAwait(false);
326328
return VoidResponse.CachedInstance;
327329
},
328330
new SystemEnvironment(),
329331
logger.Object,
330332
new SystemTask(),
331333
_testContext.CancellationToken);
332-
server.RegisterSerializer(new IntMessageSerializer(), typeof(IntMessage));
334+
IntMessageSerializer requestSerializer = new();
335+
server.RegisterSerializer(requestSerializer, typeof(IntMessage));
333336
server.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse));
334-
NamedPipeClient client = new(
337+
// This server-side scenario needs no client reply read. Dispose the idle peer before releasing the
338+
// response, without racing the client's pending read and its buffer/semaphore cleanup.
339+
NamedPipeClientStream client = new(
340+
".",
335341
pipeNameDescription.Name,
336-
new SystemEnvironment(),
337-
exitProcessOnConnectionLoss: false);
338-
client.RegisterSerializer(new IntMessageSerializer(), typeof(IntMessage));
339-
client.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse));
342+
PipeDirection.InOut,
343+
PipeOptions.Asynchronous);
340344

341345
try
342346
{
343347
Task waitConnectionTask = server.WaitConnectionAsync(_testContext.CancellationToken);
344348
await client.ConnectAsync(_testContext.CancellationToken);
345349
await waitConnectionTask;
346350

347-
Task<VoidResponse> requestTask = client.RequestReplyAsync<IntMessage, VoidResponse>(
348-
new IntMessage(42),
349-
_testContext.CancellationToken);
351+
byte[] requestBytes =
352+
[
353+
.. BitConverter.GetBytes(sizeof(int) * 2),
354+
.. BitConverter.GetBytes(requestSerializer.Id),
355+
.. BitConverter.GetBytes(42),
356+
];
357+
await client.WriteAsync(requestBytes, 0, requestBytes.Length, _testContext.CancellationToken);
350358
await requestReceived.Task.TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout);
359+
Assert.AreEqual(new IntMessage(42), await requestReceived.Task);
351360
client.Dispose();
352361
releaseResponse.TrySetResult(true);
353362

@@ -356,11 +365,9 @@ public async Task SingleConnectionNamedPipeServer_ClientDisconnectsBeforeReply_L
356365
Assert.AreEqual(
357366
$"Pipe '{pipeNameDescription.Name}' closed while writing reply; exiting server loop.",
358367
message);
359-
Task completedRequestTask = await Task.WhenAny(
360-
requestTask,
361-
Task.Delay(TimeoutHelper.DefaultHangTimeSpanTimeout, _testContext.CancellationToken));
362-
Assert.AreSame(requestTask, completedRequestTask, "The client request did not observe the closed pipe.");
363-
await Assert.ThrowsExactlyAsync<ObjectDisposedException>(() => requestTask);
368+
Assert.IsTrue(
369+
await server.WaitForDisconnectAsync(TimeoutHelper.DefaultHangTimeSpanTimeout),
370+
"The server loop did not observe the closed pipe.");
364371
}
365372
finally
366373
{

0 commit comments

Comments
 (0)