Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gathogojr committed Feb 23, 2022
1 parent a983a2b commit 59c914e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 23 deletions.
32 changes: 17 additions & 15 deletions src/Microsoft.OData.Core/ODataNotificationReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ internal sealed class ODataNotificationReader : TextReader, IAsyncDisposable
internal sealed class ODataNotificationReader : TextReader
#endif
{
private TextReader textReader;
private IODataStreamListener listener;
private bool disposed = false;
private readonly TextReader textReader;
private readonly IODataStreamListener listener;
private readonly bool synchronous;
private bool disposed = false;

/// <summary>
/// Initializes a new instance of the <see cref="ODataNotificationReader"/> class.
/// </summary>
/// <param name="textReader">The wrapped text reader.</param>
/// <param name="listener">Listener to notify when the text reader is being disposed.</param>
/// <param name="synchronous">true if execution context is synchronous; otherwise false.</param>
/// <remarks>
/// When an instance of this class is disposed, it in turn disposes the wrapped text reader.
/// </remarks>
internal ODataNotificationReader(TextReader textReader, IODataStreamListener listener, bool synchronous = true)
{
Debug.Assert(textReader != null, "Creating a notification reader for a null textReader.");
Debug.Assert(listener != null, "Creating a notification reader with a null textReader.");
Debug.Assert(listener != null, "Creating a notification reader with a null listener.");

this.textReader = textReader;
this.listener = listener;
Expand Down Expand Up @@ -126,33 +135,26 @@ protected override void Dispose(bool disposing)
// Tell the listener that the stream is being disposed.
if (synchronous)
{
this.listener?.StreamDisposed();
this.listener.StreamDisposed();
}
else
{
this.listener?.StreamDisposedAsync().Wait();
this.listener.StreamDisposedAsync().Wait();
}

this.listener = null;
// NOTE: Do not dispose the text reader since this instance does not own it.
this.textReader = null;
}

this.disposed = true;
this.textReader?.Dispose();
base.Dispose(disposing);
}

#if NETSTANDARD2_0
public async ValueTask DisposeAsync()
{
if (!this.disposed && this.listener != null)
if (!this.disposed)
{
await this.listener.StreamDisposedAsync()
.ConfigureAwait(false);

this.listener = null;
// NOTE: Do not dispose the text reader since this instance does not own it.
this.textReader = null;
}

// Dispose unmanaged resources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.OData.Tests
{
public class ODataNotificationReaderTests
public class ODataNotificationReaderTests : IDisposable
{
private MemoryStream stream;
private TextReader reader;
private TextWriter streamListenerWriter;
private IODataStreamListener streamListener;

public ODataNotificationReaderTests()
{
this.stream = new MemoryStream();
this.reader = new StreamReader(this.stream);
this.streamListener = new MockODataStreamListener(new StreamWriter(this.stream));
this.reader = new StreamReader(this.stream, encoding: Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true);
this.streamListenerWriter = new StreamWriter(this.stream, encoding: Encoding.UTF8, bufferSize: 1024, leaveOpen: true);
this.streamListener = new MockODataStreamListener(this.streamListenerWriter);
}

[Theory]
Expand Down Expand Up @@ -114,16 +117,49 @@ public async Task NotificationReaderDisposeShouldInvokeStreamDisposedAsync()
}
#endif

public void Dispose() // Fired after every test is ran
{
this.streamListenerWriter.Dispose();
this.reader.Dispose();
this.stream.Dispose();
}

private string ReadStreamContents()
{
this.stream.Position = 0;
return new StreamReader(this.stream).ReadToEnd();
string streamContents;

using (var reader = new StreamReader(
this.stream,
encoding: Encoding.UTF8,
detectEncodingFromByteOrderMarks: true,
bufferSize: 1024,
leaveOpen: true))
{

this.stream.Position = 0;
streamContents = reader.ReadToEnd();
}

return streamContents;
}

private Task<string> ReadStreamContentsAsync()
private async Task<string> ReadStreamContentsAsync()
{
this.stream.Position = 0;
return new StreamReader(this.stream).ReadToEndAsync();
string streamContents;

using (var reader = new StreamReader(
this.stream,
encoding: Encoding.UTF8,
detectEncodingFromByteOrderMarks: true,
bufferSize: 1024,
leaveOpen: true))
{

this.stream.Position = 0;
streamContents = await reader.ReadToEndAsync();
}

return streamContents;
}

private class MockODataStreamListener : IODataStreamListener
Expand Down

0 comments on commit 59c914e

Please sign in to comment.