// This test relies on TestArchiveOpenerReadAndWrite succeeding void TestArchiveOpenerExceptions() throw(Exception) { OutputFileHandler handler(mArchiveDir, false); handler.SetArchiveDirectory(); FileFinder archive_dir_finder(mArchiveDir, RelativeTo::ChasteTestOutput); std::string archive_base_name = "archive_opener.arch"; // Remove the process-specific archive for this process FileFinder(ArchiveLocationInfo::GetProcessUniqueFilePath(archive_base_name)).Remove(); TS_ASSERT_THROWS_CONTAINS(InputArchiveOpener archive_opener_in(archive_dir_finder, archive_base_name), "Cannot load secondary archive file: "); PetscTools::Barrier("TestArchiveOpenerExceptions-1"); // Remove the main archive if (PetscTools::AmMaster()) { ABORT_IF_THROWS(handler.FindFile(archive_base_name).Remove()); } PetscTools::Barrier("TestArchiveOpenerExceptions-2"); TS_ASSERT_THROWS_CONTAINS(InputArchiveOpener archive_opener_in(archive_dir_finder, archive_base_name), "Cannot load main archive file: "); // Remove write permissions on the archive dir //Note: changing *directory* permissions and other attributes does not work on Windows //See http://support.microsoft.com/kb/326549 #ifndef _MSC_VER if (PetscTools::AmMaster()) { chmod(handler.GetOutputDirectoryFullPath().c_str(), CHASTE_READONLY); } PetscTools::Barrier("TestArchiveOpenerExceptions-3"); /* * Now neither the master nor the slaves can write to their output files. * This avoids hitting a PetscBarrier() in the ~ArchiveOpener() because they * all throw an error first. * * If this test starts hanging it is because these TS_ASSERT_THROWS_CONTAINS * are not being thrown (rather than a real parallel calling problem). */ if (PetscTools::AmMaster()) { TS_ASSERT_THROWS_CONTAINS(OutputArchiveOpener archive_opener_out(archive_dir_finder, archive_base_name), "Failed to open main archive file for writing: "); } else { TS_ASSERT_THROWS_CONTAINS(OutputArchiveOpener archive_opener_out(archive_dir_finder, archive_base_name), "Failed to open secondary archive file for writing: "); } PetscTools::Barrier("TestArchiveOpenerExceptions-4"); if (PetscTools::AmMaster()) { // Restore permissions on the folder before allowing processes to continue. chmod(handler.GetOutputDirectoryFullPath().c_str(), CHASTE_READ_WRITE_EXECUTE); } #endif // _MSC_VER PetscTools::Barrier("TestArchiveOpenerExceptions-5"); }
void TestQueueRemovesAndCreatesDirectories() throw (Exception) { FileFinder checkpoints("checkpoints2", RelativeTo::ChasteTestOutput); // Remove directory in case it was there from previous executions. PetscTools::Barrier("TestQueueRemovesAndCreatesDirectories-0"); if (PetscTools::AmMaster()) { ABORT_IF_THROWS(checkpoints.Remove()); } PetscTools::Barrier("TestQueueRemovesAndCreatesDirectories-1"); TS_ASSERT(!checkpoints.Exists()); PetscTools::Barrier("TestQueueRemovesAndCreatesDirectories-2"); OutputDirectoryFifoQueue fifo_queue("checkpoints2", 2); TS_ASSERT(checkpoints.IsDir()); fifo_queue.CreateNextDir("0.1"); FileFinder dir1("0.1", checkpoints); TS_ASSERT(dir1.IsDir()); fifo_queue.CreateNextDir("0.2"); FileFinder dir2("0.2", checkpoints); TS_ASSERT(dir2.IsDir()); TS_ASSERT(dir1.IsDir()); PetscTools::Barrier("TestQueueRemovesAndCreatesDirectories-3"); fifo_queue.CreateNextDir("0.3"); FileFinder dir3("0.3", checkpoints); TS_ASSERT(dir3.IsDir()); TS_ASSERT(dir2.IsDir()); TS_ASSERT(!dir1.Exists()); PetscTools::Barrier("TestQueueRemovesAndCreatesDirectories-4"); fifo_queue.CreateNextDir("0.4"); FileFinder dir4("0.4", checkpoints); TS_ASSERT(dir4.IsDir()); TS_ASSERT(dir3.IsDir()); TS_ASSERT(!dir2.Exists()); TS_ASSERT(!dir1.Exists()); }
std::string OutputDirectoryFifoQueue::CreateNextDir(const std::string& rSubdirectoryName) { std::string subdirectory_full_name = mBaseDirectory + "/" + rSubdirectoryName; if (mQueue.size() == mQueueMaxSize) { FileFinder dir_to_remove(mBaseDirectory + "/" + mQueue.front(), RelativeTo::ChasteTestOutput); if (PetscTools::AmMaster()) { ABORT_IF_THROWS(dir_to_remove.Remove()); } PetscTools::Barrier("OutputDirectoryFifoQueue::CreateNextDir"); mQueue.pop(); } mQueue.push(rSubdirectoryName); OutputFileHandler handler(subdirectory_full_name); assert(mQueue.size() <= mQueueMaxSize); return subdirectory_full_name; }
void TestWeCanOnlyDeleteFoldersWeHaveMadeOurselves() throw(Exception, std::exception) { std::string test_folder = "cannot_delete_me"; if (PetscTools::AmMaster()) { ABORT_IF_THROWS(fs::create_directories(OutputFileHandler::GetChasteTestOutputDirectory() + test_folder)); } // Wait until directory has been created, and check it exists PetscTools::Barrier("TestWeCanOnlyDeleteFoldersWeHaveMadeOurselves-1"); FileFinder cannot_delete(test_folder, RelativeTo::ChasteTestOutput); TS_ASSERT(cannot_delete.IsDir()); // Try to use it as an output folder TS_ASSERT_THROWS_CONTAINS(OutputFileHandler bad_handler(test_folder), "because signature file \".chaste_deletable_folder\" is not present"); // Tidy up if (PetscTools::AmMaster()) { TS_ASSERT(cannot_delete.Exists()); cannot_delete.DangerousRemove(); TS_ASSERT(!cannot_delete.Exists()); } // Now create a folder the proper way test_folder = "can_delete_me"; OutputFileHandler handler(test_folder); out_stream p_file_stream = handler.OpenOutputFile("test_file"); p_file_stream->close(); // Windows does not like deleting open files // Test file is present FileFinder test_file = handler.FindFile("test_file"); TS_ASSERT(test_file.Exists()); PetscTools::Barrier("TestWeCanOnlyDeleteFoldersWeHaveMadeOurselves-2"); OutputFileHandler handler2(test_folder, false /* don't clean */); // Test file is still present TS_ASSERT(test_file.Exists()); PetscTools::Barrier("TestWeCanOnlyDeleteFoldersWeHaveMadeOurselves-3"); OutputFileHandler handler3(test_folder, true /* do clean */); // Test file is deleted TS_ASSERT(!test_file.Exists()); PetscTools::Barrier("TestWeCanOnlyDeleteFoldersWeHaveMadeOurselves-4"); // Check we can delete the test_folder too if (PetscTools::AmMaster()) { FileFinder folder = handler.FindFile(""); TS_ASSERT(folder.Exists()); folder.Remove(); TS_ASSERT(!folder.Exists()); } // Test we can make a directory of folders and delete them all OutputFileHandler handler4("what_about_me/and_me/and_me/and_da_da_da", true); // Check we have made a subdirectory FileFinder sub_folder("what_about_me/and_me", RelativeTo::ChasteTestOutput); TS_ASSERT(sub_folder.IsDir()); PetscTools::Barrier("TestWeCanOnlyDeleteFoldersWeHaveMadeOurselves-5"); OutputFileHandler handler5("what_about_me", true); // Check we have wiped the sub-directories TS_ASSERT(!sub_folder.Exists()); PetscTools::Barrier("TestWeCanOnlyDeleteFoldersWeHaveMadeOurselves-6"); // Check we can delete the main directory too if (PetscTools::AmMaster()) { FileFinder folder = handler5.FindFile(""); TS_ASSERT(folder.Exists()); folder.Remove(); TS_ASSERT(!folder.Exists()); } }