public void FlushAsyncTest() { this.CombinatorialEngineProvider.RunCombinations( new[] { new int[] { 0 }, new int[] { 1 }, new int[] { 1, 20, 1, 0 }, new int[] { 100000, 1, 100000, 23 } }, new bool[] { false, true }, new bool[] { false, true }, AsyncTestStream.InterestingBehaviors, (writeCounts, useBiggerBuffer, repeat, asyncBehaviors) => { AsyncTestStream testStream = new AsyncTestStream() { FailOnRead = true, AsyncMethodBehaviorsEnumerator = asyncBehaviors.EndLessLoop() }; AsyncBufferedStreamTestWrapper asyncBufferedStream = new AsyncBufferedStreamTestWrapper(testStream); while (true) { int totalWrittenCount = 0; foreach (int writeCount in writeCounts) { byte[] data = new byte[writeCount + (useBiggerBuffer ? 20 : 0)]; for (int i = 0; i < writeCount; i++) { data[i + (useBiggerBuffer ? 10 : 0)] = (byte)((totalWrittenCount + i) % 256); } asyncBufferedStream.Write(data, useBiggerBuffer ? 10 : 0, writeCount); totalWrittenCount += writeCount; } this.Assert.AreEqual(0, testStream.InnerStream.Length, "The buffered stream should not have written anything to the underlying stream yet."); asyncBufferedStream.FlushAsync().Wait(); byte[] writtenData = ((MemoryStream)testStream.InnerStream).GetBuffer(); this.Assert.AreEqual(totalWrittenCount, testStream.InnerStream.Length, "Wrong number of bytes was written to the stream."); for (int i = 0; i < totalWrittenCount; i++) { this.Assert.AreEqual(i % 256, writtenData[i], "Wrong data written to the stream."); } if (!repeat) { break; } testStream.InnerStream = new MemoryStream(); repeat = false; } this.Assert.IsFalse(testStream.Disposed, "The stream should not have been disposed yet."); asyncBufferedStream.Dispose(); this.Assert.IsFalse(testStream.Disposed, "The underlying stream should not be disposed."); }); }
public async void ShouldThrowIOExceptionWhenReadAsyncReturnsZeroOrLess(int returnValue) { var reader = new ChunkReader(AsyncTestStream.CreateStream(Task.FromResult(returnValue))); var ex = await Record.ExceptionAsync(() => reader.ReadNextMessagesAsync(new MemoryStream())); ex.Should().NotBeNull(); ex.Should().BeAssignableTo <IOException>().Which.Message.Should().StartWith("Unexpected end of stream, read returned"); }
public async void ShouldThrowIOExceptionWhenReadAsyncIsFaulted() { var reader = new ChunkReader(AsyncTestStream.CreateFailingStream(new IOException("some error"))); var ex = await Record.ExceptionAsync(() => reader.ReadNextMessagesAsync(new MemoryStream())); ex.Should().NotBeNull(); ex.Should().BeAssignableTo <IOException>().Which.Message.Should().Be("some error"); }
public async void ShouldThrowCancellationWhenReadAsyncIsCancelled() { var reader = new ChunkReader(AsyncTestStream.CreateCancellingStream()); var ex = await Record.ExceptionAsync(() => reader.ReadNextMessagesAsync(new MemoryStream())); ex.Should().NotBeNull(); ex.Should().BeAssignableTo <OperationCanceledException>(); }
public void BufferStreamAsyncTests() { this.CombinatorialEngineProvider.RunCombinations( new[] { new int[] {}, new int[] { 1 }, new int[] { 1, 20, 1 }, new int[] { 20 * 1024 }, new int[] { 100 * 1024 }, new int[] { 100 * 1024, 20 } }, AsyncTestStream.InterestingBehaviors, (readCounts, asyncBehaviors) => { int streamSize = 0; foreach (int i in readCounts) { streamSize += i; } streamSize *= 3; byte[] data = new byte[streamSize]; for (int i = 0; i < streamSize; i++) { data[i] = (byte)(i % 256); } ReadTestStream readTestStream = new ReadTestStream(new MemoryStream(data)); readTestStream.ReadSizesEnumerator = readCounts.Length == 0 ? null : readCounts.EndLessLoop(); AsyncTestStream asyncTestStream = new AsyncTestStream(readTestStream) { FailOnWrite = true, AsyncMethodBehaviorsEnumerator = asyncBehaviors.EndLessLoop() }; var task = BufferedReadStreamTestWrapper.BufferStream(asyncTestStream); BufferedReadStreamTestWrapper bufferedStream = task.WaitForResult(); int count = 0; int readCount = 0; do { if (count == data.Length) { // This is to ask for more data than what's in the stream to verify that the stream // correctly returns 0 bytes read at the end. readCount = bufferedStream.Read(new byte[1], 0, 1); } else { readCount = bufferedStream.Read(data, count, data.Length - count); } count += readCount; } while (readCount > 0); this.Assert.AreEqual(streamSize, count, "The stream returned wrong number of bytes."); for (int i = 0; i < streamSize; i++) { this.Assert.AreEqual(i % 256, data[i], "Wrong data written to the stream."); } this.Assert.IsTrue(asyncTestStream.Disposed, "The input stream was not disposed."); }); }
public void FlushAsyncTest() { this.CombinatorialEngineProvider.RunCombinations( new[] { new int[] { 0 }, new int[] { 1 }, new int[] { 1, 20, 1, 0 }, new int[] { 100000, 1, 100000, 23 } }, new bool[] { false, true }, new bool[] { false, true }, AsyncTestStream.InterestingBehaviors, (writeCounts, useBiggerBuffer, repeat, asyncBehaviors) => { AsyncTestStream testStream = new AsyncTestStream() { FailOnRead = true, AsyncMethodBehaviorsEnumerator = asyncBehaviors.EndLessLoop() }; AsyncBufferedStreamTestWrapper asyncBufferedStream = new AsyncBufferedStreamTestWrapper(testStream); while (true) { int totalWrittenCount = 0; foreach (int writeCount in writeCounts) { byte[] data = new byte[writeCount + (useBiggerBuffer ? 20 : 0)]; for (int i = 0; i < writeCount; i++) { data[i + (useBiggerBuffer ? 10 : 0)] = (byte)((totalWrittenCount + i) % 256); } asyncBufferedStream.Write(data, useBiggerBuffer ? 10 : 0, writeCount); totalWrittenCount += writeCount; } this.Assert.AreEqual(0, testStream.InnerStream.Length, "The buffered stream should not have written anything to the underlying stream yet."); #if SILVERLIGHT asyncBufferedStream.Flush(); #else asyncBufferedStream.FlushAsync().Wait(); #endif byte[] writtenData = ((MemoryStream)testStream.InnerStream).GetBuffer(); this.Assert.AreEqual(totalWrittenCount, testStream.InnerStream.Length, "Wrong number of bytes was written to the stream."); for (int i = 0; i < totalWrittenCount; i++) { this.Assert.AreEqual(i % 256, writtenData[i], "Wrong data written to the stream."); } if (!repeat) { break; } testStream.InnerStream = new MemoryStream(); repeat = false; } this.Assert.IsFalse(testStream.Disposed, "The stream should not have been disposed yet."); asyncBufferedStream.Dispose(); this.Assert.IsFalse(testStream.Disposed, "The underlying stream should not be disposed."); }); }