diff --git a/nano/core_test/core_test_main.cc b/nano/core_test/core_test_main.cc index a484b84f..06951447 100644 --- a/nano/core_test/core_test_main.cc +++ b/nano/core_test/core_test_main.cc @@ -1,4 +1,5 @@ #include "gtest/gtest.h" +#include namespace nano { void force_nano_test_network (); @@ -8,5 +9,7 @@ GTEST_API_ int main (int argc, char ** argv) printf ("Running main() from core_test_main.cc\n"); nano::force_nano_test_network (); testing::InitGoogleTest (&argc, argv); - return RUN_ALL_TESTS (); + auto res = RUN_ALL_TESTS (); + nano::cleanp_test_directories_on_exit (); + return res; } diff --git a/nano/node/logging.cpp b/nano/node/logging.cpp index 1f873600..7b153720 100644 --- a/nano/node/logging.cpp +++ b/nano/node/logging.cpp @@ -5,9 +5,11 @@ #include #include +boost::shared_ptr> nano::logging::file_sink; +std::atomic_flag nano::logging::logging_already_added{ ATOMIC_FLAG_INIT }; + void nano::logging::init (boost::filesystem::path const & application_path_a) { - static std::atomic_flag logging_already_added = ATOMIC_FLAG_INIT; if (!logging_already_added.test_and_set ()) { boost::log::add_common_attributes (); @@ -16,7 +18,16 @@ void nano::logging::init (boost::filesystem::path const & application_path_a) boost::log::add_console_log (std::cerr, boost::log::keywords::format = "[%TimeStamp%]: %Message%"); } auto path = application_path_a / "log"; - boost::log::add_file_log (boost::log::keywords::target = path, boost::log::keywords::file_name = path / "log_%Y-%m-%d_%H-%M-%S.%N.log", boost::log::keywords::rotation_size = rotation_size, boost::log::keywords::auto_flush = flush, boost::log::keywords::scan_method = boost::log::sinks::file::scan_method::scan_matching, boost::log::keywords::max_size = max_size, boost::log::keywords::format = "[%TimeStamp%]: %Message%"); + file_sink = boost::log::add_file_log (boost::log::keywords::target = path, boost::log::keywords::file_name = path / "log_%Y-%m-%d_%H-%M-%S.%N.log", boost::log::keywords::rotation_size = rotation_size, boost::log::keywords::auto_flush = flush, boost::log::keywords::scan_method = boost::log::sinks::file::scan_method::scan_matching, boost::log::keywords::max_size = max_size, boost::log::keywords::format = "[%TimeStamp%]: %Message%"); + } +} + +void nano::logging::release_file_sink () +{ + if (logging_already_added.test_and_set ()) + { + boost::log::core::get ()->remove_sink (nano::logging::file_sink); + nano::logging::file_sink.reset (); } } diff --git a/nano/node/logging.hpp b/nano/node/logging.hpp index 5aca027e..ef042964 100644 --- a/nano/node/logging.hpp +++ b/nano/node/logging.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -64,9 +65,14 @@ public: uintmax_t rotation_size{ 4 * 1024 * 1024 }; std::chrono::milliseconds min_time_between_log_output{ 5 }; nano::logger_mt logger{ min_time_between_log_output }; + static void release_file_sink (); int json_version () const { return 7; } + +private: + static boost::shared_ptr> file_sink; + static std::atomic_flag logging_already_added; }; } diff --git a/nano/node/testing.cpp b/nano/node/testing.cpp index 3032efbb..5cb969bc 100644 --- a/nano/node/testing.cpp +++ b/nano/node/testing.cpp @@ -77,6 +77,11 @@ nano::system::~system () i->stop (); } + // clang-format off + // Windows cannot remove the log and data files while they are still owned by this process. + // They will be removed later + // clang-format on +#ifndef _WIN32 // Clean up tmp directories created by the tests. Since it's sometimes useful to // see log files after test failures, an environment variable is supported to // retain the files. @@ -84,6 +89,7 @@ nano::system::~system () { nano::remove_temporary_directories (); } +#endif } std::shared_ptr nano::system::wallet (size_t index_a) diff --git a/nano/qt_test/entry.cpp b/nano/qt_test/entry.cpp index 823a3f60..3fcbf587 100644 --- a/nano/qt_test/entry.cpp +++ b/nano/qt_test/entry.cpp @@ -1,6 +1,6 @@ #include #include - +#include QApplication * test_application = nullptr; namespace nano { @@ -13,5 +13,7 @@ int main (int argc, char ** argv) QApplication application (argc, argv); test_application = &application; testing::InitGoogleTest (&argc, argv); - return RUN_ALL_TESTS (); + auto res = RUN_ALL_TESTS (); + nano::cleanp_test_directories_on_exit (); + return res; } diff --git a/nano/rpc_test/CMakeLists.txt b/nano/rpc_test/CMakeLists.txt index b13a227d..5a18de95 100644 --- a/nano/rpc_test/CMakeLists.txt +++ b/nano/rpc_test/CMakeLists.txt @@ -2,7 +2,7 @@ add_executable (rpc_test entry.cpp rpc.cpp) -target_link_libraries(rpc_test gtest gtest_main rpc) +target_link_libraries(rpc_test gtest rpc) target_compile_definitions(rpc_test PUBLIC diff --git a/nano/rpc_test/entry.cpp b/nano/rpc_test/entry.cpp index d28e5051..ad34dda4 100644 --- a/nano/rpc_test/entry.cpp +++ b/nano/rpc_test/entry.cpp @@ -1,5 +1,5 @@ #include - +#include namespace nano { void force_nano_test_network (); @@ -9,5 +9,7 @@ int main (int argc, char ** argv) { nano::force_nano_test_network (); testing::InitGoogleTest (&argc, argv); - return RUN_ALL_TESTS (); + auto res = RUN_ALL_TESTS (); + nano::cleanp_test_directories_on_exit (); + return res; } diff --git a/nano/secure/utility.cpp b/nano/secure/utility.cpp index 3b0898b1..d4771c72 100644 --- a/nano/secure/utility.cpp +++ b/nano/secure/utility.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -88,7 +89,7 @@ boost::filesystem::path nano::unique_path () return result; } -std::vector nano::remove_temporary_directories () +void nano::remove_temporary_directories () { for (auto & path : all_unique_paths) { @@ -108,5 +109,17 @@ std::vector nano::remove_temporary_directories () std::cerr << "Could not remove temporary lock file: " << ec.message () << std::endl; } } - return all_unique_paths; +} + +void nano::cleanp_test_directories_on_exit () +{ + // Makes sure everything is cleaned up + nano::logging::release_file_sink (); + // Clean up tmp directories created by the tests. Since it's sometimes useful to + // see log files after test failures, an environment variable is supported to + // retain the files. + if (std::getenv ("TEST_KEEP_TMPDIRS") == nullptr) + { + nano::remove_temporary_directories (); + } } diff --git a/nano/secure/utility.hpp b/nano/secure/utility.hpp index a85c8aa7..1daccfff 100644 --- a/nano/secure/utility.hpp +++ b/nano/secure/utility.hpp @@ -27,6 +27,7 @@ bool migrate_working_path (std::string &); // Get a unique path within the home directory, used for testing. // Any directories created at this location will be removed when a test finishes. boost::filesystem::path unique_path (); -// Remove all unique tmp directories created by the process. The list of unique paths are returned. -std::vector remove_temporary_directories (); +// Remove all unique tmp directories created by the process +void remove_temporary_directories (); +void cleanp_test_directories_on_exit (); } diff --git a/nano/slow_test/CMakeLists.txt b/nano/slow_test/CMakeLists.txt index e96af256..0606eb1a 100644 --- a/nano/slow_test/CMakeLists.txt +++ b/nano/slow_test/CMakeLists.txt @@ -1,4 +1,5 @@ add_executable (slow_test + entry.cpp node.cpp) -target_link_libraries (slow_test node secure gtest_main gtest libminiupnpc-static Boost::boost) +target_link_libraries (slow_test node secure gtest libminiupnpc-static Boost::boost) diff --git a/nano/slow_test/entry.cpp b/nano/slow_test/entry.cpp new file mode 100644 index 00000000..ad34dda4 --- /dev/null +++ b/nano/slow_test/entry.cpp @@ -0,0 +1,15 @@ +#include +#include +namespace nano +{ +void force_nano_test_network (); +} + +int main (int argc, char ** argv) +{ + nano::force_nano_test_network (); + testing::InitGoogleTest (&argc, argv); + auto res = RUN_ALL_TESTS (); + nano::cleanp_test_directories_on_exit (); + return res; +}