public void ExceptionTest() { AsyncPump.Run( async() => { AssertThrow <ArgumentNullException>( () => new ReadBuffer((ReadCallback)null, 1).Ignore(), () => new ReadBuffer((ReadAsyncCallback)null, 1).Ignore(), () => new WriteBuffer((WriteCallback)null, 1).Ignore(), () => new WriteBuffer((WriteAsyncCallback)null, 1).Ignore(), () => new WriteBuffer((b, o, c) => { }, 1).WriteAsUtf8(null, 0)); AssertThrow <ArgumentOutOfRangeException>(() => new ReadBuffer((b, o, c) => 0, 0).Ignore()); using (var stream = new MemoryStream()) { var readBuffer = new ReadBuffer(stream.Read, 1); AssertThrow <EndOfStreamException>( () => readBuffer.Fill(new byte[1], 0, 1), () => readBuffer.Fill(new byte[2], 0, 2)); await AssertThrowAsync <InvalidOperationException>( () => readBuffer.ReadAsync(CancellationToken.None), () => readBuffer.ReadAsync(new byte[1], 0, 1, CancellationToken.None), () => readBuffer.FillAsync(1, CancellationToken.None), () => readBuffer.FillAsync(new byte[1], 0, 1, CancellationToken.None)); var asyncReadBuffer = new ReadBuffer((ReadAsyncCallback)stream.ReadAsync, 1); await AssertThrowAsync <EndOfStreamException>( () => asyncReadBuffer.FillAsync(new byte[1], 0, 1, CancellationToken.None), () => asyncReadBuffer.FillAsync(new byte[2], 0, 2, CancellationToken.None)); AssertThrow <InvalidOperationException>( () => asyncReadBuffer.Read(), () => asyncReadBuffer.Read(new byte[1], 0, 1), () => asyncReadBuffer.Fill(1), () => asyncReadBuffer.Fill(new byte[1], 0, 1), () => asyncReadBuffer.ReadUtf8(1)); var writeBuffer = new WriteBuffer(stream.Write, 1); await AssertThrowAsync <InvalidOperationException>( () => writeBuffer.FlushAsync(CancellationToken.None), () => writeBuffer.ReserveAsync(2, CancellationToken.None), () => writeBuffer.WriteAsync(new byte[3], 0, 3, CancellationToken.None)); var asyncWriteBuffer = new WriteBuffer(stream.WriteAsync, 1); asyncWriteBuffer[asyncWriteBuffer.Count++] = 42; AssertThrow <InvalidOperationException>(() => asyncWriteBuffer.Flush()); asyncWriteBuffer[asyncWriteBuffer.Count++] = 42; AssertThrow <InvalidOperationException>( () => asyncWriteBuffer.Reserve(2), () => asyncWriteBuffer.Write(new byte[3], 0, 3)); asyncWriteBuffer[asyncWriteBuffer.Count++] = 42; var str = "Hello"; AssertThrow <InvalidOperationException>( () => asyncWriteBuffer.WriteAsUtf8(str, Encoding.UTF8.GetByteCount(str))); } }); }
public void WriteTest() { var originalBytes = new byte[2]; this.Random.NextBytes(originalBytes); // This covers the case where the written bytes are copied into the buffer in two chunks using (var stream = new MemoryStream()) { var writeBuffer = new WriteBuffer(stream.Write, 1); writeBuffer.Write(originalBytes, 0, originalBytes.Length); writeBuffer.Flush(); CollectionAssert.AreEqual(originalBytes, stream.ToArray()); } }
public void ExceptionTest() { AsyncPump.Run( async () => { AssertThrow<ArgumentNullException>( () => new ReadBuffer((ReadCallback)null, 1).Ignore(), () => new ReadBuffer((ReadAsyncCallback)null, 1).Ignore(), () => new WriteBuffer((WriteCallback)null, 1).Ignore(), () => new WriteBuffer((WriteAsyncCallback)null, 1).Ignore(), () => new WriteBuffer((b, o, c) => { }, 1).WriteAsUtf8(null, 0)); AssertThrow<ArgumentOutOfRangeException>(() => new ReadBuffer((b, o, c) => 0, 0).Ignore()); using (var stream = new MemoryStream()) { var readBuffer = new ReadBuffer(stream.Read, 1); AssertThrow<EndOfStreamException>( () => readBuffer.Fill(new byte[1], 0, 1), () => readBuffer.Fill(new byte[2], 0, 2)); await AssertThrowAsync<InvalidOperationException>( () => readBuffer.ReadAsync(CancellationToken.None), () => readBuffer.ReadAsync(new byte[1], 0, 1, CancellationToken.None), () => readBuffer.FillAsync(1, CancellationToken.None), () => readBuffer.FillAsync(new byte[1], 0, 1, CancellationToken.None)); var asyncReadBuffer = new ReadBuffer((ReadAsyncCallback)stream.ReadAsync, 1); await AssertThrowAsync<EndOfStreamException>( () => asyncReadBuffer.FillAsync(new byte[1], 0, 1, CancellationToken.None), () => asyncReadBuffer.FillAsync(new byte[2], 0, 2, CancellationToken.None)); AssertThrow<InvalidOperationException>( () => asyncReadBuffer.Read(), () => asyncReadBuffer.Read(new byte[1], 0, 1), () => asyncReadBuffer.Fill(1), () => asyncReadBuffer.Fill(new byte[1], 0, 1), () => asyncReadBuffer.ReadUtf8(1)); var writeBuffer = new WriteBuffer(stream.Write, 1); await AssertThrowAsync<InvalidOperationException>( () => writeBuffer.FlushAsync(CancellationToken.None), () => writeBuffer.ReserveAsync(2, CancellationToken.None), () => writeBuffer.WriteAsync(new byte[3], 0, 3, CancellationToken.None)); var asyncWriteBuffer = new WriteBuffer(stream.WriteAsync, 1); asyncWriteBuffer[asyncWriteBuffer.Count++] = 42; AssertThrow<InvalidOperationException>(() => asyncWriteBuffer.Flush()); asyncWriteBuffer[asyncWriteBuffer.Count++] = 42; AssertThrow<InvalidOperationException>( () => asyncWriteBuffer.Reserve(2), () => asyncWriteBuffer.Write(new byte[3], 0, 3)); asyncWriteBuffer[asyncWriteBuffer.Count++] = 42; var str = "Hello"; AssertThrow<InvalidOperationException>( () => asyncWriteBuffer.WriteAsUtf8(str, Encoding.UTF8.GetByteCount(str))); } }); }