Skip to content

Commit

Permalink
Improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Dec 29, 2024
1 parent 57c58ae commit 4796eae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/DotNext.IO/IO/PoolingBufferedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public int MaxBufferSize
/// <inheritdoc/>
public override int ReadTimeout
{
get => stream?.ReadTimeout ?? throw new NotSupportedException();
get => stream?.ReadTimeout ?? throw new InvalidOperationException();
set
{
if (stream is null)
throw new NotSupportedException();
throw new InvalidOperationException();

stream.ReadTimeout = value;
}
Expand All @@ -83,11 +83,11 @@ public override int ReadTimeout
/// <inheritdoc/>
public override int WriteTimeout
{
get => stream?.WriteTimeout ?? throw new NotSupportedException();
get => stream?.WriteTimeout ?? throw new InvalidOperationException();
set
{
if (stream is null)
throw new NotSupportedException();
throw new InvalidOperationException();

stream.WriteTimeout = value;
}
Expand Down
42 changes: 42 additions & 0 deletions src/DotNext.Tests/IO/PoolingBufferedStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ public static void CheckProperties()
Equal(stream.CanWrite, bufferedStream.CanWrite);
Equal(stream.CanSeek, bufferedStream.CanSeek);
Equal(stream.CanTimeout, bufferedStream.CanTimeout);

Throws<InvalidOperationException>(() => stream.ReadTimeout);
Throws<InvalidOperationException>(() => stream.ReadTimeout = 10);

Throws<InvalidOperationException>(() => stream.WriteTimeout);
Throws<InvalidOperationException>(() => stream.WriteTimeout = 10);
}

[Fact]
Expand Down Expand Up @@ -299,4 +305,40 @@ public static async Task CopyStream2Async()

Equal(expected, destination.GetBuffer());
}

[Fact]
public static void SetLength()
{
const int bufferSize = 4096;
using var stream = new MemoryStream(bufferSize);
using var bufferedStream = new PoolingBufferedStream(stream, leaveOpen: false)
{
MaxBufferSize = bufferSize,
Allocator = Memory.GetArrayAllocator<byte>(),
};

var expected = RandomBytes(bufferSize);
bufferedStream.Write(expected);

bufferedStream.SetLength(bufferSize);
Equal(expected, stream.GetBuffer());
}

[Fact]
public static void ResetBuffer()
{
const int bufferSize = 4096;
using var bufferedStream = new PoolingBufferedStream(new MemoryStream(bufferSize), leaveOpen: false)
{
MaxBufferSize = bufferSize,
Allocator = Memory.GetArrayAllocator<byte>(),
};

var expected = RandomBytes(bufferSize);
bufferedStream.Write(expected);
True(bufferedStream.HasBufferedDataToWrite);

bufferedStream.Reset();
False(bufferedStream.HasBufferedDataToWrite);
}
}

0 comments on commit 4796eae

Please sign in to comment.