Skip to content

Commit

Permalink
Merge pull request #769 from dotnet/fixDblLogging
Browse files Browse the repository at this point in the history
Fix double-logging in `MonitoringStream`
  • Loading branch information
AArnott authored Aug 23, 2024
2 parents 86a8985 + 5bf9737 commit 1e38407
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
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
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

0 comments on commit 1e38407

Please sign in to comment.