Skip to content

Commit

Permalink
Merge pull request #772 from dotnet/mergev2.11ToMain
Browse files Browse the repository at this point in the history
Merge v2.11 into main
  • Loading branch information
AArnott authored Aug 23, 2024
2 parents 34fc5e8 + abd03ce commit 886a3b3
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ updates:
directory: /
schedule:
interval: weekly
ignore:
- dependency-name: Microsoft.Bcl.AsyncInterfaces # We want to match the minimum target .NET runtime
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
<BenchmarkDotNetVersion>0.13.12</BenchmarkDotNetVersion>
<AspNetCoreVersion>2.2.0</AspNetCoreVersion>
<VSThreadingVersion>17.8.14</VSThreadingVersion>
<VSThreadingVersion>17.10.48</VSThreadingVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="BenchmarkDotNet.Diagnostics.Windows" Version="$(BenchmarkDotNetVersion)" />
<PackageVersion Include="BenchmarkDotNet" Version="$(BenchmarkDotNetVersion)" />
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="$(AspNetCoreVersion)" />
<PackageVersion Include="Microsoft.AspNetCore" Version="$(AspNetCoreVersion)" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="$(VSThreadingVersion)" />
Expand All @@ -23,7 +23,7 @@
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />
<PackageVersion Include="PInvoke.Kernel32" Version="0.7.124" />
<PackageVersion Include="StreamJsonRpc" Version="2.17.11" />
<PackageVersion Include="System.IO.Pipelines" Version="7.0.0" />
<PackageVersion Include="System.IO.Pipelines" Version="8.0.0" />
<PackageVersion Include="System.IO.Pipes" Version="4.3.0" />
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageVersion Include="xunit.abstractions" Version="2.0.3" />
Expand Down
8 changes: 4 additions & 4 deletions src/Nerdbank.Streams/MonitoringStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
public override int Read(Span<byte> buffer)
{
this.WillReadSpan?.Invoke(this, buffer);
int bytesRead = base.Read(buffer);
int bytesRead = this.inner.Read(buffer);
this.DidReadSpan?.Invoke(this, buffer);
this.RaiseEndOfStreamIfNecessary(bytesRead);
return bytesRead;
Expand All @@ -262,7 +262,7 @@ public override int Read(Span<byte> buffer)
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
this.WillReadMemory?.Invoke(this, buffer);
int bytesRead = await base.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
int bytesRead = await this.inner.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
this.DidReadMemory?.Invoke(this, buffer);
this.RaiseEndOfStreamIfNecessary(bytesRead);
return bytesRead;
Expand All @@ -272,15 +272,15 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
public override void Write(ReadOnlySpan<byte> buffer)
{
this.WillWriteSpan?.Invoke(this, buffer);
base.Write(buffer);
this.inner.Write(buffer);
this.DidWriteSpan?.Invoke(this, buffer);
}

/// <inheritdoc/>
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
this.WillWriteMemory?.Invoke(this, buffer);
await base.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
await this.inner.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
this.DidWriteMemory?.Invoke(this, buffer);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Nerdbank.Streams/MultiplexingStream.Channel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Andrew Arnott. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#pragma warning disable VSTHRD003 // Avoid awaiting foreign Tasks -- We always use .ConfigureAwait(false).

namespace Nerdbank.Streams
{
using System;
Expand Down
9 changes: 8 additions & 1 deletion src/Nerdbank.Streams/MultiplexingStream.Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,14 @@ public bool FaultOpenChannelsOnStreamDisposal
/// <returns>This instance if already frozen, otherwise a frozen copy.</returns>
public Options GetFrozenCopy() => this.IsFrozen ? this : new Options(this, frozen: true);

private void ThrowIfFrozen() => Verify.Operation(!this.IsFrozen, Strings.Frozen);
private void ThrowIfFrozen()
{
// Avoid loading string unless needed
if (this.IsFrozen)
{
Verify.FailOperation(Strings.Frozen);
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/Nerdbank.Streams/MultiplexingStream.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Andrew Arnott. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#pragma warning disable VSTHRD003 // Avoid awaiting foreign Tasks -- We always use .ConfigureAwait(false).

namespace Nerdbank.Streams
{
using System;
Expand Down
2 changes: 1 addition & 1 deletion src/Nerdbank.Streams/Nerdbank.Streams.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" PrivateAssets="all" />
<PackageReference Include="Microsoft.VisualStudio.Validation" />
<PackageReference Include="System.IO.Pipelines" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Condition="'$(TargetFramework)' != 'net6.0'" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'" />
</ItemGroup>
<ItemGroup>
<Compile Update="Strings.Designer.cs">
Expand Down
2 changes: 2 additions & 0 deletions src/Nerdbank.Streams/ReadOnlySequenceStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
return TaskOfZero;
}

#pragma warning disable VSTHRD103 // Call async methods when in an async method - This task is guaranteed to already be complete.
if (this.lastReadTask?.Result == bytesRead)
{
return this.lastReadTask;
Expand All @@ -94,6 +95,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
{
return this.lastReadTask = Task.FromResult(bytesRead);
}
#pragma warning restore VSTHRD103 // Call async methods when in an async method
}

/// <inheritdoc/>
Expand Down
2 changes: 2 additions & 0 deletions src/Nerdbank.Streams/SpanPolyfillExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#pragma warning disable AvoidAsyncSuffix // Avoid Async suffix
#pragma warning disable VSTHRD003 // Avoid awaiting foreign Tasks -- We always use .ConfigureAwait(false).

#if !SPAN_BUILTIN

namespace Nerdbank.Streams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
</ItemGroup>

<ItemGroup>
<!-- Temporarily upgrade Microsoft.Bcl.AsyncInterfaces while we reference StreamJsonRpc that wants 7.0.0 -->
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" VersionOverride="7.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Threading" />
</ItemGroup>

Expand Down
30 changes: 22 additions & 8 deletions test/Nerdbank.Streams.Tests/MonitoringStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ public void Read_Span_RaisesEvents()
Assert.Same(this.monitoringStream, s);
Assert.Equal(6, e.Length);
};
this.monitoringStream.DidRead += (s, e) => Assert.Fail("Unexpected event.");
this.monitoringStream.DidReadMemory += (s, e) => Assert.Fail("Unexpected event.");
this.monitoringStream.DidReadByte += (s, e) => Assert.Fail("Unexpected event.");
int bytesRead = this.monitoringStream.Read(this.buffer.AsSpan(2, 6));
Assert.Equal(5, bytesRead);
Assert.Equal(bytesRead, this.underlyingStream.Position);
Expand Down Expand Up @@ -487,6 +490,9 @@ public async Task ReadAsync_Memory_RaisesEvents()
Assert.Same(this.monitoringStream, s);
Assert.Equal(6, e.Length);
};
this.monitoringStream.DidRead += (s, e) => Assert.Fail("Unexpected event.");
this.monitoringStream.DidReadSpan += (s, e) => Assert.Fail("Unexpected event.");
this.monitoringStream.DidReadByte += (s, e) => Assert.Fail("Unexpected event.");
int bytesRead = await this.monitoringStream.ReadAsync(this.buffer.AsMemory(2, 6));
Assert.Equal(5, bytesRead);
Assert.Equal(bytesRead, this.underlyingStream.Position);
Expand Down Expand Up @@ -529,6 +535,9 @@ public void Write_Span_RaisesEvents()
Assert.Same(this.monitoringStream, s);
Assert.Equal(3, e.Length);
};
this.monitoringStream.DidWrite += (s, e) => Assert.Fail("Unexpected event.");
this.monitoringStream.DidWriteMemory += (s, e) => Assert.Fail("Unexpected event.");
this.monitoringStream.DidWriteByte += (s, e) => Assert.Fail("Unexpected event.");
this.monitoringStream.Write(this.buffer.AsSpan(2, 3));
Assert.True(willWriteInvoked);
Assert.True(didWriteInvoked);
Expand All @@ -538,25 +547,30 @@ public void Write_Span_RaisesEvents()
[Fact]
public async Task WriteAsync_Memory_RaisesEvents()
{
bool willWriteInvoked = false;
bool didWriteInvoked = false;
int willWriteInvoked = 0;
int didWriteInvoked = 0;
this.monitoringStream.WillWriteMemory += (s, e) =>
{
willWriteInvoked = true;
Assert.False(didWriteInvoked);
Assert.Equal(0, willWriteInvoked);
willWriteInvoked++;
Assert.Equal(0, didWriteInvoked);
Assert.Same(this.monitoringStream, s);
Assert.Equal(3, e.Length);
};
this.monitoringStream.DidWriteMemory += (s, e) =>
{
didWriteInvoked = true;
Assert.True(willWriteInvoked);
Assert.Equal(0, didWriteInvoked);
Assert.Equal(1, willWriteInvoked);
didWriteInvoked++;
Assert.Same(this.monitoringStream, s);
Assert.Equal(3, e.Length);
};
this.monitoringStream.DidWrite += (s, e) => Assert.Fail("Unexpected event.");
this.monitoringStream.DidWriteSpan += (s, e) => Assert.Fail("Unexpected event.");
this.monitoringStream.DidWriteByte += (s, e) => Assert.Fail("Unexpected event.");
await this.monitoringStream.WriteAsync(this.buffer.AsMemory(2, 3));
Assert.True(willWriteInvoked);
Assert.True(didWriteInvoked);
Assert.Equal(1, willWriteInvoked);
Assert.Equal(1, didWriteInvoked);
Assert.Equal(new byte[] { 8, 9, 10, 4, 5 }, this.underlyingStream.ToArray());
}

Expand Down
2 changes: 2 additions & 0 deletions test/Nerdbank.Streams.Tests/Nerdbank.Streams.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" />
<!-- Temporarily upgrade Microsoft.Bcl.AsyncInterfaces while we reference StreamJsonRpc that wants 7.0.0 -->
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" VersionOverride="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Microsoft.VisualStudio.Threading" />
<PackageReference Include="NSubstitute" />
Expand Down

0 comments on commit 886a3b3

Please sign in to comment.