public void WhenMaxAllowedWriteStreamBytesPassedWriteStreamIsConvertedToReadStream() { var forwardableCurrentDateTimeSource = new ForwardableCurrentDateTimeSource(DateTime.Now); long convertedWriteStreamBytes = 0; using (var rollingMemory = (new RollingMemoryBuilder()) .SetupStreamLifecycleImplementation( CreateMemoryWriteStreamFactory(null), CreateMemoryWriteStreamToReadStreamConverter(stream => convertedWriteStreamBytes += stream.Length), CreateMemoryStreamDataSourceDisposerFactory(null), CreateMemoryStreamDataSourceDisposerFactory(null)) .SetStreamLifecycleThresholds( 2, null, null) .SetCurrentDateTimeSource( forwardableCurrentDateTimeSource).Build()) { var testData = new byte[] { 1, 2, 3, 4 }; rollingMemory.WriteStream.Write(testData, 0, testData.Length); Assert.IsTrue(convertedWriteStreamBytes >= 2); } }
public void WhenMaxUnavailableAgeNotPassedDataIsUnavailable() { var forwardableCurrentDateTimeSource = new ForwardableCurrentDateTimeSource(DateTime.Now); int readCount; using (var rollingMemory = (new RollingMemoryBuilder()) .SetupStreamLifecycleImplementation( CreateMemoryWriteStreamFactory(null), CreateMemoryWriteStreamToReadStreamConverter(null), CreateMemoryStreamDataSourceDisposerFactory(null), CreateMemoryStreamDataSourceDisposerFactory(null)) .SetStreamLifecycleThresholds( null, TimeSpan.FromSeconds(1), null) .SetCurrentDateTimeSource( forwardableCurrentDateTimeSource).Build()) { var testData = new byte[] { 1, 2, 3, 4 }; rollingMemory.WriteStream.Write(testData, 0, testData.Length); var readResult = new byte[testData.Length]; readCount = rollingMemory.ReadStream.Read(readResult, 0, readResult.Length); } Assert.AreEqual(0, readCount); }
public void WhenReadStreamTemporarilyUnavailableReturnsFewerBytes() { var forwardableCurrentDateTimeSource = new ForwardableCurrentDateTimeSource(DateTime.Now); int readCount = 0; var writeStreamToReadStreamConverterMock = new Mock<IWriteStreamToReadStreamConverter>(); writeStreamToReadStreamConverterMock.Setup(writeStreamToReadStreamConverter => writeStreamToReadStreamConverter.ConvertWriteStreamToReadStream(It.Is<Stream>(stream => stream is MemoryStream))) .Returns((Func<Stream, Stream>)(stream => { var memoryStream = stream as MemoryStream; memoryStream.Position = 0; var readStreamMock = new Mock<Stream>(); readStreamMock.Setup(readStream => readStream.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>())) .Returns((Func<byte[], int, int, int>)((buffer, offset, count) => { return memoryStream.Read(buffer, offset, Math.Min(1, count)); })); return readStreamMock.Object; })); var readStreamDataSourceDisposerFactoryMock = new Mock<IStreamDataSourceDisposerFactory>(); var readStreamDataSourceDisposerMock = new Mock<IDataSourceDisposer>(); readStreamDataSourceDisposerFactoryMock.Setup(readStreamDataSourceDisposerFactory => readStreamDataSourceDisposerFactory.CreateStreamDataSourceDisposer(It.IsAny<Stream>())) .Returns(readStreamDataSourceDisposerMock.Object); using (var rollingMemory = (new RollingMemoryBuilder()) .SetupStreamLifecycleImplementation( CreateMemoryWriteStreamFactory(null), writeStreamToReadStreamConverterMock.Object, CreateMemoryStreamDataSourceDisposerFactory(null), readStreamDataSourceDisposerFactoryMock.Object) .SetStreamLifecycleThresholds( null, null, 0) .SetCurrentDateTimeSource( forwardableCurrentDateTimeSource).Build()) { var testData = new byte[] { 1, 2, 3, 4 }; rollingMemory.WriteStream.Write(testData, 0, testData.Length); var readResult = new byte[testData.Length]; readCount = rollingMemory.ReadStream.Read(readResult, 0, readResult.Length); readCount += rollingMemory.ReadStream.Read(readResult, readCount, readResult.Length - readCount); Assert.IsTrue(readResult.Take(readCount).SequenceEqual(new byte[] { 1, 2 })); } }