diff --git a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs index 9469725e51..80e3c4d3ea 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs @@ -66,10 +66,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(); } } @@ -117,10 +117,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 548b15f972..baa0b8935e 100644 --- a/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs +++ b/src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs @@ -124,10 +124,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); @@ -160,10 +160,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 7e830ab2b7..c102c2913c 100644 --- a/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildTestApplicationLifecycleCallbacks.cs +++ b/src/Platform/Microsoft.Testing.Extensions.MSBuild/MSBuildTestApplicationLifecycleCallbacks.cs @@ -55,10 +55,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 c73914056f..9dfafe138f 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxProcessLifetimeHandler.cs @@ -100,9 +100,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 33e0e73cff..c9e5588a31 100644 --- a/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxTestApplicationLifecycleCallbacks.cs +++ b/src/Platform/Microsoft.Testing.Extensions.TrxReport/TrxTestApplicationLifecycleCallbacks.cs @@ -64,9 +64,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 f62a9d809e..0a6db62591 100644 --- a/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs +++ b/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/InvokeTestingPlatformTask.cs @@ -252,10 +252,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/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(); } diff --git a/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeBase.cs b/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeBase.cs index 4a12606676..cf5e856017 100644 --- a/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeBase.cs +++ b/src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeBase.cs @@ -12,9 +12,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();