collector/test.cpp

67 lines
2.2 KiB
C++
Raw Normal View History

2022-10-06 05:10:49 +02:00
/**
* @file test.cpp
* The unit test executable
*
* @author Tobias Schmidl
* @copyright Copyright © 2022, Tobias Schmidl
* SPDX-License-Identifier: MIT
*/
2022-10-06 10:26:45 +02:00
#include "collector.hpp"
2022-10-06 05:10:49 +02:00
#include "monitor.hpp"
2022-09-29 20:31:11 +02:00
#include "version.hpp"
#define BOOST_TEST_MODULE Collector test
#include <boost/test/unit_test.hpp>
2022-10-06 05:10:49 +02:00
#include <chrono>
#include <fstream>
#include <future>
#include <thread>
using namespace collector::monitor;
using namespace collector::collector;
using namespace std::chrono_literals;
2022-09-29 20:31:11 +02:00
2022-10-06 05:10:49 +02:00
boost::asio::io_service service;
2022-09-29 20:31:11 +02:00
2022-10-06 05:10:49 +02:00
BOOST_AUTO_TEST_CASE(monitor_test)
2022-09-29 20:31:11 +02:00
{
2022-10-06 05:10:49 +02:00
BOOST_TEST_MESSAGE("Test version " << VERSION);
const std::filesystem::path test_file{ "test_file" };
std::filesystem::remove(test_file);
const auto create_file_result = std::async([&test_file]() {
std::this_thread::sleep_for(2s);
std::ofstream{ test_file };
});
Monitor monitor(service, std::filesystem::current_path());
const auto& watch_result =
monitor.watch(std::regex(test_file.generic_string()),
boost::asio::dir_monitor_event::event_type::added);
BOOST_TEST(std::filesystem::equivalent(test_file,
watch_result.path.generic_string()));
std::filesystem::remove(test_file);
2022-09-29 20:31:11 +02:00
}
2022-10-06 10:26:45 +02:00
BOOST_AUTO_TEST_CASE(collector_test)
{
BOOST_TEST_MESSAGE("Test version " << VERSION);
const std::filesystem::path target_file{ "test.tar" },
test_input_dir{ "test_input" };
std::filesystem::remove(target_file);
std::filesystem::remove_all(test_input_dir);
std::filesystem::create_directory(test_input_dir);
const auto create_file_result = std::async([&test_input_dir]() {
std::this_thread::sleep_for(5s);
std::ofstream test_file{ test_input_dir / "should_be_in_it" };
test_file << "Hello World." << std::endl;
test_file.flush();
std::ofstream{ test_input_dir / "Core.TEST.00.lz4" };
std::ofstream{ test_input_dir / "might_not_be_in_it" };
});
Collector collector(service, test_input_dir, target_file);
BOOST_TEST(collector());
BOOST_TEST(std::filesystem::is_regular_file(target_file));
std::filesystem::remove(target_file);
std::filesystem::remove_all(test_input_dir);
}