public void CloudBlockBlobDownloadRangeToStreamAPMRetry()
        {
            byte[]             buffer    = GetRandomBuffer(1 * 1024 * 1024);
            int                offset    = 1024;
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (MemoryStream originalBlob = new MemoryStream(buffer))
                {
                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        ICancellableAsyncResult result = blob.BeginUploadFromStream(originalBlob,
                                                                                    ar => waitHandle.Set(),
                                                                                    null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);
                    }
                }

                using (MemoryStream originalBlob = new MemoryStream())
                {
                    originalBlob.Write(buffer, offset, buffer.Length - offset);

                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        Exception manglerEx = null;
                        using (HttpMangler proxy = new HttpMangler(false,
                                                                   new[]
                        {
                            TamperBehaviors.TamperNRequestsIf(
                                session => ThreadPool.QueueUserWorkItem(state =>
                            {
                                Thread.Sleep(1000);
                                try
                                {
                                    session.Abort();
                                }
                                catch (Exception e)
                                {
                                    manglerEx = e;
                                }
                            }),
                                2,
                                AzureStorageSelectors.BlobTraffic().IfHostNameContains(container.ServiceClient.Credentials.AccountName))
                        }))
                        {
                            foreach (var options in new[]
                            {
                                new BlobRequestOptions()
                                {
                                    ChecksumOptions =
                                        new ChecksumOptions
                                    {
                                        UseTransactionalMD5 = true,
                                        UseTransactionalCRC64 = false
                                    }
                                },
                                new BlobRequestOptions()
                                {
                                    ChecksumOptions =
                                        new ChecksumOptions
                                    {
                                        UseTransactionalMD5 = false,
                                        UseTransactionalCRC64 = true
                                    }
                                }
                            })
                            {
                                using (MemoryStream downloadedBlob = new MemoryStream())
                                {
                                    OperationContext operationContext = new OperationContext();

                                    ICancellableAsyncResult result = blob.BeginDownloadRangeToStream(downloadedBlob, offset, buffer.Length - offset, null, options, operationContext,
                                                                                                     ar => waitHandle.Set(),
                                                                                                     null);
                                    waitHandle.WaitOne();
                                    blob.EndDownloadToStream(result);
                                    TestHelper.AssertStreamsAreEqual(originalBlob, downloadedBlob);

                                    if (manglerEx != null)
                                    {
                                        throw manglerEx;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Beispiel #2
0
        public void CloudFileDownloadToStreamAPMRetry()
        {
            byte[]         buffer = GetRandomBuffer(1 * 1024 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                share.Create();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                using (MemoryStream originalFile = new MemoryStream(buffer))
                {
                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        ICancellableAsyncResult result = file.BeginUploadFromStream(originalFile,
                                                                                    ar => waitHandle.Set(),
                                                                                    null);
                        waitHandle.WaitOne();
                        file.EndUploadFromStream(result);

                        using (MemoryStream downloadedFile = new MemoryStream())
                        {
                            Exception manglerEx = null;
                            using (HttpMangler proxy = new HttpMangler(false,
                                                                       new[]
                            {
                                TamperBehaviors.TamperNRequestsIf(
                                    session => ThreadPool.QueueUserWorkItem(state =>
                                {
                                    Thread.Sleep(1000);
                                    try
                                    {
                                        session.Abort();
                                    }
                                    catch (Exception e)
                                    {
                                        manglerEx = e;
                                    }
                                }),
                                    2,
                                    AzureStorageSelectors.FileTraffic().IfHostNameContains(share.ServiceClient.Credentials.AccountName))
                            }))
                            {
                                OperationContext operationContext = new OperationContext();
                                result = file.BeginDownloadToStream(downloadedFile, null, null, operationContext,
                                                                    ar => waitHandle.Set(),
                                                                    null);
                                waitHandle.WaitOne();
                                file.EndDownloadToStream(result);
                                TestHelper.AssertStreamsAreEqual(originalFile, downloadedFile);
                            }

                            if (manglerEx != null)
                            {
                                throw manglerEx;
                            }
                        }
                    }
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }