public async Task CopyNupkgFileToAsync_ReleasesThrottleOnExceptionAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) using (var throttle = new SemaphoreSlim(initialCount: 1, maxCount: 1)) { var destinationFilePath = Path.Combine(test.TestDirectory.Path, "a"); // Locking the destination file path will cause the copy operation to throw. using (var fileLock = new FileStream( destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None)) { test.Downloader.SetThrottle(throttle); var copyTask = Task.Run(async() => { try { await test.Downloader.CopyNupkgFileToAsync( destinationFilePath, CancellationToken.None); } catch (Exception) { } }); await copyTask; Assert.Equal(1, throttle.CurrentCount); } } }
public void SetThrottle_AcceptsNullThrottle() { using (var test = LocalPackageArchiveDownloaderTest.Create()) { test.Downloader.SetThrottle(throttle: null); } }
public async Task CopyNupkgFileToAsync_RespectsThrottleAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) using (var throttle = new SemaphoreSlim(initialCount: 0, maxCount: 1)) { var destinationFilePath = Path.Combine(test.TestDirectory.Path, "copied.nupkg"); test.Downloader.SetThrottle(throttle); var wasCopied = false; var copyTask = Task.Run(async() => { wasCopied = await test.Downloader.CopyNupkgFileToAsync( destinationFilePath, CancellationToken.None); }); await Task.Delay(100); Assert.False(wasCopied); Assert.False(File.Exists(destinationFilePath)); throttle.Release(); await copyTask; Assert.True(wasCopied); Assert.True(File.Exists(destinationFilePath)); } }
public async Task SetThrottle_AcceptsNullThrottleAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { test.Downloader.SetThrottle(throttle: null); } }
public void Constructor_InitializesProperties() { using (var test = LocalPackageArchiveDownloaderTest.Create()) { Assert.IsType <PackageArchiveReader>(test.Downloader.ContentReader); Assert.IsType <PackageArchiveReader>(test.Downloader.CoreReader); } }
public async Task Constructor_InitializesPropertiesAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { Assert.IsType <PackageArchiveReader>(test.Downloader.ContentReader); Assert.IsType <PackageArchiveReader>(test.Downloader.CoreReader); } }
public async Task Dispose_IsIdempotentAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { test.Downloader.Dispose(); test.Downloader.Dispose(); } }
public void Dispose_IsIdempotent() { using (var test = LocalPackageArchiveDownloaderTest.Create()) { test.Downloader.Dispose(); test.Downloader.Dispose(); } }
public async Task SetExceptionHandler_ThrowsForNullHandlerAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { var exception = Assert.Throws <ArgumentNullException>( () => test.Downloader.SetExceptionHandler(handleExceptionAsync: null)); Assert.Equal("handleExceptionAsync", exception.ParamName); } }
public async Task CopyNupkgFileToAsync_ThrowsIfCancelledAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { await Assert.ThrowsAsync <OperationCanceledException>( () => test.Downloader.CopyNupkgFileToAsync( destinationFilePath: "a", cancellationToken: new CancellationToken(canceled: true))); } }
public async Task GetPackageHashAsync_ThrowsIfCancelledAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { await Assert.ThrowsAsync <OperationCanceledException>( () => test.Downloader.GetPackageHashAsync( hashAlgorithm: "a", cancellationToken: new CancellationToken(canceled: true))); } }
public void ContentReader_ThrowsIfDisposed() { using (var test = LocalPackageArchiveDownloaderTest.Create()) { test.Downloader.Dispose(); var exception = Assert.Throws <ObjectDisposedException>(() => test.Downloader.ContentReader); Assert.Equal(nameof(LocalPackageArchiveDownloader), exception.ObjectName); } }
public async Task CoreReader_ThrowsIfDisposedAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { test.Downloader.Dispose(); var exception = Assert.Throws <ObjectDisposedException>(() => test.Downloader.CoreReader); Assert.Equal(nameof(LocalPackageArchiveDownloader), exception.ObjectName); } }
public async Task CopyNupkgFileToAsync_ThrowsForNullOrEmptyDestinationFilePathAsync(string destinationFilePath) { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { var exception = await Assert.ThrowsAsync <ArgumentException>( () => test.Downloader.CopyNupkgFileToAsync( destinationFilePath, CancellationToken.None)); Assert.Equal("destinationFilePath", exception.ParamName); } }
public async Task GetPackageHashAsync_ThrowsForNullOrEmptyHashAlgorithmAsync(string hashAlgorithm) { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { var exception = await Assert.ThrowsAsync <ArgumentException>( () => test.Downloader.GetPackageHashAsync( hashAlgorithm, CancellationToken.None)); Assert.Equal("hashAlgorithm", exception.ParamName); } }
public async Task GetPackageHashAsync_ReturnsPackageHashAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { var hashAlgorithm = "SHA512"; var expectedPackageHash = CalculatePackageHash(test.SourcePackageFile.FullName, hashAlgorithm); var actualPackageHash = await test.Downloader.GetPackageHashAsync( hashAlgorithm, CancellationToken.None); Assert.Equal(expectedPackageHash, actualPackageHash); } }
public async Task GetPackageHashAsync_ThrowsIfDisposedAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { test.Downloader.Dispose(); var exception = await Assert.ThrowsAsync <ObjectDisposedException>( () => test.Downloader.GetPackageHashAsync( hashAlgorithm: "SHA512", cancellationToken: CancellationToken.None)); Assert.Equal(nameof(LocalPackageArchiveDownloader), exception.ObjectName); } }
public async Task CopyNupkgFileToAsync_ThrowsIfDisposedAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { test.Downloader.Dispose(); var exception = await Assert.ThrowsAsync <ObjectDisposedException>( () => test.Downloader.CopyNupkgFileToAsync( destinationFilePath: "a", cancellationToken: CancellationToken.None)); Assert.Equal(nameof(LocalPackageArchiveDownloader), exception.ObjectName); } }
public async Task CopyNupkgFileToAsync_ReturnsTrueOnSuccessAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { var destinationFilePath = Path.Combine(test.TestDirectory.Path, "copied.nupkg"); var wasCopied = await test.Downloader.CopyNupkgFileToAsync( destinationFilePath, CancellationToken.None); Assert.True(wasCopied); var sourceBytes = File.ReadAllBytes(test.SourcePackageFile.FullName); var destinationBytes = File.ReadAllBytes(destinationFilePath); Assert.Equal(sourceBytes, destinationBytes); } }
public async Task CopyNupkgFileToAsync_ReturnsFalseIfExceptionHandledAsync() { using (var test = await LocalPackageArchiveDownloaderTest.CreateAsync()) { var destinationFilePath = Path.Combine(test.TestDirectory.Path, "a"); // Locking the destination file path will cause the copy operation to throw. using (var fileLock = new FileStream( destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None)) { test.Downloader.SetExceptionHandler(exception => Task.FromResult(true)); var wasCopied = await test.Downloader.CopyNupkgFileToAsync( destinationFilePath, CancellationToken.None); Assert.False(wasCopied); } } }