public void ShouldCancelDownloadToFileAsyncTaskAfter50Milliseconds() { string fileUploaded = _smallWmv; string outputDirectory = "Download" + Guid.NewGuid(); string fileDownloaded = Path.Combine(outputDirectory, Path.GetFileName(fileUploaded)); IAsset asset = AssetTests.CreateAsset(_mediaContext, Path.GetFullPath(fileUploaded), AssetCreationOptions.StorageEncrypted); IAssetFile assetFile = asset.AssetFiles.First(); Assert.AreEqual(assetFile.Asset.Id, asset.Id); Assert.AreEqual(1, asset.Locators.Count); CleanDirectory(outputDirectory); var source = new CancellationTokenSource(); IAccessPolicy accessPolicy = _mediaContext.AccessPolicies.Create("SdkDownload", TimeSpan.FromHours(12), AccessPermissions.Read); ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, accessPolicy); BlobTransferClient blobTransferClient = _mediaContext.MediaServicesClassFactory.GetBlobTransferClient(); Exception canceledException = null; Task downloadToFileTask = null; try { downloadToFileTask = assetFile.DownloadAsync(fileDownloaded, blobTransferClient, locator, source.Token); // Send a cancellation signal after 2 seconds. Thread.Sleep(50); source.Cancel(); // Block the thread waiting for the job to finish. downloadToFileTask.Wait(); } catch (AggregateException exception) { Assert.AreEqual(1, exception.InnerExceptions.Count); canceledException = exception.InnerException; } finally { if (File.Exists(fileDownloaded)) { File.Delete(fileDownloaded); } } Assert.IsNotNull(canceledException); Assert.IsInstanceOfType(canceledException, typeof(OperationCanceledException)); // The async task ends in a Canceled state. Assert.AreEqual(TaskStatus.Canceled, downloadToFileTask.Status); CloudMediaContext newContext = WindowsAzureMediaServicesTestConfiguration.CreateCloudMediaContext(); IAsset retreivedAsset = newContext.Assets.Where(a => a.Id == asset.Id).Single(); Assert.AreEqual(2, retreivedAsset.Locators.Count); }
/// <summary> /// Verifies the and download asset. /// </summary> /// <param name="asset">The asset.</param> /// <param name="expectedFileCount">The expected file count.</param> private void VerifyAndDownloadAssetFileNTimes(IAssetFile assetFile, IAsset asset, int count, int expirationTime, bool cleanOutputDirectory, bool expectFailure = false) { var source = new CancellationTokenSource(); if (expirationTime == 0) { expirationTime = 1000; } IAccessPolicy accessPolicy = _mediaContext.AccessPolicies.Create("SdkDownload", TimeSpan.FromSeconds(expirationTime), AccessPermissions.Read); ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, accessPolicy); var blobTransfer = new BlobTransferClient { NumberOfConcurrentTransfers = _mediaContext.NumberOfConcurrentTransfers, ParallelTransferThreadCount = _mediaContext.ParallelTransferThreadCount }; var downloads = new List <Task>(); var paths = new List <string>(); if (cleanOutputDirectory) { CleanDirectory(_outputDirectory); } try { for (int i = 0; i < count; i++) { foreach (IAssetFile file in _mediaContext.Files.Where(c => c.ParentAssetId == asset.Id)) { string path = Path.Combine(_outputDirectory, Guid.NewGuid().ToString()); paths.Add(path); downloads.Add(assetFile.DownloadAsync(path, blobTransfer, locator, source.Token)); } } Task.WaitAll(downloads.ToArray()); } finally { foreach (var fileDownloaded in paths) { if (File.Exists(fileDownloaded)) { File.Delete(fileDownloaded); } else { if (!expectFailure) { Assert.Fail(); } } } } }
public void DoDownloadFileFromAsset(IAsset asset, IAssetFile File, object folder, int index) { // If download is in the queue, let's wait our turn DoGridTransferWaitIfNeeded(index); string labeldb = string.Format("Starting download of '{0}' of asset '{1}' to {2}", File.Name, asset.Name, folder as string); ILocator sasLocator = null; var locatorTask = Task.Factory.StartNew(() => { sasLocator = _context.Locators.Create(LocatorType.Sas, asset, AccessPermissions.Read, TimeSpan.FromHours(24)); }); locatorTask.Wait(); TextBoxLogWriteLine(labeldb); BlobTransferClient blobTransferClient = new BlobTransferClient { NumberOfConcurrentTransfers = _context.NumberOfConcurrentTransfers, ParallelTransferThreadCount = _context.ParallelTransferThreadCount }; Task.Factory.StartNew(async () => { bool Error = false; try { await File.DownloadAsync(Path.Combine(folder as string, File.Name), blobTransferClient, sasLocator, CancellationToken.None); sasLocator.Delete(); } catch (Exception e) { Error = true; TextBoxLogWriteLine(string.Format("Download of file '{0}' failed !", File.Name), true); TextBoxLogWriteLine(e); DoGridTransferDeclareError(index, e); } if (!Error) { DoGridTransferDeclareCompleted(index, folder.ToString()); } }); }
/// <summary> /// Verifies the and download asset. /// </summary> /// <param name="asset">The asset.</param> /// <param name="expectedFileCount">The expected file count.</param> private void VerifyAndDownloadAssetFileNTimes(IAssetFile assetFile, IAsset asset,int count,int expirationTime, bool cleanOutputDirectory,bool expectFailure = false) { var source = new CancellationTokenSource(); if (expirationTime == 0) { expirationTime = 1000; } IAccessPolicy accessPolicy = _mediaContext.AccessPolicies.Create("SdkDownload", TimeSpan.FromSeconds(expirationTime), AccessPermissions.Read); ILocator locator = _mediaContext.Locators.CreateSasLocator(asset, accessPolicy); var blobTransfer = new BlobTransferClient { NumberOfConcurrentTransfers = _mediaContext.NumberOfConcurrentTransfers, ParallelTransferThreadCount = _mediaContext.ParallelTransferThreadCount }; var downloads = new List<Task>(); var paths = new List<string>(); if (cleanOutputDirectory) { CleanDirectory(_outputDirectory); } try { for (int i = 0; i < count; i++) { foreach (IAssetFile file in _mediaContext.Files.Where(c => c.ParentAssetId == asset.Id)) { string path = Path.Combine(_outputDirectory, Guid.NewGuid().ToString()); paths.Add(path); downloads.Add(assetFile.DownloadAsync(path, blobTransfer, locator, source.Token)); } } Task.WaitAll(downloads.ToArray()); } finally { foreach (var fileDownloaded in paths) { if (File.Exists(fileDownloaded)) { File.Delete(fileDownloaded); } else { if (!expectFailure) { Assert.Fail(); } } } } }