From 256b8d2d8bc693d9bc4b079b1111f23dd5b90fe8 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sat, 21 Dec 2024 19:13:42 +1100 Subject: [PATCH 1/2] make RegisterSerializer generic --- .../HangDumpActivityIndicator.cs | 16 ++++++++-------- .../HangDumpProcessLifetimeHandler.cs | 16 ++++++++-------- ...MSBuildTestApplicationLifecycleCallbacks.cs | 8 ++++---- .../RetryFailedTestsPipeServer.cs | 10 +++++----- .../RetryLifecycleCallbacks.cs | 10 +++++----- .../TrxProcessLifetimeHandler.cs | 6 +++--- .../TrxTestApplicationLifecycleCallbacks.cs | 6 +++--- .../Tasks/InvokeTestingPlatformTask.cs | 8 ++++---- .../IPC/NamedPipeBase.cs | 6 ++++-- .../IPC/Serializers/RegisterSerializers.cs | 18 +++++++++--------- .../IPC/IPCTests.cs | 16 ++++++++-------- 11 files changed, 61 insertions(+), 59 deletions(-) diff --git a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs index aeff5caa2a..c69dfb16de 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs @@ -69,10 +69,10 @@ public HangDumpActivityIndicator( string namedPipeName = _environment.GetEnvironmentVariable(pipeNameEnvironmentVariable) ?? throw new InvalidOperationException($"Expected {pipeNameEnvironmentVariable} environment variable set."); _namedPipeClient = new NamedPipeClient(namedPipeName); - _namedPipeClient.RegisterSerializer(new ActivityIndicatorMutexNameRequestSerializer(), typeof(ActivityIndicatorMutexNameRequest)); - _namedPipeClient.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); - _namedPipeClient.RegisterSerializer(new SessionEndSerializerRequestSerializer(), typeof(SessionEndSerializerRequest)); - _namedPipeClient.RegisterSerializer(new ConsumerPipeNameRequestSerializer(), typeof(ConsumerPipeNameRequest)); + _namedPipeClient.RegisterSerializer(); + _namedPipeClient.RegisterSerializer(); + _namedPipeClient.RegisterSerializer(); + _namedPipeClient.RegisterSerializer(); } } @@ -120,10 +120,10 @@ await _namedPipeClient.RequestReplyAsync(); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); await _logger.LogTraceAsync($"Send consumer pipe name to the test controller '{_pipeNameDescription.Name}'"); await _namedPipeClient.RequestReplyAsync(new ConsumerPipeNameRequest(_pipeNameDescription.Name), cancellationToken) .TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout, cancellationToken); diff --git a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs index 077c7ae384..8caa3b8c31 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs @@ -129,10 +129,10 @@ public async Task BeforeTestHostProcessStartAsync(CancellationToken cancellation async () => { _singleConnectionNamedPipeServer = new(_pipeNameDescription, CallbackAsync, _environment, _logger, _task, cancellationToken); - _singleConnectionNamedPipeServer.RegisterSerializer(new ActivityIndicatorMutexNameRequestSerializer(), typeof(ActivityIndicatorMutexNameRequest)); - _singleConnectionNamedPipeServer.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); - _singleConnectionNamedPipeServer.RegisterSerializer(new SessionEndSerializerRequestSerializer(), typeof(SessionEndSerializerRequest)); - _singleConnectionNamedPipeServer.RegisterSerializer(new ConsumerPipeNameRequestSerializer(), typeof(ConsumerPipeNameRequest)); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); await _logger.LogDebugAsync($"Waiting for connection to {_singleConnectionNamedPipeServer.PipeName.Name}"); await _singleConnectionNamedPipeServer.WaitConnectionAsync(cancellationToken).TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout, cancellationToken); }, cancellationToken); @@ -165,10 +165,10 @@ private async Task CallbackAsync(IRequest request) { await _logger.LogDebugAsync($"Consumer pipe name received '{consumerPipeNameRequest.PipeName}'"); _namedPipeClient = new NamedPipeClient(consumerPipeNameRequest.PipeName); - _namedPipeClient.RegisterSerializer(new GetInProgressTestsResponseSerializer(), typeof(GetInProgressTestsResponse)); - _namedPipeClient.RegisterSerializer(new GetInProgressTestsRequestSerializer(), typeof(GetInProgressTestsRequest)); - _namedPipeClient.RegisterSerializer(new ExitSignalActivityIndicatorTaskRequestSerializer(), typeof(ExitSignalActivityIndicatorTaskRequest)); - _namedPipeClient.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); + _namedPipeClient.RegisterSerializer(); + _namedPipeClient.RegisterSerializer(); + _namedPipeClient.RegisterSerializer(); + _namedPipeClient.RegisterSerializer(); _waitConsumerPipeName.Set(); return VoidResponse.CachedInstance; } diff --git a/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildTestApplicationLifecycleCallbacks.cs b/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildTestApplicationLifecycleCallbacks.cs index dd4a62547c..504f163e0d 100644 --- a/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildTestApplicationLifecycleCallbacks.cs +++ b/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildTestApplicationLifecycleCallbacks.cs @@ -57,10 +57,10 @@ public async Task BeforeRunAsync(CancellationToken cancellationToken) } PipeClient = new(msbuildInfo[0]); - PipeClient.RegisterSerializer(new ModuleInfoRequestSerializer(), typeof(ModuleInfoRequest)); - PipeClient.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); - PipeClient.RegisterSerializer(new FailedTestInfoRequestSerializer(), typeof(FailedTestInfoRequest)); - PipeClient.RegisterSerializer(new RunSummaryInfoRequestSerializer(), typeof(RunSummaryInfoRequest)); + PipeClient.RegisterSerializer(); + PipeClient.RegisterSerializer(); + PipeClient.RegisterSerializer(); + PipeClient.RegisterSerializer(); using var cancellationTokenSource = new CancellationTokenSource(TimeoutHelper.DefaultHangTimeSpanTimeout); using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token, _testApplicationCancellationTokenSource.CancellationToken); await PipeClient.ConnectAsync(linkedCancellationToken.Token); diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs index 21032e0497..dca3328b15 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryFailedTestsPipeServer.cs @@ -27,11 +27,11 @@ public RetryFailedTestsPipeServer(IServiceProvider serviceProvider, string[] fai serviceProvider.GetTask(), serviceProvider.GetTestApplicationCancellationTokenSource().CancellationToken); - _singleConnectionNamedPipeServer.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); - _singleConnectionNamedPipeServer.RegisterSerializer(new FailedTestRequestSerializer(), typeof(FailedTestRequest)); - _singleConnectionNamedPipeServer.RegisterSerializer(new GetListOfFailedTestsRequestSerializer(), typeof(GetListOfFailedTestsRequest)); - _singleConnectionNamedPipeServer.RegisterSerializer(new GetListOfFailedTestsResponseSerializer(), typeof(GetListOfFailedTestsResponse)); - _singleConnectionNamedPipeServer.RegisterSerializer(new TotalTestsRunRequestSerializer(), typeof(TotalTestsRunRequest)); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); _failedTests = failedTests; } diff --git a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs index 6d019d9fd1..0c582fdc2a 100644 --- a/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs +++ b/src/Platform/Microsoft.Testing.Extensions.Retry/RetryLifecycleCallbacks.cs @@ -58,11 +58,11 @@ public async Task BeforeRunAsync(CancellationToken cancellationToken) logger.LogDebug($"Connecting to pipe '{pipeName[0]}'"); Client = new(pipeName[0]); - Client.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); - Client.RegisterSerializer(new FailedTestRequestSerializer(), typeof(FailedTestRequest)); - Client.RegisterSerializer(new GetListOfFailedTestsRequestSerializer(), typeof(GetListOfFailedTestsRequest)); - Client.RegisterSerializer(new GetListOfFailedTestsResponseSerializer(), typeof(GetListOfFailedTestsResponse)); - Client.RegisterSerializer(new TotalTestsRunRequestSerializer(), typeof(TotalTestsRunRequest)); + Client.RegisterSerializer(); + Client.RegisterSerializer(); + Client.RegisterSerializer(); + Client.RegisterSerializer(); + Client.RegisterSerializer(); await Client.ConnectAsync(cancellationToken); GetListOfFailedTestsResponse result = await Client.RequestReplyAsync(new GetListOfFailedTestsRequest(), cancellationToken); diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs index 3ecf341949..87277fae8d 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs @@ -102,9 +102,9 @@ public Task BeforeTestHostProcessStartAsync(CancellationToken cancellation) async () => { _singleConnectionNamedPipeServer = new(_pipeNameDescription, CallbackAsync, _environment, _logger, _task, cancellation); - _singleConnectionNamedPipeServer.RegisterSerializer(new ReportFileNameRequestSerializer(), typeof(ReportFileNameRequest)); - _singleConnectionNamedPipeServer.RegisterSerializer(new TestAdapterInformationRequestSerializer(), typeof(TestAdapterInformationRequest)); - _singleConnectionNamedPipeServer.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); + _singleConnectionNamedPipeServer.RegisterSerializer(); await _singleConnectionNamedPipeServer.WaitConnectionAsync(cancellation).TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout, cancellation); }, cancellation); diff --git a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxTestApplicationLifecycleCallbacks.cs b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxTestApplicationLifecycleCallbacks.cs index 9102eeaa8e..208563494d 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxTestApplicationLifecycleCallbacks.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxTestApplicationLifecycleCallbacks.cs @@ -66,9 +66,9 @@ public async Task BeforeRunAsync(CancellationToken cancellationToken) string namedPipeName = _environment.GetEnvironmentVariable(TrxEnvironmentVariableProvider.TRXNAMEDPIPENAME) ?? throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, ExtensionResources.TrxReportGeneratorMissingTrxNamedPipeEnvironmentVariable, TrxEnvironmentVariableProvider.TRXNAMEDPIPENAME)); NamedPipeClient = new NamedPipeClient(namedPipeName); - NamedPipeClient.RegisterSerializer(new ReportFileNameRequestSerializer(), typeof(ReportFileNameRequest)); - NamedPipeClient.RegisterSerializer(new TestAdapterInformationRequestSerializer(), typeof(TestAdapterInformationRequest)); - NamedPipeClient.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); + NamedPipeClient.RegisterSerializer(); + NamedPipeClient.RegisterSerializer(); + NamedPipeClient.RegisterSerializer(); // Connect to the named pipe server await NamedPipeClient.ConnectAsync(cancellationToken).TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout, cancellationToken); diff --git a/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs b/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs index fbebc18061..29bb1b567e 100644 --- a/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs +++ b/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs @@ -257,10 +257,10 @@ protected override void ProcessStarted() while (!_waitForConnections.IsCancellationRequested) { NamedPipeServer pipeServer = new(_pipeNameDescription, HandleRequestAsync, new SystemEnvironment(), new MSBuildLogger(), new SystemTask(), maxNumberOfServerInstances: 100, CancellationToken.None); - pipeServer.RegisterSerializer(new ModuleInfoRequestSerializer(), typeof(ModuleInfoRequest)); - pipeServer.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); - pipeServer.RegisterSerializer(new FailedTestInfoRequestSerializer(), typeof(FailedTestInfoRequest)); - pipeServer.RegisterSerializer(new RunSummaryInfoRequestSerializer(), typeof(RunSummaryInfoRequest)); + pipeServer.RegisterSerializer(); + pipeServer.RegisterSerializer(); + pipeServer.RegisterSerializer(); + pipeServer.RegisterSerializer(); await pipeServer.WaitConnectionAsync(_waitForConnections.Token); _connections.Add(pipeServer); Log.LogMessage(MessageImportance.Low, $"Client connected to '{_pipeNameDescription.Name}'"); diff --git a/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeBase.cs b/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeBase.cs index 9f75028cd4..a4ab3ed02d 100644 --- a/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeBase.cs +++ b/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeBase.cs @@ -14,9 +14,11 @@ internal abstract class NamedPipeBase private readonly Dictionary _typeSerializer = []; private readonly Dictionary _idSerializer = []; - public void RegisterSerializer(INamedPipeSerializer namedPipeSerializer, Type type) + public void RegisterSerializer() + where TSerializer : INamedPipeSerializer, new() { - _typeSerializer.Add(type, namedPipeSerializer); + INamedPipeSerializer namedPipeSerializer = new TSerializer(); + _typeSerializer.Add(typeof(TInput), namedPipeSerializer); _idSerializer.Add(namedPipeSerializer.Id, namedPipeSerializer); } diff --git a/src/Platform/Microsoft.Testing.Platform/IPC/Serializers/RegisterSerializers.cs b/src/Platform/Microsoft.Testing.Platform/IPC/Serializers/RegisterSerializers.cs index c972c4d1b0..d0c352dc10 100644 --- a/src/Platform/Microsoft.Testing.Platform/IPC/Serializers/RegisterSerializers.cs +++ b/src/Platform/Microsoft.Testing.Platform/IPC/Serializers/RegisterSerializers.cs @@ -24,14 +24,14 @@ internal static class RegisterSerializers { public static void RegisterAllSerializers(this NamedPipeBase namedPipeBase) { - namedPipeBase.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); - namedPipeBase.RegisterSerializer(new TestHostProcessExitRequestSerializer(), typeof(TestHostProcessExitRequest)); - namedPipeBase.RegisterSerializer(new TestHostProcessPIDRequestSerializer(), typeof(TestHostProcessPIDRequest)); - namedPipeBase.RegisterSerializer(new CommandLineOptionMessagesSerializer(), typeof(CommandLineOptionMessages)); - namedPipeBase.RegisterSerializer(new DiscoveredTestMessagesSerializer(), typeof(DiscoveredTestMessages)); - namedPipeBase.RegisterSerializer(new TestResultMessagesSerializer(), typeof(TestResultMessages)); - namedPipeBase.RegisterSerializer(new FileArtifactMessagesSerializer(), typeof(FileArtifactMessages)); - namedPipeBase.RegisterSerializer(new TestSessionEventSerializer(), typeof(TestSessionEvent)); - namedPipeBase.RegisterSerializer(new HandshakeMessageSerializer(), typeof(HandshakeMessage)); + namedPipeBase.RegisterSerializer(); + namedPipeBase.RegisterSerializer(); + namedPipeBase.RegisterSerializer(); + namedPipeBase.RegisterSerializer(); + namedPipeBase.RegisterSerializer(); + namedPipeBase.RegisterSerializer(); + namedPipeBase.RegisterSerializer(); + namedPipeBase.RegisterSerializer(); + namedPipeBase.RegisterSerializer(); } } diff --git a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/IPC/IPCTests.cs b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/IPC/IPCTests.cs index fea23a67d7..50b7436a15 100644 --- a/test/UnitTests/Microsoft.Testing.Platform.UnitTests/IPC/IPCTests.cs +++ b/test/UnitTests/Microsoft.Testing.Platform.UnitTests/IPC/IPCTests.cs @@ -89,10 +89,10 @@ public async Task SingleConnectionNamedPipeServer_RequestReplySerialization_Succ Queue receivedMessages = new(); PipeNameDescription pipeNameDescription = NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N")); NamedPipeClient namedPipeClient = new(pipeNameDescription.Name); - namedPipeClient.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); - namedPipeClient.RegisterSerializer(new TextMessageSerializer(), typeof(TextMessage)); - namedPipeClient.RegisterSerializer(new IntMessageSerializer(), typeof(IntMessage)); - namedPipeClient.RegisterSerializer(new LongMessageSerializer(), typeof(LongMessage)); + namedPipeClient.RegisterSerializer(); + namedPipeClient.RegisterSerializer(); + namedPipeClient.RegisterSerializer(); + namedPipeClient.RegisterSerializer(); ManualResetEventSlim manualResetEventSlim = new(false); var clientConnected = Task.Run(async () => @@ -130,10 +130,10 @@ public async Task SingleConnectionNamedPipeServer_RequestReplySerialization_Succ new Mock().Object, new SystemTask(), CancellationToken.None); - singleConnectionNamedPipeServer.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse)); - singleConnectionNamedPipeServer.RegisterSerializer(new TextMessageSerializer(), typeof(TextMessage)); - singleConnectionNamedPipeServer.RegisterSerializer(new IntMessageSerializer(), typeof(IntMessage)); - singleConnectionNamedPipeServer.RegisterSerializer(new LongMessageSerializer(), typeof(LongMessage)); + singleConnectionNamedPipeServer.RegisterSerializer(); + singleConnectionNamedPipeServer.RegisterSerializer(); + singleConnectionNamedPipeServer.RegisterSerializer(); + singleConnectionNamedPipeServer.RegisterSerializer(); await singleConnectionNamedPipeServer.WaitConnectionAsync(CancellationToken.None); manualResetEventSlim.Wait(); From eb5f55f891e2f0910b347e545829b454d43a34a9 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sat, 21 Dec 2024 19:16:44 +1100 Subject: [PATCH 2/2] Update INamedPipeBase.cs --- src/Platform/Microsoft.Testing.Platform/IPC/INamedPipeBase.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Platform/Microsoft.Testing.Platform/IPC/INamedPipeBase.cs b/src/Platform/Microsoft.Testing.Platform/IPC/INamedPipeBase.cs index f8c85dc0de..418b47e156 100644 --- a/src/Platform/Microsoft.Testing.Platform/IPC/INamedPipeBase.cs +++ b/src/Platform/Microsoft.Testing.Platform/IPC/INamedPipeBase.cs @@ -5,5 +5,6 @@ namespace Microsoft.Testing.Platform.IPC; internal interface INamedPipeBase { - void RegisterSerializer(INamedPipeSerializer namedPipeSerializer, Type type); + void RegisterSerializer() + where TSerializer : INamedPipeSerializer, new(); }