Esempio n. 1
0
        public void TestRetryBlock(int failCount)
        {
            bool expectSuccess = failCount < SingleSegmentUploader.MaxBufferUploadAttemptCount;

            int callCount = 0;

            var workingFrontEnd = new InMemoryFrontEnd();
            var fe = new MockableFrontEnd(workingFrontEnd);

            fe.CreateStreamImplementation =
                (streamPath, overwrite, data, byteCount) =>
            {
                callCount++;
                if (callCount <= failCount)
                {
                    throw new IntentionalException();
                }
                workingFrontEnd.CreateStream(streamPath, overwrite, data, byteCount);
            };

            fe.AppendToStreamImplementation =
                (streamPath, data, offset, byteCount) =>
            {
                callCount++;
                if (callCount <= failCount)
                {
                    throw new IntentionalException();
                }
                workingFrontEnd.AppendToStream(streamPath, data, offset, byteCount);
            };

            var metadata        = CreateMetadata(_smallFilePath, _smallFileContents.Length);
            var progressTracker = new TestProgressTracker();
            var ssu             = new SingleSegmentUploader(0, metadata, fe, progressTracker);

            ssu.UseBackOffRetryStrategy = false;

            if (expectSuccess)
            {
                ssu.Upload();
                var actualContents = workingFrontEnd.GetStreamContents(StreamPath);
                AssertExtensions.AreEqual(_smallFileContents, actualContents, "Unexpected uploaded stream contents.");
            }
            else
            {
                Assert.Throws <IntentionalException>(() => { ssu.Upload(); });
            }
            VerifyTracker(progressTracker, expectSuccess);
        }
Esempio n. 2
0
        public void SingleSegmentUploader_TargetStreamExists()
        {
            var fe = new InMemoryFrontEnd();

            //load up an existing stream
            fe.CreateStream(StreamPath, true, null, 0);
            var data = Encoding.UTF8.GetBytes("random");

            fe.AppendToStream(StreamPath, data, 0, (int)data.Length);

            //force a re-upload of the stream
            var metadata = CreateMetadata(_smallFilePath, _smallFileContents.Length);
            var ssu      = new SingleSegmentUploader(0, metadata, fe);

            ssu.UseBackOffRetryStrategy = false;
            ssu.Upload();

            var actualContents = fe.GetStreamContents(StreamPath);

            AssertExtensions.AreEqual(_smallFileContents, actualContents, "Unexpected uploaded stream contents.");
        }
        private void TestRetry(int segmentFailCount)
        {
            //we only have access to the underlying FrontEnd, so we need to simulate many exceptions in order to force a segment to fail the upload (multiply by SingleSegmentUploader.MaxBufferUploadAttemptAccount)
            //this only works because we have a small file, which we know will fit in only one buffer (for a larger file, more complex operations are necessary)
            int  actualfailCount = segmentFailCount * SingleSegmentUploader.MaxBufferUploadAttemptCount;
            bool expectSuccess   = segmentFailCount < MultipleSegmentUploader.MaxUploadAttemptCount;

            int callCount = 0;

            //create a mock front end sitting on top of a working front end that simulates some erros for some time
            var workingFrontEnd = new InMemoryFrontEnd();
            var fe = new MockableFrontEnd(workingFrontEnd);

            fe.CreateStreamImplementation =
                (streamPath, overwrite, data, byteCount) =>
            {
                callCount++;
                if (callCount <= actualfailCount)
                {
                    throw new IntentionalException();
                }
                workingFrontEnd.CreateStream(streamPath, overwrite, data, byteCount);
            };

            fe.AppendToStreamImplementation =
                (streamPath, data, offset, byteCount) =>
            {
                callCount++;
                if (callCount <= actualfailCount)
                {
                    throw new IntentionalException();
                }
                workingFrontEnd.AppendToStream(streamPath, data, offset, byteCount);
            };

            var metadata = CreateMetadata(1);

            try
            {
                var msu = new MultipleSegmentUploader(metadata, 1, fe);
                msu.UseSegmentBlockBackOffRetryStrategy = false;

                if (expectSuccess)
                {
                    //the Upload method should not throw any exceptions in this case
                    msu.Upload();

                    //if we are expecting success, verify that both the metadata and the target streams are complete
                    VerifyTargetStreamsAreComplete(metadata, workingFrontEnd);
                }
                else
                {
                    //the Upload method should throw an aggregate exception in this case
                    Assert.Throws <AggregateException>(() => { msu.Upload(); });

                    //if we do not expect success, verify that at least 1 segment was marked as Failed
                    Assert.True(metadata.Segments.Any(s => s.Status == SegmentUploadStatus.Failed), "Could not find any failed segments");

                    //for every other segment, verify it was completed OK
                    foreach (var segment in metadata.Segments.Where(s => s.Status != SegmentUploadStatus.Failed))
                    {
                        VerifyTargetStreamIsComplete(segment, metadata, workingFrontEnd);
                    }
                }
            }
            finally
            {
                metadata.DeleteFile();
            }
        }
        private void TestRetry(int segmentFailCount)
        {
            //we only have access to the underlying FrontEnd, so we need to simulate many exceptions in order to force a segment to fail the upload (multiply by SingleSegmentUploader.MaxBufferUploadAttemptAccount)
            //this only works because we have a small file, which we know will fit in only one buffer (for a larger file, more complex operations are necessary)
            int actualfailCount = segmentFailCount * SingleSegmentUploader.MaxBufferUploadAttemptCount;
            bool expectSuccess = segmentFailCount < MultipleSegmentUploader.MaxUploadAttemptCount;

            int callCount = 0;

            //create a mock front end sitting on top of a working front end that simulates some erros for some time
            var workingFrontEnd = new InMemoryFrontEnd();
            var fe = new MockableFrontEnd(workingFrontEnd);
            fe.CreateStreamImplementation =
                (streamPath, overwrite, data, byteCount) =>
                {
                    callCount++;
                    if (callCount <= actualfailCount)
                    {
                        throw new IntentionalException();
                    }
                    workingFrontEnd.CreateStream(streamPath, overwrite, data, byteCount);
                };

            fe.AppendToStreamImplementation =
                (streamPath, data, offset, byteCount) =>
                {
                    callCount++;
                    if (callCount <= actualfailCount)
                    {
                        throw new IntentionalException();
                    }
                    workingFrontEnd.AppendToStream(streamPath, data, offset, byteCount);
                };

            var metadata = CreateMetadata(1);
            try
            {
                var msu = new MultipleSegmentUploader(metadata, 1, fe);
                msu.UseSegmentBlockBackOffRetryStrategy = false;

                if (expectSuccess)
                {
                    //the Upload method should not throw any exceptions in this case
                    Assert.DoesNotThrow(() => { msu.Upload(); });
                    
                    //if we are expecting success, verify that both the metadata and the target streams are complete
                    VerifyTargetStreamsAreComplete(metadata, workingFrontEnd);
                }
                else
                {
                    //the Upload method should throw an aggregate exception in this case
                    Assert.Throws<AggregateException>(() => { msu.Upload(); });

                    //if we do not expect success, verify that at least 1 segment was marked as Failed
                    Assert.True(metadata.Segments.Any(s => s.Status == SegmentUploadStatus.Failed), "Could not find any failed segments");

                    //for every other segment, verify it was completed OK
                    foreach (var segment in metadata.Segments.Where(s => s.Status != SegmentUploadStatus.Failed))
                    {
                        VerifyTargetStreamIsComplete(segment, metadata, workingFrontEnd);
                    }
                }
            }
            finally
            {
                metadata.DeleteFile();
            }
        }