Example #1
0
        public void FileWriteStreamBasicTestAPM()
        {
            byte[] buffer = GetRandomBuffer(2 * 1024 * 1024);

            MD5             hasher     = MD5.Create();
            CloudFileClient fileClient = GenerateCloudFileClient();

            fileClient.DefaultRequestOptions.ParallelOperationThreadCount = 4;
            string         name  = GetRandomShareName();
            CloudFileShare share = fileClient.GetShareReference(name);

            try
            {
                share.Create();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                file.StreamWriteSizeInBytes = buffer.Length;

                using (MemoryStream wholeFile = new MemoryStream())
                {
                    FileRequestOptions options = new FileRequestOptions()
                    {
                        StoreFileContentMD5 = true,
                    };

                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        IAsyncResult result = file.BeginOpenWrite(fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value * 2 * buffer.Length, null, options, null,
                                                                  ar => waitHandle.Set(),
                                                                  null);
                        waitHandle.WaitOne();
                        using (CloudFileStream fileStream = file.EndOpenWrite(result))
                        {
                            IAsyncResult[] results = new IAsyncResult[fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value * 2];
                            for (int i = 0; i < results.Length; i++)
                            {
                                results[i] = fileStream.BeginWrite(buffer, 0, buffer.Length, null, null);
                                wholeFile.Write(buffer, 0, buffer.Length);
                                Assert.AreEqual(wholeFile.Position, fileStream.Position);
                            }

                            for (int i = 0; i < fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value; i++)
                            {
                                Assert.IsTrue(results[i].IsCompleted);
                            }

                            for (int i = fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value; i < results.Length; i++)
                            {
                                Assert.IsFalse(results[i].IsCompleted);
                            }

                            for (int i = 0; i < results.Length; i++)
                            {
                                fileStream.EndWrite(results[i]);
                            }

                            result = fileStream.BeginCommit(
                                ar => waitHandle.Set(),
                                null);
                            waitHandle.WaitOne();
                            fileStream.EndCommit(result);
                        }
                    }

                    wholeFile.Seek(0, SeekOrigin.Begin);
                    string md5 = Convert.ToBase64String(hasher.ComputeHash(wholeFile));
                    file.FetchAttributes();
                    Assert.AreEqual(md5, file.Properties.ContentMD5);

                    using (MemoryStream downloadedFile = new MemoryStream())
                    {
                        file.DownloadToStream(downloadedFile);
                        TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile);
                    }

                    fileClient.DefaultRequestOptions.ParallelOperationThreadCount = 2;

                    TestHelper.ExpectedException <ArgumentException>(
                        () => file.BeginOpenWrite(null, null, options, null, null, null),
                        "BeginOpenWrite with StoreFileContentMD5 on an existing file should fail");

                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        IAsyncResult result = file.BeginOpenWrite(null,
                                                                  ar => waitHandle.Set(),
                                                                  null);
                        waitHandle.WaitOne();
                        using (Stream fileStream = file.EndOpenWrite(result))
                        {
                            fileStream.Seek(buffer.Length / 2, SeekOrigin.Begin);
                            wholeFile.Seek(buffer.Length / 2, SeekOrigin.Begin);

                            IAsyncResult[] results = new IAsyncResult[fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value * 2];
                            for (int i = 0; i < results.Length; i++)
                            {
                                results[i] = fileStream.BeginWrite(buffer, 0, buffer.Length, null, null);
                                wholeFile.Write(buffer, 0, buffer.Length);
                                Assert.AreEqual(wholeFile.Position, fileStream.Position);
                            }

                            for (int i = 0; i < fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value; i++)
                            {
                                Assert.IsTrue(results[i].IsCompleted);
                            }

                            for (int i = fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value; i < results.Length; i++)
                            {
                                Assert.IsFalse(results[i].IsCompleted);
                            }

                            for (int i = 0; i < results.Length; i++)
                            {
                                fileStream.EndWrite(results[i]);
                            }

                            wholeFile.Seek(0, SeekOrigin.End);
                        }

                        file.FetchAttributes();
                        Assert.AreEqual(md5, file.Properties.ContentMD5);

                        using (MemoryStream downloadedFile = new MemoryStream())
                        {
                            options.DisableContentMD5Validation = true;
                            file.DownloadToStream(downloadedFile, null, options);
                            TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile);
                        }
                    }
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }