public ReadBufferingStreamTests(ITestOutputHelper logger)
 {
     this.logger           = logger;
     this.underlyingStream = new MemoryStream(UnderlyingStreamBuffer.ToArray(), writable: false);
     Assert.True(UnderlyingStreamBuffer.Length > DefaultCapacity);
     this.bufferingStream = new ReadBufferingStream(this.underlyingStream, DefaultCapacity);
 }
    public async Task ReadAsync_Returns0AtEndOfStream()
    {
        var underlyingStream = new MemoryStream(new byte[] { 1, 2, 3 });

        this.bufferingStream = new ReadBufferingStream(underlyingStream, 5);
        var buffer = new byte[underlyingStream.Length + 1];

        Assert.Equal(3, await this.bufferingStream.ReadAsync(buffer, 0, buffer.Length));
        Assert.Equal(0, await this.bufferingStream.ReadAsync(buffer, 0, buffer.Length));
    }
 public void Dispose_DisposesStream(bool disposeStream)
 {
     this.bufferingStream = new ReadBufferingStream(this.underlyingStream, 10, disposeStream);
     this.bufferingStream.Dispose();
     if (disposeStream)
     {
         Assert.Throws <ObjectDisposedException>(() => this.underlyingStream.Seek(0, SeekOrigin.Begin));
     }
     else
     {
         this.underlyingStream.Seek(0, SeekOrigin.Begin);
     }
 }
    public async Task Read_Returns0AtEndOfStream()
    {
        var underlyingStream = new MemoryStream(new byte[] { 1, 2, 3 });

        this.bufferingStream = new ReadBufferingStream(underlyingStream, 5);
        await this.bufferingStream.FillBufferAsync();

        var buffer = new byte[underlyingStream.Length + 1];

        Assert.Equal(3, this.bufferingStream.Read(buffer, 0, buffer.Length));

        Assert.Throws <InvalidOperationException>(() => this.bufferingStream.Read(buffer, 0, buffer.Length));
        await this.bufferingStream.FillBufferAsync();

        Assert.Equal(0, this.bufferingStream.Read(buffer, 0, buffer.Length));
    }
    public async Task ReadByte_ReturnsMinus1AtEndOfStream()
    {
        var underlyingStream = new MemoryStream(new byte[1] {
            5
        });

        this.bufferingStream = new ReadBufferingStream(underlyingStream, 5);
        await this.bufferingStream.FillBufferAsync();

        Assert.Equal(5, this.bufferingStream.ReadByte());

        Assert.Throws <InvalidOperationException>(() => this.bufferingStream.ReadByte());
        await this.bufferingStream.FillBufferAsync();

        Assert.Equal(-1, this.bufferingStream.ReadByte());

        // Do it again, just to make sure it's repeatable.
        await this.bufferingStream.FillBufferAsync();

        Assert.Equal(-1, this.bufferingStream.ReadByte());
    }
 public void BufferCapacity()
 {
     Assert.Equal(DefaultCapacity, this.bufferingStream.BufferCapacity);
     this.bufferingStream = new ReadBufferingStream(this.underlyingStream, DefaultCapacity * 2);
     Assert.Equal(DefaultCapacity * 2, this.bufferingStream.BufferCapacity);
 }