-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update message writer and reader to ignore Message info from DI #3058
Merged
marabooy
merged 9 commits into
main
from
ft/remove-message-info-from-being-resolved-using-di
Sep 23, 2024
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
78fd2d3
Update message writer and reader to ignore Message info from DI as it…
marabooy 20cf1e1
Update test from pr review
marabooy ebdb79c
Add fix for older dotnet versions which are failing in the tests.
marabooy af4bcb0
Update code with pr review suggestions.
marabooy 841a978
Update code from pr review.
marabooy fd4870d
Update code from pr review.
marabooy 7797ab1
Update code from pr review.
marabooy d64e6a3
Update step order
marabooy 072ad47
Move Esrp code dotnet target close to usage
marabooy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
112 changes: 112 additions & 0 deletions
112
test/FunctionalTests/Microsoft.OData.Core.Tests/MessageWriterConcurrencyTests.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
//--------------------------------------------------------------------- | ||
// <copyright file="MessageWriterConcurrencyTests.cs" company="Microsoft"> | ||
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. | ||
// </copyright> | ||
//--------------------------------------------------------------------- | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System; | ||
using Xunit; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Linq; | ||
|
||
namespace Microsoft.OData.Core.Tests | ||
{ | ||
public class MessageWriterConcurrencyTests | ||
{ | ||
/// <summary> | ||
/// Verifies that concurrent message writer does not interleave execution and isolates the underlying streams. | ||
/// </summary> | ||
/// <returns>A task for the asyncronous test</returns> | ||
marabooy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[Fact] | ||
public async Task VerifyConcurrentResultsAreConsistentAsync() | ||
marabooy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
ServiceCollection services = new(); | ||
services.AddDefaultODataServices(); | ||
ServiceProvider serviceProvider = services.BuildServiceProvider(); | ||
|
||
await Task.CompletedTask; | ||
marabooy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var content1 = string.Concat(Enumerable.Repeat('A', 1000_000)); | ||
var content2 = string.Concat(Enumerable.Repeat('B', 1000_000)); | ||
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to create such long strings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessarily but it would make the interleaving more apparent if it happens |
||
for (int i = 0; i < 1000; i++) | ||
{ | ||
var values = await Task.WhenAll([WritePayload(content1, serviceProvider), WritePayload(content2, serviceProvider)]); | ||
Assert.Equal(content1.Length, values[0].Length); | ||
Assert.Equal(content2.Length, values[1].Length); | ||
|
||
Assert.Equal(content1, values[0]); | ||
Assert.Equal(content2, values[1]); | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// A helper function that writes to a strem using the message writer and returns the content that is present in the stream. | ||
marabooy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
/// <param name="content">String content to write.</param> | ||
/// <param name="serviceProvider">A service provider with the default configurations.</param> | ||
/// <returns>A task that resolves to the string present in the output stream.</returns> | ||
private async Task<string> WritePayload(string content, IServiceProvider serviceProvider) | ||
marabooy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
using Stream outputStream = new MemoryStream(); | ||
|
||
var message = new ODataMessage(outputStream, serviceProvider); | ||
await using ODataMessageWriter writer = new ODataMessageWriter(message); | ||
await Task.Yield(); | ||
|
||
await writer.WriteValueAsync(content); | ||
|
||
outputStream.Position = 0; | ||
using var reader = new StreamReader(outputStream); | ||
await Task.Yield(); | ||
string writen = await reader.ReadToEndAsync(); | ||
await writer.DisposeAsync(); | ||
marabooy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return writen; | ||
} | ||
marabooy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class ODataMessage : IODataResponseMessage, IODataResponseMessageAsync, IServiceCollectionProvider | ||
marabooy marked this conversation as resolved.
Show resolved
Hide resolved
marabooy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private Dictionary<string, string> _headers = new(); | ||
private Stream _outputStream; | ||
public ODataMessage(Stream outputStream, IServiceProvider serviceProvider) | ||
{ | ||
this.ServiceProvider = serviceProvider; | ||
_outputStream = outputStream; | ||
} | ||
public IEnumerable<KeyValuePair<string, string>> Headers => _headers; | ||
|
||
public int StatusCode { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
|
||
public IServiceProvider ServiceProvider { get; private set; } | ||
|
||
public string GetHeader(string headerName) | ||
{ | ||
if (_headers.TryGetValue(headerName, out var value)) | ||
{ | ||
return value; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public Stream GetStream() | ||
{ | ||
return _outputStream; | ||
} | ||
|
||
public Task<Stream> GetStreamAsync() | ||
{ | ||
return Task.FromResult(_outputStream); | ||
} | ||
|
||
public void SetHeader(string headerName, string headerValue) | ||
{ | ||
_headers[headerName] = headerValue; | ||
} | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so, why do we still keep "https://github.com/OData/odata.net/blob/main/src/Microsoft.OData.Core/ODataServiceCollectionExtensions.cs#L52" ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking, that could be a breaking change. The likelihood of breaking existing code is low, likely 0, but here's sample code that will break today if we removed
ODataMessageInfo
from the DI:In the current version, this would return a messageInfo instance, if we remove it from DI and ship a new version, then this code would suddenly throw an exception when the user updates.
If we want to be strict about non-breaking minor and patch releases, we can remove it from the next major version and consider making the class private.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be a breaking change but it seems it could have very few users since a search shows that this was only being used in that location. Also the DI version is scoped which should not reproduce this bug/issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/OData/odata.net/blob/main/buildandtest.yml#L24 this build step is the one that fails to build