mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-19 22:44:59 +01:00
The writer's thread called flush() with nothing around it, so an exception leaving write() ended the process with std::terminate, with no message. One that happens: the file of a log was made a std::filesystem::path of the vehicle's name, which MSVC converts in the code page of the system and throws for bytes not of it; on a Windows with "Use Unicode UTF-8 for worldwide language support" a vehicle named in 1250, as the sceneries have them (0xB3 for ł), isn't UTF-8. - the file of a log is made of the name with MultiByteToWideChar without MB_ERR_INVALID_CHARS: the same file as before for every name of the code page, U+FFFD for the bytes not of it instead of a throw. Elsewhere the bytes are the name of the file as they were - a log whose writing throws for any other reason (memory) is kept for the next try as one whose file can't be written, and the other logs are written still - the thread, flush_for() and stop() write through flush_safely(), which lets nothing out: what was taken for a writing failing as a whole is lost and told in the log, the next writing goes on - the messages name the file by the bytes of the name, not by path::string(), which converts back Tests: a log which can't be written doesn't keep the others from theirs; a vehicle named in 1250 has its log, read back by physicslog_writer::file(). Reported in the review of #385. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
218 lines
8.1 KiB
C++
218 lines
8.1 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 { return read_file(m_path / (Name + ".dat")); }
|
|
static std::string read_file(std::filesystem::path const &File) {
|
|
std::ifstream file{File};
|
|
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: a log whose file can't be written doesn't keep the others from theirs") {
|
|
log_folder folder{"failedother"};
|
|
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
|
|
|
|
std::filesystem::create_directories(folder.path() / "EU07-006.dat");
|
|
logs.begin("EU07-006", header);
|
|
logs.append("EU07-006", "0.0500 0.0000\r\n");
|
|
logs.begin("EU07-007", header);
|
|
logs.append("EU07-007", "0.0500 1.0000\r\n");
|
|
logs.flush();
|
|
CHECK(std::filesystem::is_directory(folder.path() / "EU07-006.dat"));
|
|
CHECK(folder.read("EU07-007") == std::string{header} + "0.0500 1.0000\r\n");
|
|
}
|
|
|
|
TEST_CASE("physicslog: a vehicle named in the code page of its scenery has its log, whatever the code page of the system") {
|
|
log_folder folder{"codepage"};
|
|
physicslog_writer logs{folder.path(), std::chrono::hours{1}};
|
|
|
|
// "SM42-Łódź" in 1250, as the sceneries have it: not UTF-8, which a Windows set to it would have thrown on
|
|
std::string const name{"SM42-\xA3\xF3" "d\x9F"}; // apart, or the d would be read as a digit of the \xF3
|
|
logs.begin(name, header);
|
|
logs.append(name, "0.0500 0.0000\r\n");
|
|
logs.flush();
|
|
CHECK(logs.file(name).parent_path() == folder.path());
|
|
CHECK(log_folder::read_file(logs.file(name)) == std::string{header} + "0.0500 0.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");
|
|
}
|