Beispiel #1
0
        public async Task CloudPageBlobUploadDownloadFileWithFailures()
        {
            CloudPageBlob blob     = this.testContainer.GetPageBlobReference("blob1");
            CloudPageBlob nullBlob = this.testContainer.GetPageBlobReference("null");

            await this.DoUploadDownloadFileAsync(blob, 0);

            await this.DoUploadDownloadFileAsync(blob, 4096);

            await TestHelper.ExpectedExceptionAsync <ArgumentException>(
                async() => await this.DoUploadDownloadFileAsync(blob, 4097),
                "Page blobs must be 512-byte aligned");

            await TestHelper.ExpectedExceptionAsync <IOException>(
                async() => await blob.UploadFromFileAsync("non_existentCloudPageBlobUploadDownloadFileWithFailures.file"),
                "UploadFromFileAsync requires an existing file");

            await TestHelper.ExpectedExceptionAsync <StorageException>(
                async() => await nullBlob.DownloadToFileAsync("garbageCloudPageBlobUploadDownloadFileWithFailures.file", FileMode.Create),
                "DownloadToFileAsync should not leave an empty file behind after failing.");

            Assert.IsFalse(System.IO.File.Exists("garbageCloudPageBlobUploadDownloadFileWithFailures.file"));

            await TestHelper.ExpectedExceptionAsync <StorageException>(
                async() => await nullBlob.DownloadToFileAsync("garbageCloudPageBlobUploadDownloadFileWithFailures.file", FileMode.CreateNew),
                "DownloadToFileAsync should not leave an empty file behind after failing.");

            Assert.IsFalse(System.IO.File.Exists("garbageCloudPageBlobUploadDownloadFileWithFailures.file"));

            byte[] buffer = GetRandomBuffer(100);
            using (FileStream file = new FileStream("garbageCloudPageBlobUploadDownloadFileWithFailures.file", FileMode.Create, FileAccess.Write))
            {
                await file.WriteAsync(buffer, 0, buffer.Length);
            }
            await TestHelper.ExpectedExceptionAsync <IOException>(
                async() => await nullBlob.DownloadToFileAsync("garbageCloudPageBlobUploadDownloadFileWithFailures.file", FileMode.CreateNew),
                "DownloadToFileAsync should leave an empty file behind after failing, depending on the mode.");

            Assert.IsTrue(System.IO.File.Exists("garbageCloudPageBlobUploadDownloadFileWithFailures.file"));
            System.IO.File.Delete("garbageCloudPageBlobUploadDownloadFileWithFailures.file");

            await TestHelper.ExpectedExceptionAsync <StorageException>(
                async() => await nullBlob.DownloadToFileAsync("garbageCloudPageBlobUploadDownloadFileWithFailures.file", FileMode.OpenOrCreate),
                "DownloadToFileAsync should leave an empty file behind after failing, depending on file mode.");

            Assert.IsTrue(System.IO.File.Exists("garbageCloudPageBlobUploadDownloadFileWithFailures.file"));
            System.IO.File.Delete("garbageCloudPageBlobUploadDownloadFileWithFailures.file");
        }