public async Task BlobOpenReadWriteTestAsync()
        {
            byte[]             buffer    = GetRandomBuffer(2 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudPageBlob blob = container.GetPageBlobReference("blob1");

                using (ICloudBlobStream blobStream = await blob.OpenWriteAsync(2048))
                {
                    Stream blobStreamForWrite = blobStream.AsStreamForWrite();
                    await blobStreamForWrite.WriteAsync(buffer, 0, 2048);

                    await blobStreamForWrite.FlushAsync();
                }

                using (IInputStream dstStream = await blob.OpenReadAsync())
                {
                    Stream       dstStreamForRead = dstStream.AsStreamForRead();
                    MemoryStream memoryStream     = new MemoryStream(buffer);
                    TestHelper.AssertStreamsAreEqual(memoryStream, dstStreamForRead);
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
Beispiel #2
0
        public async Task CloudBlobClientMaximumExecutionTimeoutShouldNotBeHonoredForStreamsAsync()
        {
            CloudBlobClient    blobClient = GenerateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference(Guid.NewGuid().ToString("N"));

            byte[] buffer = BlobTestBase.GetRandomBuffer(1024 * 1024);

            try
            {
                await container.CreateAsync();

                blobClient.MaximumExecutionTime = TimeSpan.FromSeconds(30);
                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                CloudPageBlob  pageBlob  = container.GetPageBlobReference("blob2");
                blockBlob.StreamWriteSizeInBytes       = 1024 * 1024;
                blockBlob.StreamMinimumReadSizeInBytes = 1024 * 1024;
                blockBlob.StreamMinimumReadSizeInBytes = 1024 * 1024;
                pageBlob.StreamMinimumReadSizeInBytes  = 1024 * 1024;

                using (ICloudBlobStream bos = await blockBlob.OpenWriteAsync())
                {
                    DateTime start = DateTime.Now;
                    for (int i = 0; i < 7; i++)
                    {
                        await bos.WriteAsync(buffer.AsBuffer());
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last write
                    int msRemaining = (int)(blobClient.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    await bos.WriteAsync(buffer.AsBuffer());

                    await bos.CommitAsync();
                }

                using (Stream bis = (await blockBlob.OpenReadAsync()).AsStreamForRead())
                {
                    DateTime start = DateTime.Now;
                    int      total = 0;
                    while (total < 7 * 1024 * 1024)
                    {
                        total += await bis.ReadAsync(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last read
                    int msRemaining = (int)(blobClient.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    while (true)
                    {
                        int count = await bis.ReadAsync(buffer, 0, buffer.Length);

                        total += count;
                        if (count == 0)
                        {
                            break;
                        }
                    }
                }

                using (ICloudBlobStream bos = await pageBlob.OpenWriteAsync(8 * 1024 * 1024))
                {
                    DateTime start = DateTime.Now;
                    for (int i = 0; i < 7; i++)
                    {
                        await bos.WriteAsync(buffer.AsBuffer());
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last write
                    int msRemaining = (int)(blobClient.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    await bos.WriteAsync(buffer.AsBuffer());

                    await bos.CommitAsync();
                }

                using (Stream bis = (await pageBlob.OpenReadAsync()).AsStreamForRead())
                {
                    DateTime start = DateTime.Now;
                    int      total = 0;
                    while (total < 7 * 1024 * 1024)
                    {
                        total += await bis.ReadAsync(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last read
                    int msRemaining = (int)(blobClient.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    while (true)
                    {
                        int count = await bis.ReadAsync(buffer, 0, buffer.Length);

                        total += count;
                        if (count == 0)
                        {
                            break;
                        }
                    }
                }
            }

            finally
            {
                blobClient.MaximumExecutionTime = null;
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }