From 5bf97378ee6c9c9768e1e1da15c213baf0c5c552 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Fri, 2 Aug 2024 17:42:20 -0600 Subject: [PATCH] Fix double-logging in `MonitoringStream` --- src/Nerdbank.Streams/MonitoringStream.cs | 8 ++--- .../MonitoringStreamTests.cs | 30 ++++++++++++++----- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Nerdbank.Streams/MonitoringStream.cs b/src/Nerdbank.Streams/MonitoringStream.cs index 3c74fa1c..6b9029d3 100644 --- a/src/Nerdbank.Streams/MonitoringStream.cs +++ b/src/Nerdbank.Streams/MonitoringStream.cs @@ -252,7 +252,7 @@ public override async Task ReadAsync(byte[] buffer, int offset, int count, public override int Read(Span 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; @@ -262,7 +262,7 @@ public override int Read(Span buffer) public override async ValueTask ReadAsync(Memory 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; @@ -272,7 +272,7 @@ public override async ValueTask ReadAsync(Memory buffer, Cancellation public override void Write(ReadOnlySpan buffer) { this.WillWriteSpan?.Invoke(this, buffer); - base.Write(buffer); + this.inner.Write(buffer); this.DidWriteSpan?.Invoke(this, buffer); } @@ -280,7 +280,7 @@ public override void Write(ReadOnlySpan buffer) public override async ValueTask WriteAsync(ReadOnlyMemory 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); } diff --git a/test/Nerdbank.Streams.Tests/MonitoringStreamTests.cs b/test/Nerdbank.Streams.Tests/MonitoringStreamTests.cs index 35aa1022..a144165e 100644 --- a/test/Nerdbank.Streams.Tests/MonitoringStreamTests.cs +++ b/test/Nerdbank.Streams.Tests/MonitoringStreamTests.cs @@ -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); @@ -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); @@ -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); @@ -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()); }