Esempio n. 1
0
        private static void ValidateCopier(int bufferLength, long?copyLength, long?maxLength, bool inputSync, int inputDelayInMs, int inputFailRequest, bool outputSync, int outputDelayInMs, int outputFailRequest, DateTime?copyTimeout, bool seekable)
        {
            byte[] buffer = GetRandomBuffer(bufferLength);

            // Finds ceiling of division operation
            int expectedCallCount =
                (int)
                (copyLength.HasValue && buffer.Length > copyLength
                     ? (-1L + copyLength + Constants.DefaultBufferSize) / Constants.DefaultBufferSize
                     : (-1L + buffer.Length + Constants.DefaultBufferSize) / Constants.DefaultBufferSize);

            int totalDelayInMs = (expectedCallCount + 1) * inputDelayInMs + expectedCallCount * outputDelayInMs;

            DataValidationStream   input          = new DataValidationStream(buffer, inputSync, inputDelayInMs, inputFailRequest, seekable);
            DataValidationStream   output         = new DataValidationStream(buffer, outputSync, outputDelayInMs, outputFailRequest, seekable);
            RESTCommand <NullType> cmdWithTimeout = new RESTCommand <NullType>(new StorageCredentials(), null)
            {
                OperationExpiryTime = copyTimeout
            };
            ExecutionState <NullType> state     = new ExecutionState <NullType>(cmdWithTimeout, null, null);
            StreamDescriptor          copyState = new StreamDescriptor();

            using (ManualResetEvent waitHandle = new ManualResetEvent(false))
            {
                var mockBufferManager = new MockBufferManager(Constants.DefaultBufferSize);

                input.WriteToAsync(output, mockBufferManager, copyLength, maxLength, false, state, copyState, _ => waitHandle.Set());
                Assert.IsTrue(waitHandle.WaitOne(totalDelayInMs + 10 * 1000));
                Assert.AreEqual(0, mockBufferManager.OutstandingBufferCount, "Outstanding buffers not returned to IBufferManager");
            }

            if (inputFailRequest >= 0)
            {
                Assert.AreEqual(input.LastException, state.ExceptionRef);
                Assert.AreEqual(inputFailRequest, input.ReadCallCount);
            }

            if (outputFailRequest >= 0)
            {
                Assert.AreEqual(output.LastException, state.ExceptionRef);
                Assert.AreEqual(outputFailRequest, output.WriteCallCount);
            }

            if (state.ExceptionRef != null)
            {
                throw state.ExceptionRef;
            }

            Assert.AreEqual(copyLength.HasValue ? copyLength : buffer.Length, copyState.Length);
            Assert.AreEqual(copyLength.HasValue ? expectedCallCount : expectedCallCount + 1, input.ReadCallCount);
            Assert.AreEqual(expectedCallCount, output.WriteCallCount);
        }
        public void ReadMessage_Returns_The_Buffer_To_The_Buffer_Manager()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder            encoder = factory.Encoder;

            byte[] bytes = new byte[5] {
                0, 1, 2, 3, 4
            };
            ArraySegment <byte> buffer = new ArraySegment <byte>(bytes);

            MockBufferManager bufferManager = new MockBufferManager();
            HttpMessage       message       = encoder.ReadMessage(buffer, bufferManager, "someType/someSubType") as HttpMessage;

            Assert.IsTrue(bufferManager.ReturnBufferCalled, "HttpMessageEncoder.ReadMessage should have returned the buffer to the buffer manager.");
            Assert.AreSame(bytes, bufferManager.BufferReturned, "HttpMessageEncoder.ReadMessage should have returned the original buffer instance to the buffer manager.");
        }
        public void WriteMessage_Takes_A_Buffer_From_The_BufferManager()
        {
            HttpMessageEncoderFactory factory = new HttpMessageEncoderFactory();
            MessageEncoder            encoder = factory.Encoder;

            HttpResponseMessage response = new HttpResponseMessage();

            response.Content = new ByteArrayContent(new byte[5] {
                0, 1, 2, 3, 4
            });
            HttpMessage message = new HttpMessage(response);

            MockBufferManager   bufferManager = new MockBufferManager();
            ArraySegment <byte> buffer        = encoder.WriteMessage(message, int.MaxValue, bufferManager);

            Assert.IsTrue(bufferManager.TakeBufferCalled, "HttpMessageEncoder.WriteMessage should have taken a buffer from the bufferManager.");
            Assert.AreSame(buffer.Array, bufferManager.BufferTaken, "HttpMessageEncoder.WriteMessage should have returned the array taken from the bufferManager.");
        }