Overview
The file src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs has grown to 795 lines, making it harder to navigate and maintain. The class is already declared as partial, which makes splitting it into focused partial-class files a natural and low-risk refactoring.
Current State
- File:
src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs
- Size: 795 lines
- Language: C#
Structural Analysis
The file contains a single internal sealed partial class ServerTestHost with the following distinct concern groups:
| Lines |
Concern |
| 57–100 |
Constructor, field declarations, and identity properties (Uid, Version, DisplayName, Description) |
| 101–186 |
Unhandled-exception handlers (OnCurrentDomainUnhandledException, OnTaskSchedulerUnobservedTaskException) and the top-level InternalRunAsync entry point |
| 188–276 |
Main server message loop (HandleMessagesAsync) — reads messages from the transport and fans them out |
| 278–411 |
Inbound request/notification dispatching (HandleNotificationAsync, HandleRequestAsync, CompleteRequest) |
| 412–637 |
Per-request test execution, metrics, and telemetry (HandleRequestCoreAsync, ExecuteRequestAsync, GetDiscoveryMetrics, GetRunMetrics) |
| 639–733 |
Outbound messaging helpers (SendErrorAsync, SendResponseAsync, SendMessageAsync, SendTestUpdateCompleteAsync, SendTestUpdateAsync, SendTelemetryEventUpdateAsync, PushDataAsync) |
| 737–794 |
Private nested helper class RpcInvocationState (cancellable invocation state machine) |
Refactoring Strategy
Because ServerTestHost is already declared partial, each split is purely additive — no imports, project files, or callers need to change.
Proposed File Splits
-
ServerTestHost.cs (trimmed)
- Contents: field declarations, constructor, identity properties (
Uid, Version, DisplayName, Description, IsInitialized, AssertInitialized), InternalRunAsync, Dispose, exception handlers
- Responsibility: Core host lifecycle and state
-
ServerTestHost.MessageLoop.cs (new partial)
- Contents:
HandleMessagesAsync, HandleNotificationAsync, HandleRequestAsync, CompleteRequest, nested RpcInvocationState
- Responsibility: Reading and dispatching inbound JSON-RPC messages
-
ServerTestHost.RequestExecution.cs (new partial)
- Contents:
HandleRequestCoreAsync, ExecuteRequestAsync, GetDiscoveryMetrics, GetRunMetrics
- Responsibility: Per-request test-run / discovery execution and telemetry metrics
-
ServerTestHost.Messaging.cs (new partial)
- Contents:
SendErrorAsync, SendResponseAsync, SendMessageAsync, SendTestUpdateCompleteAsync, SendTestUpdateAsync, SendTelemetryEventUpdateAsync, PushDataAsync
- Responsibility: Sending outbound JSON-RPC messages and notifications
Implementation Guidelines
- Preserve Behavior: All existing functionality must work identically after the split
- Maintain Public API: Keep all
public/internal symbols accessible with the same names
- No new
using directives needed: All partial files share the same namespace; group the using directives by file
- Test After Each Split: Run unit tests after each incremental partial-class extraction
- One File at a Time: Extract one logical group at a time to make code review easier
Acceptance Criteria
Priority: Medium
Effort: Small (pure code movement — no logic changes required)
Expected Impact: Improved code navigability, easier per-concern reviews, reduced merge conflicts on the host layer
Generated by Daily File Diet · sonnet46 1M · ◷
Overview
The file
src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cshas grown to 795 lines, making it harder to navigate and maintain. The class is already declared aspartial, which makes splitting it into focused partial-class files a natural and low-risk refactoring.Current State
src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.csStructural Analysis
The file contains a single
internal sealed partial class ServerTestHostwith the following distinct concern groups:Uid,Version,DisplayName,Description)OnCurrentDomainUnhandledException,OnTaskSchedulerUnobservedTaskException) and the top-levelInternalRunAsyncentry pointHandleMessagesAsync) — reads messages from the transport and fans them outHandleNotificationAsync,HandleRequestAsync,CompleteRequest)HandleRequestCoreAsync,ExecuteRequestAsync,GetDiscoveryMetrics,GetRunMetrics)SendErrorAsync,SendResponseAsync,SendMessageAsync,SendTestUpdateCompleteAsync,SendTestUpdateAsync,SendTelemetryEventUpdateAsync,PushDataAsync)RpcInvocationState(cancellable invocation state machine)Refactoring Strategy
Because
ServerTestHostis already declaredpartial, each split is purely additive — no imports, project files, or callers need to change.Proposed File Splits
ServerTestHost.cs(trimmed)Uid,Version,DisplayName,Description,IsInitialized,AssertInitialized),InternalRunAsync,Dispose, exception handlersServerTestHost.MessageLoop.cs(new partial)HandleMessagesAsync,HandleNotificationAsync,HandleRequestAsync,CompleteRequest, nestedRpcInvocationStateServerTestHost.RequestExecution.cs(new partial)HandleRequestCoreAsync,ExecuteRequestAsync,GetDiscoveryMetrics,GetRunMetricsServerTestHost.Messaging.cs(new partial)SendErrorAsync,SendResponseAsync,SendMessageAsync,SendTestUpdateCompleteAsync,SendTestUpdateAsync,SendTelemetryEventUpdateAsync,PushDataAsyncImplementation Guidelines
public/internalsymbols accessible with the same namesusingdirectives needed: All partial files share the same namespace; group theusingdirectives by fileAcceptance Criteria
./build.sh -test)Priority: Medium
Effort: Small (pure code movement — no logic changes required)
Expected Impact: Improved code navigability, easier per-concern reviews, reduced merge conflicts on the host layer