Skip to content

Commit

Permalink
make RegisterSerializer generic
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Dec 21, 2024
1 parent 5e4d216 commit 256b8d2
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActivityIndicatorMutexNameRequestSerializer, ActivityIndicatorMutexNameRequest>();
_namedPipeClient.RegisterSerializer<VoidResponseSerializer, VoidResponse>();
_namedPipeClient.RegisterSerializer<SessionEndSerializerRequestSerializer, SessionEndSerializerRequest>();
_namedPipeClient.RegisterSerializer<ConsumerPipeNameRequestSerializer, ConsumerPipeNameRequest>();
}
}

Expand Down Expand Up @@ -120,10 +120,10 @@ await _namedPipeClient.RequestReplyAsync<ActivityIndicatorMutexNameRequest, Void
_pipeNameDescription = NamedPipeServer.GetPipeName(Guid.NewGuid().ToString("N"));
_logger.LogTrace($"Hang dump pipe name: '{_pipeNameDescription.Name}'");
_singleConnectionNamedPipeServer = new(_pipeNameDescription, CallbackAsync, _environment, _logger, _task, cancellationToken);
_singleConnectionNamedPipeServer.RegisterSerializer(new GetInProgressTestsResponseSerializer(), typeof(GetInProgressTestsResponse));
_singleConnectionNamedPipeServer.RegisterSerializer(new GetInProgressTestsRequestSerializer(), typeof(GetInProgressTestsRequest));
_singleConnectionNamedPipeServer.RegisterSerializer(new ExitSignalActivityIndicatorTaskRequestSerializer(), typeof(ExitSignalActivityIndicatorTaskRequest));
_singleConnectionNamedPipeServer.RegisterSerializer(new VoidResponseSerializer(), typeof(VoidResponse));
_singleConnectionNamedPipeServer.RegisterSerializer<GetInProgressTestsResponseSerializer, GetInProgressTestsResponse>();
_singleConnectionNamedPipeServer.RegisterSerializer<GetInProgressTestsRequestSerializer, GetInProgressTestsRequest>();
_singleConnectionNamedPipeServer.RegisterSerializer<ExitSignalActivityIndicatorTaskRequestSerializer, ExitSignalActivityIndicatorTaskRequest>();
_singleConnectionNamedPipeServer.RegisterSerializer<VoidResponseSerializer, VoidResponse>();
await _logger.LogTraceAsync($"Send consumer pipe name to the test controller '{_pipeNameDescription.Name}'");
await _namedPipeClient.RequestReplyAsync<ConsumerPipeNameRequest, VoidResponse>(new ConsumerPipeNameRequest(_pipeNameDescription.Name), cancellationToken)
.TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActivityIndicatorMutexNameRequestSerializer, ActivityIndicatorMutexNameRequest>();
_singleConnectionNamedPipeServer.RegisterSerializer<VoidResponseSerializer, VoidResponse>();
_singleConnectionNamedPipeServer.RegisterSerializer<SessionEndSerializerRequestSerializer, SessionEndSerializerRequest>();
_singleConnectionNamedPipeServer.RegisterSerializer<ConsumerPipeNameRequestSerializer, ConsumerPipeNameRequest>();
await _logger.LogDebugAsync($"Waiting for connection to {_singleConnectionNamedPipeServer.PipeName.Name}");
await _singleConnectionNamedPipeServer.WaitConnectionAsync(cancellationToken).TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout, cancellationToken);
}, cancellationToken);
Expand Down Expand Up @@ -165,10 +165,10 @@ private async Task<IResponse> 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<GetInProgressTestsResponseSerializer, GetInProgressTestsResponse>();
_namedPipeClient.RegisterSerializer<GetInProgressTestsRequestSerializer, GetInProgressTestsRequest>();
_namedPipeClient.RegisterSerializer<ExitSignalActivityIndicatorTaskRequestSerializer, ExitSignalActivityIndicatorTaskRequest>();
_namedPipeClient.RegisterSerializer<VoidResponseSerializer, VoidResponse>();
_waitConsumerPipeName.Set();
return VoidResponse.CachedInstance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModuleInfoRequestSerializer, ModuleInfoRequest>();
PipeClient.RegisterSerializer<VoidResponseSerializer, VoidResponse>();
PipeClient.RegisterSerializer<FailedTestInfoRequestSerializer, FailedTestInfoRequest>();
PipeClient.RegisterSerializer<RunSummaryInfoRequestSerializer, RunSummaryInfoRequest>();
using var cancellationTokenSource = new CancellationTokenSource(TimeoutHelper.DefaultHangTimeSpanTimeout);
using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token, _testApplicationCancellationTokenSource.CancellationToken);
await PipeClient.ConnectAsync(linkedCancellationToken.Token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<VoidResponseSerializer, VoidResponse>();
_singleConnectionNamedPipeServer.RegisterSerializer<FailedTestRequestSerializer, FailedTestRequest>();
_singleConnectionNamedPipeServer.RegisterSerializer<GetListOfFailedTestsRequestSerializer, GetListOfFailedTestsRequest>();
_singleConnectionNamedPipeServer.RegisterSerializer<GetListOfFailedTestsResponseSerializer, GetListOfFailedTestsResponse>();
_singleConnectionNamedPipeServer.RegisterSerializer<TotalTestsRunRequestSerializer, TotalTestsRunRequest>();
_failedTests = failedTests;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<VoidResponseSerializer, VoidResponse>();
Client.RegisterSerializer<FailedTestRequestSerializer, FailedTestRequest>();
Client.RegisterSerializer<GetListOfFailedTestsRequestSerializer, GetListOfFailedTestsRequest>();
Client.RegisterSerializer<GetListOfFailedTestsResponseSerializer, GetListOfFailedTestsResponse>();
Client.RegisterSerializer<TotalTestsRunRequestSerializer, TotalTestsRunRequest>();
await Client.ConnectAsync(cancellationToken);

GetListOfFailedTestsResponse result = await Client.RequestReplyAsync<GetListOfFailedTestsRequest, GetListOfFailedTestsResponse>(new GetListOfFailedTestsRequest(), cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReportFileNameRequestSerializer, ReportFileNameRequest>();
_singleConnectionNamedPipeServer.RegisterSerializer<TestAdapterInformationRequestSerializer, TestAdapterInformationRequest>();
_singleConnectionNamedPipeServer.RegisterSerializer<VoidResponseSerializer, VoidResponse>();
await _singleConnectionNamedPipeServer.WaitConnectionAsync(cancellation).TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout, cancellation);
}, cancellation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReportFileNameRequestSerializer, ReportFileNameRequest>();
NamedPipeClient.RegisterSerializer<TestAdapterInformationRequestSerializer, TestAdapterInformationRequest>();
NamedPipeClient.RegisterSerializer<VoidResponseSerializer, VoidResponse>();

// Connect to the named pipe server
await NamedPipeClient.ConnectAsync(cancellationToken).TimeoutAfterAsync(TimeoutHelper.DefaultHangTimeSpanTimeout, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModuleInfoRequestSerializer, ModuleInfoRequest>();
pipeServer.RegisterSerializer<VoidResponseSerializer, VoidResponse>();
pipeServer.RegisterSerializer<FailedTestInfoRequestSerializer, FailedTestInfoRequest>();
pipeServer.RegisterSerializer<RunSummaryInfoRequestSerializer, RunSummaryInfoRequest>();
await pipeServer.WaitConnectionAsync(_waitForConnections.Token);
_connections.Add(pipeServer);
Log.LogMessage(MessageImportance.Low, $"Client connected to '{_pipeNameDescription.Name}'");
Expand Down
6 changes: 4 additions & 2 deletions src/Platform/Microsoft.Testing.Platform/IPC/NamedPipeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ internal abstract class NamedPipeBase
private readonly Dictionary<Type, object> _typeSerializer = [];
private readonly Dictionary<int, object> _idSerializer = [];

public void RegisterSerializer(INamedPipeSerializer namedPipeSerializer, Type type)
public void RegisterSerializer<TSerializer, TInput>()
where TSerializer : INamedPipeSerializer, new()
{
_typeSerializer.Add(type, namedPipeSerializer);
INamedPipeSerializer namedPipeSerializer = new TSerializer();
_typeSerializer.Add(typeof(TInput), namedPipeSerializer);
_idSerializer.Add(namedPipeSerializer.Id, namedPipeSerializer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<VoidResponseSerializer, VoidResponse>();
namedPipeBase.RegisterSerializer<TestHostProcessExitRequestSerializer, TestHostProcessExitRequest>();
namedPipeBase.RegisterSerializer<TestHostProcessPIDRequestSerializer, TestHostProcessPIDRequest>();
namedPipeBase.RegisterSerializer<CommandLineOptionMessagesSerializer, CommandLineOptionMessages>();
namedPipeBase.RegisterSerializer<DiscoveredTestMessagesSerializer, DiscoveredTestMessages>();
namedPipeBase.RegisterSerializer<TestResultMessagesSerializer, TestResultMessages>();
namedPipeBase.RegisterSerializer<FileArtifactMessagesSerializer, FileArtifactMessages>();
namedPipeBase.RegisterSerializer<TestSessionEventSerializer, TestSessionEvent>();
namedPipeBase.RegisterSerializer<HandshakeMessageSerializer, HandshakeMessage>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public async Task SingleConnectionNamedPipeServer_RequestReplySerialization_Succ
Queue<BaseMessage> 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<VoidResponseSerializer, VoidResponse>();
namedPipeClient.RegisterSerializer<TextMessageSerializer, TextMessage>();
namedPipeClient.RegisterSerializer<IntMessageSerializer, IntMessage>();
namedPipeClient.RegisterSerializer<LongMessageSerializer, LongMessage>();

ManualResetEventSlim manualResetEventSlim = new(false);
var clientConnected = Task.Run(async () =>
Expand Down Expand Up @@ -130,10 +130,10 @@ public async Task SingleConnectionNamedPipeServer_RequestReplySerialization_Succ
new Mock<ILogger>().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<VoidResponseSerializer, VoidResponse>();
singleConnectionNamedPipeServer.RegisterSerializer<TextMessageSerializer, TextMessage>();
singleConnectionNamedPipeServer.RegisterSerializer<IntMessageSerializer, IntMessage>();
singleConnectionNamedPipeServer.RegisterSerializer<LongMessageSerializer, LongMessage>();
await singleConnectionNamedPipeServer.WaitConnectionAsync(CancellationToken.None);
manualResetEventSlim.Wait();

Expand Down

0 comments on commit 256b8d2

Please sign in to comment.