public void CreateWriter_BuffersReturned_OnException()
        {
            // Arrange
            var bytePool = new Mock<IArraySegmentPool<byte>>(MockBehavior.Strict);
            bytePool
                .Setup(p => p.Lease(It.IsAny<int>()))
                .Returns(new LeasedArraySegment<byte>(new ArraySegment<byte>(new byte[0]), bytePool.Object));
            bytePool
                .Setup(p => p.Return(It.IsAny<LeasedArraySegment<byte>>()))
                .Verifiable();

            var charPool = new Mock<IArraySegmentPool<char>>(MockBehavior.Strict);
            charPool
                .Setup(p => p.Lease(MemoryPoolHttpResponseStreamWriterFactory.DefaultBufferSize))
                .Returns(new LeasedArraySegment<char>(new ArraySegment<char>(new char[0]), charPool.Object));
            charPool
                .Setup(p => p.Return(It.IsAny<LeasedArraySegment<char>>()))
                .Verifiable();

            var encoding = new Mock<Encoding>();
            encoding
                .Setup(e => e.GetEncoder())
                .Throws(new InvalidOperationException());

            var factory = new MemoryPoolHttpResponseStreamWriterFactory(bytePool.Object, charPool.Object);

            // Act
            Assert.Throws<InvalidOperationException>(() => factory.CreateWriter(new MemoryStream(), encoding.Object));

            // Assert
            bytePool.Verify();
            charPool.Verify();
        }
Example #2
0
        public void CreateWriter_BuffersReturned_OnException()
        {
            // Arrange
            var bytePool = new Mock <IArraySegmentPool <byte> >(MockBehavior.Strict);

            bytePool
            .Setup(p => p.Lease(It.IsAny <int>()))
            .Returns(new LeasedArraySegment <byte>(new ArraySegment <byte>(new byte[0]), bytePool.Object));
            bytePool
            .Setup(p => p.Return(It.IsAny <LeasedArraySegment <byte> >()))
            .Verifiable();

            var charPool = new Mock <IArraySegmentPool <char> >(MockBehavior.Strict);

            charPool
            .Setup(p => p.Lease(MemoryPoolHttpResponseStreamWriterFactory.DefaultBufferSize))
            .Returns(new LeasedArraySegment <char>(new ArraySegment <char>(new char[0]), charPool.Object));
            charPool
            .Setup(p => p.Return(It.IsAny <LeasedArraySegment <char> >()))
            .Verifiable();

            var encoding = new Mock <Encoding>();

            encoding
            .Setup(e => e.GetEncoder())
            .Throws(new InvalidOperationException());

            var factory = new MemoryPoolHttpResponseStreamWriterFactory(bytePool.Object, charPool.Object);

            // Act
            Assert.Throws <InvalidOperationException>(() => factory.CreateWriter(new MemoryStream(), encoding.Object));

            // Assert
            bytePool.Verify();
            charPool.Verify();
        }