Пример #1
0
        public void ClearPageRangeScenarioTest(string containerName, string blobName, HttpStatusCode?expectedError)
        {
            // 1. Create Sparse Page Blob
            int blobSize = 128 * 1024;

            BlobProperties properties = new BlobProperties()
            {
                BlobType = BlobType.PageBlob
            };
            Uri uri = BlobTests.ConstructPutUri(BlobContext.Address, containerName, blobName);
            OperationContext opContext  = new OperationContext();
            HttpWebRequest   webRequest = BlobHttpWebRequestFactory.Put(uri, BlobContext.Timeout, properties, BlobType.PageBlob, blobSize, null, opContext);

            BlobTests.SignRequest(webRequest, BlobContext);

            using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
            {
                BlobTests.PutBlobResponse(response, BlobContext, expectedError);
            }

            // 2. Now upload some page ranges
            for (int m = 0; m * 512 * 4 < blobSize; m++)
            {
                int startOffset = 512 * 4 * m;
                int length      = 512;

                PageRange range = new PageRange(startOffset, startOffset + length - 1);
                opContext = new OperationContext();
                HttpWebRequest pageRequest = BlobHttpWebRequestFactory.PutPage(uri, BlobContext.Timeout, range, PageWrite.Update, null, opContext);
                pageRequest.ContentLength = 512;
                BlobTests.SignRequest(pageRequest, BlobContext);

                Stream outStream = pageRequest.GetRequestStream();

                for (int n = 0; n < 512; n++)
                {
                    outStream.WriteByte((byte)m);
                }

                outStream.Close();
                using (HttpWebResponse pageResponse = pageRequest.GetResponse() as HttpWebResponse)
                {
                }
            }

            // 3. Now do a Get Page Ranges
            List <PageRange> pageRanges = new List <PageRange>();

            opContext = new OperationContext();
            HttpWebRequest pageRangeRequest = BlobHttpWebRequestFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, opContext);

            BlobTests.SignRequest(pageRangeRequest, BlobContext);
            using (HttpWebResponse pageRangeResponse = pageRangeRequest.GetResponse() as HttpWebResponse)
            {
                GetPageRangesResponse getPageRangesResponse = new GetPageRangesResponse(pageRangeResponse.GetResponseStream());
                pageRanges.AddRange(getPageRangesResponse.PageRanges.ToList());
            }

            // 4. Now Clear some pages
            bool skipFlag = false;

            foreach (PageRange pRange in pageRanges)
            {
                skipFlag = !skipFlag;
                if (skipFlag)
                {
                    continue;
                }

                opContext = new OperationContext();
                HttpWebRequest clearPageRequest = BlobHttpWebRequestFactory.PutPage(uri, BlobContext.Timeout, pRange, PageWrite.Clear, null, opContext);
                clearPageRequest.ContentLength = 0;
                BlobTests.SignRequest(clearPageRequest, BlobContext);
                using (HttpWebResponse clearResponse = clearPageRequest.GetResponse() as HttpWebResponse)
                {
                }
            }

            // 5. Get New Page ranges and verify
            List <PageRange> newPageRanges = new List <PageRange>();

            opContext = new OperationContext();
            HttpWebRequest newPageRangeRequest = BlobHttpWebRequestFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, opContext);

            BlobTests.SignRequest(newPageRangeRequest, BlobContext);
            using (HttpWebResponse newPageRangeResponse = newPageRangeRequest.GetResponse() as HttpWebResponse)
            {
                GetPageRangesResponse getNewPageRangesResponse = new GetPageRangesResponse(newPageRangeResponse.GetResponseStream());
                newPageRanges.AddRange(getNewPageRangesResponse.PageRanges.ToList());
            }

            Assert.AreEqual(pageRanges.Count(), newPageRanges.Count() * 2);
            for (int l = 0; l < newPageRanges.Count(); l++)
            {
                Assert.AreEqual(pageRanges[2 * l].StartOffset, newPageRanges[l].StartOffset);
                Assert.AreEqual(pageRanges[2 * l].EndOffset, newPageRanges[l].EndOffset);
            }
        }
        public async Task ClearPageRangeScenarioTest(string containerName, string blobName, HttpStatusCode?expectedError)
        {
            // 1. Create Sparse Page Blob
            int blobSize = 128 * 1024;

            BlobProperties properties = new BlobProperties()
            {
                BlobType = BlobType.PageBlob
            };
            Uri uri = BlobTests.ConstructPutUri(BlobContext.Address, containerName, blobName);
            OperationContext   opContext  = new OperationContext();
            HttpRequestMessage webRequest = BlobHttpRequestMessageFactory.Put(uri, BlobContext.Timeout, properties, BlobType.PageBlob, blobSize, null, null, null, opContext, null, null);


            using (HttpResponseMessage response = await BlobTestUtils.GetResponse(webRequest, BlobContext))
            {
                BlobTests.PutBlobResponse(response, BlobContext, expectedError);
            }

            // 2. Now upload some page ranges
            for (int m = 0; m * 512 * 4 < blobSize; m++)
            {
                int startOffset = 512 * 4 * m;
                int length      = 512;

                PageRange range = new PageRange(startOffset, startOffset + length - 1);
                opContext = new OperationContext();
                HttpRequestMessage pageRequest = BlobHttpRequestMessageFactory.PutPage(uri, BlobContext.Timeout, range, PageWrite.Update, null, null, opContext, null, null);
                HttpRequestHandler.SetContentLength(pageRequest, 512);

                byte[] content = new byte[length];
                random.NextBytes(content);

                pageRequest.Content = new ByteArrayContent(content);

                using (HttpResponseMessage pageResponse = await BlobTestUtils.GetResponse(pageRequest, BlobContext))
                {
                }
            }

            // 3. Now do a Get Page Ranges
            List <PageRange> pageRanges = new List <PageRange>();

            opContext = new OperationContext();
            HttpRequestMessage pageRangeRequest = BlobHttpRequestMessageFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, null, opContext, null, null);

            using (HttpResponseMessage pageRangeResponse = await BlobTestUtils.GetResponse(pageRangeRequest, BlobContext))
            {
                var ranges = await GetPageRangesResponse.ParseAsync(HttpResponseParsers.GetResponseStream(pageRangeResponse), CancellationToken.None);

                pageRanges.AddRange(ranges);
            }

            // 4. Now Clear some pages
            bool skipFlag = false;

            foreach (PageRange pRange in pageRanges)
            {
                skipFlag = !skipFlag;
                if (skipFlag)
                {
                    continue;
                }

                opContext = new OperationContext();
                HttpRequestMessage clearPageRequest = BlobHttpRequestMessageFactory.PutPage(uri, BlobContext.Timeout, pRange, PageWrite.Clear, null, null, opContext, null, null);
                HttpRequestHandler.SetContentLength(clearPageRequest, 0);
                using (HttpResponseMessage clearResponse = await BlobTestUtils.GetResponse(clearPageRequest, BlobContext))
                {
                }
            }

            // 5. Get New Page ranges and verify
            List <PageRange> newPageRanges = new List <PageRange>();

            opContext = new OperationContext();
            HttpRequestMessage newPageRangeRequest = BlobHttpRequestMessageFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, null, opContext, null, null);

            using (HttpResponseMessage newPageRangeResponse = await BlobTestUtils.GetResponse(newPageRangeRequest, BlobContext))
            {
                var ranges = await GetPageRangesResponse.ParseAsync(HttpResponseParsers.GetResponseStream(newPageRangeResponse), CancellationToken.None);

                newPageRanges.AddRange(ranges);
            }

            Assert.AreEqual(pageRanges.Count(), newPageRanges.Count() * 2);
            for (int l = 0; l < newPageRanges.Count(); l++)
            {
                Assert.AreEqual(pageRanges[2 * l].StartOffset, newPageRanges[l].StartOffset);
                Assert.AreEqual(pageRanges[2 * l].EndOffset, newPageRanges[l].EndOffset);
            }
        }