public async Task IfAFileIsSavedTracked_ThenChangesArePersisted()
        {
            var file = Path.GetTempFileName();

            try
            {
                var taskOutputStorage = new TaskOutputStorage(StorageAccount, _jobId, _taskId);
                using (await taskOutputStorage.SaveTrackedAsync(TaskOutputKind.TaskLog, file, "Tracked1.txt", TimeSpan.FromMilliseconds(10)))
                {
                    await Task.Delay(TimeSpan.FromMilliseconds(30));

                    File.AppendAllLines(file, new[] { "Line 1" });
                    await Task.Delay(TimeSpan.FromMilliseconds(20));

                    File.AppendAllLines(file, new[] { "Line 2" });
                    await Task.Delay(TimeSpan.FromMilliseconds(20));

                    File.AppendAllLines(file, new[] { "Line 3" });
                }

                var blob = await taskOutputStorage.GetOutputAsync(TaskOutputKind.TaskLog, "Tracked1.txt");

                var blobContent = await blob.ReadAsByteArrayAsync();

                var originalContent = File.ReadAllBytes(file);

                Assert.Equal(originalContent, blobContent);
            }
            finally
            {
                File.Delete(file);
            }
        }
        public async Task IfARetryPolicyIsSpecifiedInTheStorageAccountConstructor_ThenItIsUsed()
        {
            var taskOutputStorage = new TaskOutputStorage(StorageAccount, _jobId, _taskId, new LinearRetry(TimeSpan.FromSeconds(5), 4));
            await taskOutputStorage.SaveAsync(TaskOutputKind.TaskOutput, FilePath("TestText1.txt"), "SavedWithLinearRetry1.txt");

            var output = await taskOutputStorage.GetOutputAsync(TaskOutputKind.TaskOutput, "SavedWithLinearRetry1.txt");

            var blob          = output.CloudBlob;
            var storageClient = blob.ServiceClient;

            Assert.IsType <LinearRetry>(storageClient.DefaultRequestOptions.RetryPolicy);
        }
        public async Task IfAFileIsSavedWithAPathOutsideTheWorkingDirectory_ThenTheUpPartsOfThePathAreStripped()
        {
            var taskOutputStorage = new TaskOutputStorage(StorageAccount, _jobId, _taskId);
            await taskOutputStorage.SaveAsyncImpl(TaskOutputKind.TaskIntermediate, FileSubfolder("File"), @"..\TestTextForOutsideWorkingDirectory.txt");

            var blob = await taskOutputStorage.GetOutputAsync(TaskOutputKind.TaskIntermediate, "TestTextForOutsideWorkingDirectory.txt");

            var blobContent = await blob.ReadAsByteArrayAsync();

            var originalContent = File.ReadAllBytes(FilePath("TestTextForOutsideWorkingDirectory.txt"));

            Assert.Equal(originalContent, blobContent);
        }
        public async Task IfTextIsSaved_ThenItCanBeGot()
        {
            var sampleXml = "<document><empty /></document>";

            var taskOutputStorage = new TaskOutputStorage(StorageAccount, _jobId, _taskId);
            await taskOutputStorage.SaveTextAsync(TaskOutputKind.TaskOutput, sampleXml, "TextNotFromFile.xml");

            var blob = await taskOutputStorage.GetOutputAsync(TaskOutputKind.TaskOutput, "TextNotFromFile.xml");

            var blobContent = Encoding.UTF8.GetString(await blob.ReadAsByteArrayAsync());

            Assert.Equal(sampleXml, blobContent);
        }
        public async Task IfAFileIsSavedWithAMultiLevelPath_ThenItCanBeGot()
        {
            var taskOutputStorage = new TaskOutputStorage(StorageAccount, _jobId, _taskId);
            await taskOutputStorage.SaveAsync(TaskOutputKind.TaskPreview, FilePath("TestText1.txt"), "This/File/Is/Gettable.txt");

            var blob = await taskOutputStorage.GetOutputAsync(TaskOutputKind.TaskPreview, "This/File/Is/Gettable.txt");

            var blobContent = await blob.ReadAsByteArrayAsync();

            var originalContent = File.ReadAllBytes(FilePath("TestText1.txt"));

            Assert.Equal(originalContent, blobContent);
        }
        public async Task IfARetryPolicyIsSpecifiedInTheContainerUrlConstructor_ThenItIsUsed()
        {
            using (var batchClient = await BatchClient.OpenAsync(new FakeBatchServiceClient()))
            {
                var job       = batchClient.JobOperations.CreateJob(_jobId, null);
                var container = job.GetOutputStorageContainerUrl(StorageAccount, TimeSpan.FromMinutes(2));

                var taskOutputStorage = new TaskOutputStorage(new Uri(container), _taskId, new LinearRetry(TimeSpan.FromSeconds(5), 4));
                await taskOutputStorage.SaveAsync(TaskOutputKind.TaskOutput, FilePath("TestText1.txt"), "SavedWithLinearRetry2.txt");

                var output = await taskOutputStorage.GetOutputAsync(TaskOutputKind.TaskOutput, "SavedWithLinearRetry2.txt");

                var blob          = output.CloudBlob;
                var storageClient = blob.ServiceClient;
                Assert.IsType <LinearRetry>(storageClient.DefaultRequestOptions.RetryPolicy);
            }
        }
        public async Task IfATrackedFileIsIsUseWhenItIsDueToBeFlushed_ThenNoErrorOccursAndChangesArePersisted()
        {
            var file = Path.GetTempFileName();

            try
            {
                var taskOutputStorage = new TaskOutputStorage(StorageAccount, _jobId, _taskId);
                using (await taskOutputStorage.SaveTrackedAsync(TaskOutputKind.TaskLog, file, "Tracked2.txt", TimeSpan.FromMilliseconds(5)))
                {
                    using (var writer = File.AppendText(file))
                    {
                        for (int i = 0; i < 100; ++i)
                        {
                            await Task.Delay(TimeSpan.FromMilliseconds(3));

                            await writer.WriteLineAsync($"Line {i}");

                            await Task.Delay(TimeSpan.FromMilliseconds(3));
                        }
                    }
                    using (var writer = File.AppendText(file))
                    {
                        for (int i = 0; i < 100; ++i)
                        {
                            await writer.WriteLineAsync($"Line {i + 100}");

                            await Task.Delay(TimeSpan.FromMilliseconds(2));
                        }
                    }
                }

                var blob = await taskOutputStorage.GetOutputAsync(TaskOutputKind.TaskLog, "Tracked2.txt");

                var blobContent = await blob.ReadAsByteArrayAsync();

                var originalContent = File.ReadAllBytes(file);

                Assert.Equal(originalContent, blobContent);
            }
            finally
            {
                File.Delete(file);
            }
        }
        public async Task CannotPassANullKindWhenGetting()
        {
            var ex = await Assert.ThrowsAsync <ArgumentNullException>(() => _storage.GetOutputAsync(null, "test.txt"));

            Assert.Equal("kind", ex.ParamName);
        }