|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using HdrHistogram.Utilities; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace HdrHistogram.UnitTests.Utilities |
| 7 | +{ |
| 8 | + public class ByteBufferTests |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// Reproduces Issue #99. |
| 12 | + /// <see cref="ByteBuffer.ReadFrom"/> must loop until all requested bytes |
| 13 | + /// have been read, because <see cref="Stream.Read"/> is permitted to |
| 14 | + /// return fewer bytes than requested — even when more data is available. |
| 15 | + /// <see cref="System.IO.Compression.DeflateStream"/> does exactly this |
| 16 | + /// at DEFLATE block boundaries. |
| 17 | + /// </summary> |
| 18 | + [Theory] |
| 19 | + [InlineData(1024, 100)] |
| 20 | + [InlineData(4096, 511)] |
| 21 | + [InlineData(8192, 1000)] |
| 22 | + public void ReadFrom_returns_all_bytes_when_stream_returns_partial_reads( |
| 23 | + int totalBytes, int maxBytesPerRead) |
| 24 | + { |
| 25 | + var data = new byte[totalBytes]; |
| 26 | + new Random(42).NextBytes(data); |
| 27 | + var stream = new PartialReadStream(new MemoryStream(data), maxBytesPerRead); |
| 28 | + var buffer = ByteBuffer.Allocate(totalBytes); |
| 29 | + |
| 30 | + int bytesRead = buffer.ReadFrom(stream, totalBytes); |
| 31 | + |
| 32 | + Assert.Equal(totalBytes, bytesRead); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// A stream wrapper that returns at most <c>maxBytesPerRead</c> bytes |
| 37 | + /// per <see cref="Read"/> call, simulating the behaviour of |
| 38 | + /// <see cref="System.IO.Compression.DeflateStream"/> at compression |
| 39 | + /// block boundaries. |
| 40 | + /// </summary> |
| 41 | + private sealed class PartialReadStream : Stream |
| 42 | + { |
| 43 | + private readonly Stream _inner; |
| 44 | + private readonly int _maxBytesPerRead; |
| 45 | + |
| 46 | + public PartialReadStream(Stream inner, int maxBytesPerRead) |
| 47 | + { |
| 48 | + _inner = inner; |
| 49 | + _maxBytesPerRead = maxBytesPerRead; |
| 50 | + } |
| 51 | + |
| 52 | + public override int Read(byte[] buffer, int offset, int count) |
| 53 | + { |
| 54 | + return _inner.Read(buffer, offset, Math.Min(count, _maxBytesPerRead)); |
| 55 | + } |
| 56 | + |
| 57 | + public override bool CanRead => true; |
| 58 | + public override bool CanSeek => false; |
| 59 | + public override bool CanWrite => false; |
| 60 | + public override long Length => throw new NotSupportedException(); |
| 61 | + public override long Position |
| 62 | + { |
| 63 | + get => throw new NotSupportedException(); |
| 64 | + set => throw new NotSupportedException(); |
| 65 | + } |
| 66 | + public override void Flush() { } |
| 67 | + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| 68 | + public override void SetLength(long value) => throw new NotSupportedException(); |
| 69 | + public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments