Exemplo n.º 1
0
        public async Task CloudAppendBlobUploadDownloadFileWithFailures()
        {
            CloudAppendBlob blob     = this.testContainer.GetAppendBlobReference("blob1");
            CloudAppendBlob nullBlob = this.testContainer.GetAppendBlobReference("null");

            await this.DoUploadDownloadFileAsync(blob, 0);

            await this.DoUploadDownloadFileAsync(blob, 4096);

            await this.DoUploadDownloadFileAsync(blob, 4097);

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

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

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

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

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

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

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

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

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