public void WebTransferCache_DeleteFileWhenOpen() { // Verify that an open file will not be deleted and no exception // is thrown. This simulates the case where the website is in the // process of downloading the file. WebTransferCache cache; WebTransferFile file; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromMinutes(10), TimeSpan.FromSeconds(10)); cache.Start(); try { file = cache.GetCommittedFile(Guid.Empty, ".pdf"); cache.GetCommittedFile(file.ID, ".pdf"); // File should still be there using (file.GetStream()) { cache.DeleteFile(file.ID, ".pdf"); cache.GetCommittedFile(file.ID, ".pdf"); // Shouldn't throw an exception } } finally { cache.Stop(); } }
public void WebTransferCache_Download() { // Test the file download scenario. WebTransferCache cache; WebTransferFile transferFile; Guid id; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10)); cache.Start(); try { // Create a file transferFile = cache.GetCommittedFile(Guid.Empty, ".pdf"); id = transferFile.ID; Assert.AreNotEqual(Guid.Empty, transferFile.ID); Assert.IsFalse(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); // Write some data using (var stream = transferFile.GetStream()) { stream.WriteInt32(1001); } // Open an existing file transferFile = cache.GetCommittedFile(id, ".pdf"); Assert.AreEqual(id, transferFile.ID); Assert.IsFalse(transferFile.IsUploading); Assert.IsTrue(File.Exists(transferFile.Path)); Assert.AreEqual(new Uri(new Uri("http://test.com/" + cache.HashIDToSubFolder(transferFile.ID)), transferFile.ID.ToString("D") + ".pdf"), transferFile.Uri); using (var stream = transferFile.GetStream()) { Assert.AreEqual(1001, stream.ReadInt32()); } } finally { cache.Stop(); } }
public void WebTransferCache_DownloadMissing() { // Verify that we can detect a missing download file. WebTransferCache cache; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10)); cache.Start(); try { cache.GetCommittedFile(Guid.NewGuid(), ".pdf"); } catch (Exception e) { // We're expecting a FileNotFoundException. Assert.IsInstanceOfType(e, typeof(FileNotFoundException)); } finally { cache.Stop(); } }
public void WebTransferCache_PurgeDownloadWhileOpen() { // Verify that open download files are not purged even though // they exceed the age limit. WebTransferCache cache; WebTransferFile downloadFile; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromSeconds(10), TimeSpan.FromMinutes(10)); cache.SetSleepTime(TimeSpan.FromSeconds(0.5)); cache.Start(); try { downloadFile = cache.GetCommittedFile(Guid.Empty, ".pdf"); using (downloadFile.GetStream()) { Thread.Sleep(5000); cache.GetCommittedFile(downloadFile.ID, ".pdf"); // File should still be there Thread.Sleep(1000); cache.GetCommittedFile(downloadFile.ID, ".pdf"); // File should still be there Thread.Sleep(7000); cache.GetCommittedFile(downloadFile.ID, ".pdf"); // File should still be there } // The file is closed now. Wait a bit and verify that it gets purged. Thread.Sleep(2000); try { cache.GetCommittedFile(downloadFile.ID, ".pdf"); Assert.Fail("Expected the file to be purged."); } catch (FileNotFoundException) { // Expected } } finally { cache.Stop(); } }
public void WebTransferCache_PurgeUpload() { // Verify that upload files are purged properly as they age. WebTransferCache cache; WebTransferFile downloadFile; WebTransferFile uploadFile; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromMinutes(10), TimeSpan.FromSeconds(10)); cache.SetSleepTime(TimeSpan.FromSeconds(0.5)); cache.Start(); try { downloadFile = cache.GetCommittedFile(Guid.Empty, ".pdf"); uploadFile = cache.GetUploadFile(Guid.Empty, ".pdf"); Thread.Sleep(5000); cache.GetUploadFile(uploadFile.ID, ".pdf"); // File should still be there Thread.Sleep(1000); cache.GetUploadFile(uploadFile.ID, ".pdf"); // File should still be there Thread.Sleep(7000); // File should have been purged try { cache.GetUploadFile(uploadFile.ID, ".pdf"); Assert.Fail("Expected the file to be purged."); } catch (FileNotFoundException) { // Expected } cache.GetCommittedFile(downloadFile.ID, ".pdf"); // File should still be there } finally { cache.Stop(); } }
public void WebTransferCache_DeleteFileByUri() { // Verify that we can delete a cached file via its URI. WebTransferCache cache; WebTransferFile file; Uri uri; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromMinutes(10), TimeSpan.FromSeconds(10)); cache.Start(); try { file = cache.GetCommittedFile(Guid.Empty, ".pdf"); uri = cache.GetCommittedFile(file.ID, ".pdf").Uri; // File should still be there cache.DeleteFile(uri); // File should have been deleted try { cache.GetCommittedFile(file.ID, ".pdf"); Assert.Fail("Expected the file to be deleted."); } catch (FileNotFoundException) { // Expected } // Verify that some error cases don't throw exceptions. cache.DeleteFile(uri); // File already deleted cache.DeleteFile(new Uri("http://test.com")); // No segments in URI cache.DeleteFile(new Uri("http://test.com/hello.htm")); // Invalid GUID cache.DeleteFile(new Uri("http://test.com/6ED48410-A140-434c-B294-0CE81042EEA9")); // No file extension } finally { cache.Stop(); } }
public void WebTransferCache_DeleteFileByID() { // Verify that we can delete a cached file via its ID. WebTransferCache cache; WebTransferFile file; ClearFolder(); cache = new WebTransferCache(new Uri("http://test.com"), folder, "", TimeSpan.FromMinutes(10), TimeSpan.FromSeconds(10)); cache.Start(); try { file = cache.GetCommittedFile(Guid.Empty, ".pdf"); cache.GetCommittedFile(file.ID, ".pdf"); // File should still be there cache.DeleteFile(file.ID, ".pdf"); // File should have been deleted try { cache.GetCommittedFile(file.ID, ".pdf"); Assert.Fail("Expected the file to be deleted."); } catch (FileNotFoundException) { // Expected } } finally { cache.Stop(); } }