public void TargetStreamIsCommittedOnlyOnceEvenIfStreamIsRewound() { var targetStream = new Mock <Stream> { CallBase = true }; var streamTransacted = targetStream.As <ITransactionalStream>(); streamTransacted.Setup(s => s.Commit()); using (var stream = new ReplicatingReadStream(new MemoryStream(_content), targetStream.Object)) { stream.Drain(); stream.Position = 0; stream.Drain(); } streamTransacted.Verify(s => s.Commit(), Times.Once()); }
public void SourceStreamCannotBeSoughtBeforeExhaustion() { using (var stream = new ReplicatingReadStream(new MemoryStream(_content), new MemoryStream())) { Assert.That(stream.CanSeek, Is.False); // don't drain the whole stream stream.Read(new byte[1024], 0, 1024); Assert.That( () => stream.Position = 0, Throws.InstanceOf <InvalidOperationException>() .With.Message.EqualTo( string.Format("{0} is not seekable while the inner stream has not been thoroughly read and replicated.", typeof(ReplicatingReadStream).Name))); Assert.That( () => stream.Seek(0, SeekOrigin.Begin), Throws.InstanceOf <InvalidOperationException>() .With.Message.EqualTo( string.Format("{0} cannot be sought while the inner stream has not been thoroughly read and replicated.", typeof(ReplicatingReadStream).Name))); } }