18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 22:44:59 +01:00
Files
maszyna/tests/unit/test_physicslog.cpp
maj00r 7547e6bf07 fix(physicslog): a log whose file can't be written keeps its lines and header, and the writer is stopped once on every way out
A failed write put its lines back, ahead of those since, so a file held by
another program for a while gets its header and every line once it can be
written; up to 4 MB kept per log. Stopping is joined once, and not from the
writer's own thread. main stops the logs after an error it caught too, and the
test watchdog flushes with a time limit, so a disk that doesn't answer doesn't
keep the run from ending.
2026-11-15 23:32:38 +01:00

190 lines
6.8 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "utilities/physicslog.h"
#include <chrono>
#include <filesystem>
#include <format>
#include <fstream>
#include <sstream>
#include <string>
#include <system_error>
#include <thread>
// the physics logs are kept and written by a thread of their own every so often, each file opened only to be written to
namespace {
// a folder of its own for the logs of a test, in the build folder of the tests, removed with the object. the writer
// makes it itself
class log_folder {
public:
explicit log_folder(std::string const &Name) : m_path{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / std::format("eu07_physicslog_test_{}", Name)} {
std::filesystem::remove_all(m_path);
}
~log_folder() {
std::error_code error;
std::filesystem::remove_all(m_path, error);
}
log_folder(log_folder const &) = delete;
log_folder &operator=(log_folder const &) = delete;
std::filesystem::path const &path() const { return m_path; }
// the log as it stands, read in text mode as it is written: what was given to it
std::string read(std::string const &Name) const {
std::ifstream file{m_path / (Name + ".dat")};
if (false == file.is_open())
return {};
std::ostringstream text;
text << file.rdbuf();
return text.str();
}
// waits up to five seconds for the log to read Text
bool reads(std::string const &Name, std::string const &Text) const {
auto const until{std::chrono::steady_clock::now() + std::chrono::seconds{5}};
while (read(Name) != Text && std::chrono::steady_clock::now() < until)
std::this_thread::sleep_for(std::chrono::milliseconds{10});
return read(Name) == Text;
}
private:
std::filesystem::path m_path;
};
constexpr auto header{"Time[s] Velocity[m/s]\r\n"};
} // namespace
TEST_CASE("physicslog: a log begun is emptied and given its header, and its lines follow it at each writing") {
log_folder folder{"begin"};
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
logs.begin("EU07-424", header);
logs.append("EU07-424", "0.0500 0.0000\r\n");
CHECK(folder.read("EU07-424").empty());
logs.flush();
CHECK(folder.read("EU07-424") == std::string{header} + "0.0500 0.0000\r\n");
logs.append("EU07-424", "0.1000 1.5000\r\n");
logs.flush();
CHECK(folder.read("EU07-424") == std::string{header} + "0.0500 0.0000\r\n0.1000 1.5000\r\n");
// begun again: the lines not written yet are dropped along with those written
logs.append("EU07-424", "0.1500 3.0000\r\n");
logs.begin("EU07-424", header);
logs.flush();
CHECK(folder.read("EU07-424") == header);
}
TEST_CASE("physicslog: no file is kept open between the writings, and a log not begun is added to as it stands") {
log_folder folder{"closed"};
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
logs.begin("SM42-001", header);
logs.flush();
// Windows doesn't let a file open in a program be removed. Linux does, so there it checks nothing; the log is
// written again below anyway
std::error_code error;
CHECK(std::filesystem::remove(folder.path() / "SM42-001.dat", error));
CHECK_FALSE(error);
logs.append("SM42-001", "0.0500 0.0000\r\n");
logs.flush();
CHECK(folder.read("SM42-001") == "0.0500 0.0000\r\n");
}
TEST_CASE("physicslog: the thread writes the logs by itself") {
log_folder folder{"thread"};
physicslog_writer logs{folder.path(), std::chrono::milliseconds{20}};
logs.begin("ET22-001", header);
logs.append("ET22-001", "0.0500 0.0000\r\n");
CHECK(folder.reads("ET22-001", std::string{header} + "0.0500 0.0000\r\n"));
logs.append("ET22-001", "0.1000 0.0000\r\n");
CHECK(folder.reads("ET22-001", std::string{header} + "0.0500 0.0000\r\n0.1000 0.0000\r\n"));
}
TEST_CASE("physicslog: stopping writes what is kept at once, and what comes after waits for a writing") {
log_folder folder{"stop"};
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
logs.begin("EN57-1001", header);
logs.append("EN57-1001", "0.0500 0.0000\r\n");
logs.stop();
CHECK(folder.read("EN57-1001") == std::string{header} + "0.0500 0.0000\r\n");
logs.append("EN57-1001", "0.1000 0.0000\r\n");
std::this_thread::sleep_for(std::chrono::milliseconds{50});
CHECK(folder.read("EN57-1001") == std::string{header} + "0.0500 0.0000\r\n");
logs.flush();
CHECK(folder.read("EN57-1001") == std::string{header} + "0.0500 0.0000\r\n0.1000 0.0000\r\n");
}
TEST_CASE("physicslog: the lines of a log whose file can't be written are kept, with its header, for the next try") {
log_folder folder{"failed"};
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
// a folder in the way of the file: it can't be opened, as one held by another program
std::filesystem::create_directories(folder.path() / "EU07-005.dat");
logs.begin("EU07-005", header);
logs.append("EU07-005", "0.0500 0.0000\r\n");
logs.flush();
logs.append("EU07-005", "0.1000 0.5000\r\n");
logs.flush();
CHECK(std::filesystem::is_directory(folder.path() / "EU07-005.dat"));
std::filesystem::remove(folder.path() / "EU07-005.dat");
logs.append("EU07-005", "0.1500 1.0000\r\n");
logs.flush();
CHECK(folder.read("EU07-005") == std::string{header} + "0.0500 0.0000\r\n0.1000 0.5000\r\n0.1500 1.0000\r\n");
}
TEST_CASE("physicslog: a log begun anew while its file can't be written drops what was kept for it") {
log_folder folder{"failedbegun"};
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
std::filesystem::create_directories(folder.path() / "SU45-001.dat");
logs.begin("SU45-001", header);
logs.append("SU45-001", "0.0500 0.0000\r\n");
logs.flush();
std::filesystem::remove(folder.path() / "SU45-001.dat");
logs.begin("SU45-001", header);
logs.append("SU45-001", "0.0500 2.0000\r\n");
logs.flush();
CHECK(folder.read("SU45-001") == std::string{header} + "0.0500 2.0000\r\n");
}
TEST_CASE("physicslog: stopping twice, from two threads at once, writes once and ends") {
log_folder folder{"stoptwice"};
physicslog_writer logs{folder.path(), std::chrono::milliseconds{20}};
logs.begin("EP09-001", header);
logs.append("EP09-001", "0.0500 0.0000\r\n");
{
std::jthread other{[&logs] { logs.stop(); }};
logs.stop();
}
CHECK(folder.read("EP09-001") == std::string{header} + "0.0500 0.0000\r\n");
}
TEST_CASE("physicslog: a flush with a time limit writes, as flush does") {
log_folder folder{"flushfor"};
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
logs.begin("ET41-001", header);
logs.append("ET41-001", "0.0500 0.0000\r\n");
CHECK(logs.flush_for(std::chrono::seconds{1}));
CHECK(folder.read("ET41-001") == std::string{header} + "0.0500 0.0000\r\n");
}
TEST_CASE("physicslog: what is kept is written when the writer goes") {
log_folder folder{"end"};
{
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
logs.begin("ST44-001", header);
logs.append("ST44-001", "0.0500 0.0000\r\n");
}
CHECK(folder.read("ST44-001") == std::string{header} + "0.0500 0.0000\r\n");
}