Skip to content

Use ReadOnlyMemoryStream for byte-input MemoryStream in tests - #4166

Merged
Mpdreamz merged 2 commits into
mainfrom
chore/dotnet-11-csharp15
Nov 11, 2026
Merged

Mpdreamz merged 2 commits into
mainfrom
chore/dotnet-11-csharp15

Conversation

@Mpdreamz

Copy link
Copy Markdown
Member

Replaces new MemoryStream(bytes) with new ReadOnlyMemoryStream(bytes) at the 24 test-file sites where the stream is created as a read-only input. ReadOnlyMemoryStream is new in .NET 11 and wraps an existing ReadOnlyMemory<byte> without the write-state bookkeeping of MemoryStream; it also states intent — these streams are inputs, not write buffers.

Affects: Automation

Prompt summary: PR 3 of the .NET 11 migration stack: adopt the genuine .NET 11 library wins that apply to this codebase. ReadOnlyMemoryStream is the clearest one — every test that constructs new MemoryStream(Encoding.UTF8.GetBytes(x)) or new MemoryStream(x) to fake a readable response stream is a direct conversion candidate.

Why

MemoryStream(byte[]) carries internal write-position state and resizing logic that is wasted when the stream is only ever read. The test sites all follow the same pattern: encode some string or literal into bytes, wrap it as a stream, and pass it to a transformer or fake S3 response. A ReadOnlyMemoryStream is both smaller at runtime and clearer at the call site about what the test intends.

What

StreamTransformerTests — 9 SSE input streams

All nine var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(sseData)) calls in StreamTransformerTests.cs are converted. These simulate AWS Bedrock or Claude SSE responses arriving as readable byte streams.

S3 ResponseStream fakes — 8 sites across 4 files

FakeS3.cs, S3IncrementalUploaderTests.cs, ChangelogUploadServiceTests.cs, and VersionIndexPublisherTests.cs all set ResponseStream on a GetObjectResponse to a MemoryStream wrapping content bytes. Each is a direct conversion — the AWS SDK declares ResponseStream as Stream.

OpenAPI input streams — 5 sites across 3 files

ReaderTests.cs, InfinityEnumSpecTests.cs, ApiNavParityTests.cs, and TagMetadataTests.cs wrap spec YAML or JSON bytes to pass to OpenApiDocument.LoadAsync. Read-only semantics are appropriate; the three MemoryStream() allocations used for writing output in InfinityEnumSpecTests are unchanged.

Remaining: write-side MemoryStream and complex cases

Production sites (FindUrlCommand.cs, SitemapBuilder.cs, ChangelogCommand.cs, OpenApiReader.cs) use new MemoryStream() as a write accumulator — WritableMemoryStream in .NET 11 requires a pre-allocated Memory<byte> buffer and is not a drop-in replacement. EuidEnrichmentTests.cs holds streams in a List<MemoryStream> for deferred disposal — the list type change adds churn for no gain. Both are left unchanged.

Verify

./build.sh unit-test

Stack: 3 of 3, on top of #4165

🤖 Generated with Claude Code

@Mpdreamz Mpdreamz added the chore label Sep 21, 2026
@Mpdreamz
Mpdreamz requested a review from a team as a code owner September 21, 2026 15:54
@Mpdreamz
Mpdreamz requested a review from theletterf September 21, 2026 15:54
@Mpdreamz
Mpdreamz added this pull request to stack #4167 September 21, 2026 15:55

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved — no actionable issues found.


What is this? | From workflow: PR Review

Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not.

Base automatically changed from chore/dotnet-11-runtime-async to main November 11, 2026 07:15
Mpdreamz and others added 2 commits November 11, 2026 08:16
Replace new MemoryStream(bytes) with new ReadOnlyMemoryStream(bytes) at
all test-file sites where the stream is created purely to be read.
ReadOnlyMemoryStream is new in .NET 11 and wraps an existing
ReadOnlyMemory<byte> without the write-state overhead of MemoryStream;
it also communicates intent: this stream is an input, not a write buffer.

11 test files, 24 sites changed:
- StreamTransformerTests (9 sites — SSE input streams)
- FakeS3, S3IncrementalUploaderTests, ChangelogUploadServiceTests (S3 ResponseStream fakes)
- ReaderTests, InfinityEnumSpecTests, ApiNavParityTests, TagMetadataTests (OpenAPI input streams)
- VersionIndexPublisherTests, ScrubberAllowlistIdentityTests, AskAiGatewayStreamingTests

Write-side MemoryStream allocations (new MemoryStream() for accumulating output)
are unchanged, as ReadOnlyMemoryStream is not a substitute for those.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Main gained five more test sites that wrap bytes in a MemoryStream only to
read them. The one site that passes the stream to a MemoryStream parameter
stays as it is.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@Mpdreamz
Mpdreamz force-pushed the chore/dotnet-11-csharp15 branch from 5a91dbb to 5951cd9 Compare November 11, 2026 07:20
@github-actions

Copy link
Copy Markdown
Contributor

@Mpdreamz
Mpdreamz merged commit 6cb1d9c into main Nov 11, 2026
43 checks passed
@Mpdreamz
Mpdreamz deleted the chore/dotnet-11-csharp15 branch November 11, 2026 07:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants