public void ContentLength_SetStreamSupportingSeeking_StreamLengthMatchesHeaderValue() { var source = new MockStream(new byte[10], true, true); // Supports seeking. var content = new StreamContent(source); Assert.Equal(source.Length, content.Headers.ContentLength);; }
public void ContentLength_SetStreamSupportingSeeking_StreamLengthMatchesHeaderValue() { var source = new MockStream(new byte[10], true, true); // Supports seeking. var content = new StreamContent(source); Assert.Equal(source.Length, content.Headers.ContentLength); }
public void ContentLength_SetStreamNotSupportingSeeking_NullReturned() { var source = new MockStream(new byte[10], false, true); // Doesn't support seeking. var content = new StreamContent(source); Assert.Null(content.Headers.ContentLength); }
public void SetUp() { var stream = new MockStream(); ReadBuffer = new NpgsqlReadBuffer(null, stream, null, NpgsqlReadBuffer.DefaultSize, PGUtil.UTF8Encoding, PGUtil.RelaxedUTF8Encoding); Writer = stream.Writer; }
public void CopyToAsync_NullDestination_ThrowsArgumentnullException() { var source = new MockStream(new byte[10]); var content = new StreamContent(source); Assert.Throws <ArgumentNullException>(() => { Task t = content.CopyToAsync(null); }); }
/** * @param expectedFirstInt expected value of the first int read from the decrypted stream */ public StreamTester(MockStream ms, String keyDigestHex, int expectedFirstInt) { _ms = ms; byte[] keyDigest = HexRead.ReadFromString(keyDigestHex); _bds = new Biff8DecryptingStream(_ms, 0, new Biff8EncryptionKey(keyDigest)); Assert.AreEqual(expectedFirstInt, _bds.ReadInt()); _errorsOccurred = false; }
public void ContentLength_SetStreamSupportingSeekingPartiallyConsumed_StreamLengthMatchesHeaderValueMinusConsumed() { int consumed = 4; var source = new MockStream(new byte[10], true, true); // Supports seeking. source.Read(new byte[consumed], 0, consumed); var content = new StreamContent(source); Assert.Equal(source.Length - consumed, content.Headers.ContentLength); }
public void Dispose_UseMockStreamSourceAndDisposeContent_MockStreamGotDisposed() { var source = new MockStream(new byte[10]); var content = new StreamContent(source); content.Dispose(); Assert.Equal(1, source.DisposeCount); }
public void BlobDownloadInfo_Dispose() { MockStream stream = new MockStream(); BlobDownloadInfo blobDownloadInfo = BlobsModelFactory.BlobDownloadInfo(content: stream); Assert.IsFalse(stream.IsDisposed); blobDownloadInfo.Dispose(); Assert.IsTrue(stream.IsDisposed); }
public void CreateStreamTest() { const string symbol = "Symbol"; const string column = "Column"; var mockStream1 = new MockStream(3); var mockDbUpdater = Substitute.For <ITimeSeriesDbUpdater>(); mockDbUpdater.CreateColumnWriter(Arg.Any <string>(), Arg.Any <ColumnInfo>(), Arg.Any <DateTime>()) .Returns(mockStream1); var allocator = new DummyPinnedAllocator(); var recorder = new TimeSeriesRecorder( symbol, mockDbUpdater, allocator); var now = "08:00:00"; recorder.AddRow(now) .Record(column, 1) .Record(column, 2); mockDbUpdater.Received(1) .CreateColumnWriter( Arg.Is <string>(i => i == symbol), Arg.Is <ColumnInfo>(i => i.Name == column && i.Type == FieldType.Int32), Arg.Is <DateTime>(i => i == "08:00:00".ToDateTime())); mockDbUpdater.ClearReceivedCalls(); mockStream1.CheckCallsThenClear(); now = "08:00:01"; recorder.AddRow(now) .Record(column, 3) .Record(column, 4) .Record(column, 5); mockDbUpdater.ClearReceivedCalls(); mockStream1.CheckCallsThenClear(); mockStream1.CheckData(E("08:00:00", 1), E("08:00:00", 2), E("08:00:01", 3), E("08:00:01", 4), E("08:00:01", 5)); now = "08:00:02"; recorder.AddRow(now) .Record(column, 6); recorder.Commit(); mockStream1.CheckCallsThenClear(1, 0, 1); var columns = new[] { C(column, FieldType.Int32, "08:00:00", "08:00:02") }; mockDbUpdater.Received(1).Commit( Arg.Is <string>(i => i == symbol), Arg.Is <ColumnCommit[]>(i => IsEqual(i, columns))); }
public void StorageFileDownloadInfo_Dispose() { MockStream stream = new MockStream(); ShareFileDownloadInfo storageFileDownloadInfo = FilesModelFactory.StorageFileDownloadInfo(content: stream); Assert.IsFalse(stream.IsDisposed); storageFileDownloadInfo.Dispose(); Assert.IsTrue(stream.IsDisposed); }
public void CopyAsync() { var ms = new MockStream(true, false, true); int counter = 4; ms.OnRead += delegate { return(--counter); }; var memory = new MemoryStream(); Assert.IsTrue(ms.CopyToAsync(ms).Wait(1000)); }
public void It_disposes_of_the_handle_to_the_InProgressSentinel_when_NuGetCacheSentinel_is_disposed() { var mockStream = new MockStream(); var fileMock = new FileMock(); fileMock.InProgressSentinel = mockStream; using (var nugetCacheSentinel = new NuGetCacheSentinel(NUGET_CACHE_PATH, fileMock)) {} mockStream.IsDisposed.Should().BeTrue(); }
public void FlushAsync() { bool called = false; var ms = new MockStream(false, false, false); ms.OnFlush += () => { called = true; }; var t = ms.FlushAsync(); Assert.IsTrue(t.Wait(1000), "#1"); Assert.IsTrue(called, "#2"); }
public void TimeoutWrite() { using (Stream stm = new MockStream()) using (HashedStream hs = new HashedStream(stm)) { stm.WriteTimeout = 123; Assert.Equal(123, hs.WriteTimeout); hs.WriteTimeout = 456; Assert.Equal(456, stm.WriteTimeout); } }
public void WriteAsync() { bool called = false; var buffer = new byte[4]; var ms = new MockStream(false, false, true); ms.OnWrite += (b, p, c) => { called = true; }; var t = ms.WriteAsync(buffer, 0, 4); Assert.IsTrue(t.Wait(1000), "#1"); Assert.IsTrue(called, "#2"); }
public async Task ContentReadStream_GetProperty_ReturnOriginalStream(bool readStreamAsync) { var source = new MockStream(new byte[10]); var content = new StreamContent(source); Stream stream = await content.ReadAsStreamAsync(readStreamAsync); Assert.False(stream.CanWrite); Assert.Equal(source.Length, stream.Length); Assert.Equal(0, source.ReadCount); Assert.NotSame(source, stream); }
public void FlushAsync() { ManualResetEvent mre = new ManualResetEvent(false); var m = new MockStream(true, false, true); var w = new StreamWriter(m); w.Write(1); Assert.AreEqual(0L, m.Length, "#1"); var t = w.WriteLineAsync(); Assert.IsTrue(t.Wait(1000), "#2"); }
public void FlushAsync_Exception() { var ms = new MockStream(false, false, false); ms.OnFlush += () => { throw new ApplicationException(); }; var t = ms.FlushAsync(); try { t.Wait(1000); Assert.Fail(); } catch (AggregateException) { } }
public async Task CopyToAsync_CallMultipleTimesWithStreamSupportingSeeking_ContentIsSerializedMultipleTimes() { var source = new MockStream(new byte[10], true, true); // Supports seeking. var content = new StreamContent(source); var destination1 = new MemoryStream(); await content.CopyToAsync(destination1); Assert.Equal(source.Length, destination1.Length); var destination2 = new MemoryStream(); await content.CopyToAsync(destination2); Assert.Equal(source.Length, destination2.Length); }
public void ReadAsync() { bool called = false; var buffer = new byte[4]; var ms = new MockStream(true, false, false); ms.OnRead += (b, p, c) => { called = true; return(2); }; var t = ms.ReadAsync(buffer, 0, 4); Assert.IsTrue(t.Wait(1000), "#1"); Assert.IsTrue(called, "#2"); Assert.AreEqual(2, t.Result, "#3"); }
public void WriteAsync_Exception() { var buffer = new byte[4]; var ms = new MockStream(false, false, true); ms.OnWrite += (b, p, c) => { throw new ApplicationException(); }; var t = ms.WriteAsync(buffer, 0, 4); try { t.Wait(1000); Assert.Fail(); } catch (AggregateException) { } }
public void TestFlushStream() { var stream = new MockStream(); var adapter = new StreamDataAdapter(stream, true); Assert.AreEqual(0, stream._FlushCount); var f = adapter.Flush(); f.GetCompletionEvent().Wait(); Assert.AreEqual(1, stream._FlushCount); adapter.Dispose(); }
public async Task test(int projectionSeq, int streamSeq, int expectedRequestedDiscardSeq, int expectedResets) { if (streamSeq < 0 || projectionSeq < 0 || expectedRequestedDiscardSeq < 0 || expectedResets < 0) { throw new Exception("positive int required"); } var projection = Mock.Of <IReifiedProjection>(proj => proj.Sequence == (uint)projectionSeq); var stream = new MockStream((uint)streamSeq); await EventStreamWrapper <object, object> .Catchup(projection, stream); Assert.Equal((uint)expectedRequestedDiscardSeq, stream.RequestedDiscardSeq); Assert.Equal(expectedResets, stream.NbResets); }
public void Trimming_reader_retries_against_stream_when_request_bytes_are_not_met_using_Read() { var stream = new MockStream(); using (var reader = new ReaderWrapper(new TrimmingTextReader(new StreamReader(stream, Encoding.ASCII)))) { var buffer = new char[4000]; var total = 0; while (total < buffer.Length) { var read = reader.Read(buffer, total, buffer.Length - total); _log.DebugFormat("requested {0}, got {1}", buffer.Length - total, read); Assert.IsTrue(read > 0); total += read; } } }
public async Task CopyToAsync_CallMultipleTimesWithStreamNotSupportingSeeking_ThrowsInvalidOperationException() { var source = new MockStream(new byte[10], false, true); // doesn't support seeking. var content = new StreamContent(source); var destination1 = new MemoryStream(); await content.CopyToAsync(destination1); // Use hardcoded expected length, since source.Length would throw (source stream gets disposed if non-seekable). Assert.Equal(10, destination1.Length); // Note that the InvalidOperationException is thrown in CopyToAsync(). It is not thrown inside the task. var destination2 = new MemoryStream(); Assert.Throws <InvalidOperationException>(() => { Task t = content.CopyToAsync(destination2); }); }
public async Task ContentReadStream_GetPropertyPartiallyConsumed_ReturnOriginalStream(bool readStreamAsync) { int consumed = 4; var source = new MockStream(new byte[10]); source.Read(new byte[consumed], 0, consumed); var content = new StreamContent(source); Stream stream = await content.ReadAsStreamAsync(readStreamAsync); Assert.False(stream.CanWrite); Assert.Equal(source.Length, stream.Length); Assert.Equal(1, source.ReadCount); Assert.Equal(consumed, stream.Position); Assert.NotSame(source, stream); }
public void DisposeWillDisposeBaseStreamIfNotRequestedToLeaveOpen() { using (MemoryStream ms = new MemoryStream()) { bool didDispose = false; MockStream stream = new MockStream(ms, true, true, true); stream.OnDispose += (x) => { didDispose = true; }; using (BinaryWriterEx writer = new BinaryWriterEx(stream, false, true)) { //Do nothing } //Ensure that the base Stream was disposed Assert.IsTrue(didDispose); } }
public void Raw_GZip_Stream_Fails_On_Mocked_Stream() { TestDelegate act = () => { using (var targetStream = new MemoryStream()) { using (var mockStream = new MockStream(compressedData, 5)) { using (var gzipStream = new GZipStream(mockStream, CompressionMode.Decompress)) { Streams.Copy(gzipStream, targetStream); } } } }; Assert.Throws(typeof(ZlibException), act); }
public async Task CopyToAsync_CallMultipleTimesWithStreamSupportingSeekingPartiallyConsumed_ContentIsSerializedMultipleTimesFromInitialPoint() { int consumed = 4; var source = new MockStream(new byte[10], true, true); // supports seeking. source.Read(new byte[consumed], 0, consumed); var content = new StreamContent(source); var destination1 = new MemoryStream(); await content.CopyToAsync(destination1); Assert.Equal(source.Length - consumed, destination1.Length); var destination2 = new MemoryStream(); await content.CopyToAsync(destination2); Assert.Equal(source.Length - consumed, destination2.Length); }
public void DisposeWillLeaveBaseStreamOpenIfRequested() { using (MemoryStream ms = new MemoryStream()) { bool canDisposeNow = false; using (MockStream stream = new MockStream(ms, true, true, true)) { stream.OnDispose += (x) => { Assert.IsTrue(canDisposeNow); }; using (BinaryWriterEx reader = new BinaryWriterEx(stream, true, true)) { //Do nothing } canDisposeNow = true; } } }
public async Task ContentReadStream_GetProperty_ReturnOriginalStream() { var source = new MockStream(new byte[10]); var content = new StreamContent(source); Stream stream = await content.ReadAsStreamAsync(); Assert.False(stream.CanWrite); Assert.Equal(source.Length, stream.Length); Assert.Equal(0, source.ReadCount); Assert.NotSame(source, stream); }
public void WriteAsync () { var m = new MockStream(true, false, true); var w = new StreamWriter (m); var t = w.WriteAsync ("v"); Assert.IsTrue (t.Wait (1000), "#1"); t = w.WriteAsync ((string) null); Assert.IsTrue (t.Wait (1000), "#2"); t = w.WriteLineAsync ("line"); Assert.IsTrue (t.Wait (1000), "#3"); t = w.WriteLineAsync ((string) null); Assert.IsTrue (t.Wait (1000), "#4"); t = w.WriteLineAsync ('c'); Assert.IsTrue (t.Wait (1000), "#5"); }
public void FlushAsync () { ManualResetEvent mre = new ManualResetEvent (false); var m = new MockStream(true, false, true); var w = new StreamWriter (m); w.Write(1); Assert.AreEqual (0L, m.Length, "#1"); var t = w.WriteLineAsync (); Assert.IsTrue (t.Wait (1000), "#2"); Assert.IsTrue (w.FlushAsync ().Wait (1000), "#3"); }
public void WriteAsync_Exception () { var buffer = new byte[4]; var ms = new MockStream (false, false, true); ms.OnWrite += (b, p, c) => { throw new ApplicationException (); }; var t = ms.WriteAsync (buffer, 0, 4); try { t.Wait (1000); Assert.Fail (); } catch (AggregateException) { } }
public void Trimming_reader_retries_against_stream_when_request_bytes_are_not_met_using_Read() { var stream = new MockStream(); using(var reader = new ReaderWrapper(new TrimmingTextReader(new StreamReader(stream, Encoding.ASCII)))) { var buffer = new char[4000]; var total = 0; while(total < buffer.Length) { var read = reader.Read(buffer, total, buffer.Length - total); _log.DebugFormat("requested {0}, got {1}", buffer.Length - total, read); Assert.IsTrue(read > 0); total += read; } } }
public async Task ContentReadStream_GetPropertyPartiallyConsumed_ReturnOriginalStream() { int consumed = 4; var source = new MockStream(new byte[10]); source.Read(new byte[consumed], 0, consumed); var content = new StreamContent(source); Stream stream = await content.ReadAsStreamAsync(); Assert.False(stream.CanWrite); Assert.Equal(source.Length, stream.Length); Assert.Equal(1, source.ReadCount); Assert.Equal(consumed, stream.Position); Assert.NotSame(source, stream); }
public async Task CopyToAsync_CallMultipleTimesWithStreamNotSupportingSeeking_ThrowsInvalidOperationException() { var source = new MockStream(new byte[10], false, true); // doesn't support seeking. var content = new StreamContent(source); var destination1 = new MemoryStream(); await content.CopyToAsync(destination1); // Use hardcoded expected length, since source.Length would throw (source stream gets disposed if non-seekable). Assert.Equal(10, destination1.Length); // Note that the InvalidOperationException is thrown in CopyToAsync(). It is not thrown inside the task. var destination2 = new MemoryStream(); Assert.Throws<InvalidOperationException>(() => { Task t = content.CopyToAsync(destination2); }); }
public async Task ContentReadStream_CheckResultProperties_ValuesRepresentReadOnlyStream() { byte[] data = new byte[10]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)i; } var source = new MockStream(data); var content = new StreamContent(source); Stream contentReadStream = await content.ReadAsStreamAsync(); // The following checks verify that the stream returned passes all read-related properties to the // underlying MockStream and throws when using write-related members. Assert.False(contentReadStream.CanWrite); Assert.True(contentReadStream.CanRead); Assert.Equal(source.Length, contentReadStream.Length); Assert.Equal(1, source.CanSeekCount); _output.WriteLine(contentReadStream.CanSeek.ToString()); Assert.Equal(2, source.CanSeekCount); contentReadStream.Position = 3; // No exception. Assert.Equal(3, contentReadStream.Position); byte byteOnIndex3 = (byte)contentReadStream.ReadByte(); Assert.Equal(data[3], byteOnIndex3); byte[] byteOnIndex4 = new byte[1]; int result = await contentReadStream.ReadAsync(byteOnIndex4, 0, 1); Assert.Equal(1, result); Assert.Equal(data[4], byteOnIndex4[0]); byte[] byteOnIndex5 = new byte[1]; Assert.Equal(1, contentReadStream.Read(byteOnIndex5, 0, 1)); Assert.Equal(data[5], byteOnIndex5[0]); contentReadStream.ReadTimeout = 123; Assert.Equal(123, source.ReadTimeout); Assert.Equal(123, contentReadStream.ReadTimeout); Assert.Equal(0, source.CanTimeoutCount); _output.WriteLine(contentReadStream.CanTimeout.ToString()); Assert.Equal(1, source.CanTimeoutCount); Assert.Equal(0, source.SeekCount); contentReadStream.Seek(0, SeekOrigin.Begin); Assert.Equal(1, source.SeekCount); Assert.Throws<NotSupportedException>(() => { contentReadStream.WriteTimeout = 5; }); Assert.Throws<NotSupportedException>(() => contentReadStream.WriteTimeout.ToString()); Assert.Throws<NotSupportedException>(() => contentReadStream.Flush()); Assert.Throws<NotSupportedException>(() => contentReadStream.SetLength(1)); Assert.Throws<NotSupportedException>(() => contentReadStream.Write(null, 0, 0)); Assert.Throws<NotSupportedException>(() => contentReadStream.WriteByte(1)); Assert.Equal(0, source.DisposeCount); contentReadStream.Dispose(); Assert.Equal(1, source.DisposeCount); }
public async Task CopyToAsync_CallMultipleTimesWithStreamNotSupportingSeekingButBufferedStreamPartiallyConsumed_ContentSerializedOnceToBuffer() { int consumed = 4; var source = new MockStream(new byte[10], false, true); // doesn't support seeking. source.Read(new byte[consumed], 0, consumed); var content = new StreamContent(source); // After loading the content into a buffer, we should be able to copy the content to a destination stream // multiple times, even though the stream doesn't support seeking. await content.LoadIntoBufferAsync(); var destination1 = new MemoryStream(); await content.CopyToAsync(destination1); // Use hardcoded expected length, since source.Length would throw (source stream gets disposed if non-seekable). Assert.Equal(10 - consumed, destination1.Length); var destination2 = new MemoryStream(); await content.CopyToAsync(destination2); Assert.Equal(10 - consumed, destination2.Length); }
public void CopyAsync () { var ms = new MockStream (true, false, true); int counter = 4; ms.OnRead += delegate { return --counter; }; var memory = new MemoryStream (); Assert.IsTrue (ms.CopyToAsync (ms).Wait (1000)); }
public void CopyToAsync_NullDestination_ThrowsArgumentnullException() { var source = new MockStream(new byte[10]); var content = new StreamContent(source); Assert.Throws<ArgumentNullException>(() => { Task t = content.CopyToAsync(null); }); }
public void FlushAsync () { bool called = false; var ms = new MockStream (false, false, false); ms.OnFlush += () => { called = true; }; var t = ms.FlushAsync (); Assert.IsTrue (t.Wait (1000), "#1"); Assert.IsTrue (called, "#2"); }
public void FlushAsync_Exception () { var ms = new MockStream (false, false, false); ms.OnFlush += () => { throw new ApplicationException (); }; var t = ms.FlushAsync (); try { t.Wait (1000); Assert.Fail (); } catch (AggregateException) { } }
public void ReadAsync () { bool called = false; var buffer = new byte[4]; var ms = new MockStream (true, false, false); ms.OnRead += (b, p, c) => { called = true; return 2; }; var t = ms.ReadAsync (buffer, 0, 4); Assert.IsTrue (t.Wait (1000), "#1"); Assert.IsTrue (called, "#2"); Assert.AreEqual (2, t.Result, "#3"); }
public void WriteAsync () { bool called = false; var buffer = new byte[4]; var ms = new MockStream (false, false, true); ms.OnWrite += (b, p, c) => { called = true; }; var t = ms.WriteAsync (buffer, 0, 4); Assert.IsTrue (t.Wait (1000), "#1"); Assert.IsTrue (called, "#2"); }