refactor(worker): dedupe CreateEnvelope overloads and WorkerPipeClient constructors (mxaccessgw#185)

WorkerPipeSession had seven CreateEnvelope overloads that each forwarded to a
matching CreateBaseEnvelope overload; they are now one overload per body type
that stamps the header and sets the body. WorkerPipeClient's six chained
constructors collapse to one with optional arguments (null attempt timeout =
env/default, null session factory = production WorkerPipeSession). Adds
constructor-validation tests.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 13:03:28 -05:00
parent 2a91e2b083
commit 7d44ecd62d
3 changed files with 66 additions and 129 deletions
@@ -31,7 +31,7 @@ public sealed class WorkerPipeClientTests
WorkerPipeClient client = new(
connectTimeoutMilliseconds: 5000,
(stream, options) => CreateSession(stream, options));
sessionFactory: (stream, options, _) => CreateSession(stream, options));
Task clientTask = client.RunAsync(workerOptions);
await Task.Factory.FromAsync(server.BeginWaitForConnection, server.EndWaitForConnection, null);
@@ -138,6 +138,40 @@ public sealed class WorkerPipeClientTests
await Assert.ThrowsAsync<TimeoutException>(async () => await client.RunAsync(workerOptions));
}
/// <summary>Verifies that the consolidated constructor still rejects non-positive timeouts.</summary>
/// <param name="connectTimeoutMilliseconds">Overall timeout under test.</param>
/// <param name="connectAttemptTimeoutMilliseconds">Per-attempt timeout under test.</param>
/// <param name="expectedParameter">Parameter the exception must name.</param>
[Theory]
[InlineData(0, 50, "connectTimeoutMilliseconds")]
[InlineData(-1, 50, "connectTimeoutMilliseconds")]
[InlineData(1000, 0, "connectAttemptTimeoutMilliseconds")]
[InlineData(1000, -5, "connectAttemptTimeoutMilliseconds")]
public void Constructor_RejectsNonPositiveTimeouts(
int connectTimeoutMilliseconds,
int connectAttemptTimeoutMilliseconds,
string expectedParameter)
{
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(
() => new WorkerPipeClient(
logger: null,
connectTimeoutMilliseconds,
connectAttemptTimeoutMilliseconds));
Assert.Equal(expectedParameter, exception.ParamName);
}
/// <summary>
/// Verifies that the all-defaults constructor (the production path passes only a logger)
/// builds a client without a session factory.
/// </summary>
[Fact]
public void Constructor_WithDefaults_DoesNotThrow()
{
_ = new WorkerPipeClient();
_ = new WorkerPipeClient(logger: null);
}
/// <summary>
/// Reads frames until one matching the expected body case is found,
/// skipping interleaved heartbeats (the first heartbeat is emitted