Repository navigation
Use ReadOnlyMemoryStream for byte-input MemoryStream in tests - #4166
Merged
Merged
Conversation
akira28
approved these changes
Sep 21, 2026
Contributor
There was a problem hiding this comment.
Approved — no actionable issues found.
What is this? | From workflow: PR Review
Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not.
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
force-pushed
the
chore/dotnet-11-csharp15
branch
from
November 11, 2026 07:20
5a91dbb to
5951cd9
Compare
Contributor
Docs preview (local build)Handbook preview: https://docs-v3-preview.elastic.dev/elastic/docs-builder/pull/4166/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces
new MemoryStream(bytes)withnew ReadOnlyMemoryStream(bytes)at the 24 test-file sites where the stream is created as a read-only input.ReadOnlyMemoryStreamis new in .NET 11 and wraps an existingReadOnlyMemory<byte>without the write-state bookkeeping ofMemoryStream; 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.
ReadOnlyMemoryStreamis the clearest one — every test that constructsnew MemoryStream(Encoding.UTF8.GetBytes(x))ornew 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. AReadOnlyMemoryStreamis 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 inStreamTransformerTests.csare 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, andVersionIndexPublisherTests.csall setResponseStreamon aGetObjectResponseto aMemoryStreamwrapping content bytes. Each is a direct conversion — the AWS SDK declaresResponseStreamasStream.OpenAPI input streams — 5 sites across 3 files
ReaderTests.cs,InfinityEnumSpecTests.cs,ApiNavParityTests.cs, andTagMetadataTests.cswrap spec YAML or JSON bytes to pass toOpenApiDocument.LoadAsync. Read-only semantics are appropriate; the threeMemoryStream()allocations used for writing output inInfinityEnumSpecTestsare unchanged.Remaining: write-side MemoryStream and complex cases
Production sites (
FindUrlCommand.cs,SitemapBuilder.cs,ChangelogCommand.cs,OpenApiReader.cs) usenew MemoryStream()as a write accumulator —WritableMemoryStreamin .NET 11 requires a pre-allocatedMemory<byte>buffer and is not a drop-in replacement.EuidEnrichmentTests.csholds streams in aList<MemoryStream>for deferred disposal — the list type change adds churn for no gain. Both are left unchanged.Verify
Stack: 3 of 3, on top of #4165
🤖 Generated with Claude Code