mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
fix(odometer): a save lost to another simulation's save is written again (#354)
Two simulations sharing eu07_odometers.txt could lose a save: A reads the file, B renames its file in, A renames its own over it. B had already cleared its modified flag, so its next save returned at once and its states stayed lost (known limit from the review of #283). A write now leaves the store unconfirmed. The next save with nothing recorded reads and merges the file as before, and writes again only when the file lacks a state of the process, the same or newer by the merge rule (compared as written, rounded to the file's digits). Otherwise the store is confirmed and later saves return at once, as before. A process holding an older state takes the file's one, so two processes settle instead of rewriting each other.
This commit is contained in:
@@ -355,13 +355,146 @@ TEST_CASE("odometer: two simulations saving one file keep each other's states")
|
|||||||
}
|
}
|
||||||
CHECK(files == 1);
|
CHECK(files == 1);
|
||||||
}
|
}
|
||||||
SUBCASE("a store with nothing recorded since doesn't write") {
|
SUBCASE("a store with nothing recorded since doesn't write, once the file showed what it saved") {
|
||||||
|
REQUIRE(odometer::save(second, path));
|
||||||
write_file(path, "1 replaced by hand\n");
|
write_file(path, "1 replaced by hand\n");
|
||||||
REQUIRE(odometer::save(second, path));
|
REQUIRE(odometer::save(second, path));
|
||||||
CHECK(load().size() == 1);
|
CHECK(load().size() == 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
odometer::store load_store(std::filesystem::path const &Path) {
|
||||||
|
odometer::store store;
|
||||||
|
std::ifstream input{Path, std::ios::binary};
|
||||||
|
store.read(input);
|
||||||
|
return store;
|
||||||
|
}
|
||||||
|
|
||||||
|
// a comment line added to the file; a save that writes the file drops it
|
||||||
|
void mark(std::filesystem::path const &Path) {
|
||||||
|
std::ofstream{Path, std::ios::binary | std::ios::app} << "# not written since\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool marked(std::filesystem::path const &Path) {
|
||||||
|
std::ifstream input{Path, std::ios::binary};
|
||||||
|
std::stringstream content;
|
||||||
|
content << input.rdbuf();
|
||||||
|
return content.str().find("# not written since") != std::string::npos;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the save of Late reads the file before the save of Early writes it, and puts its own file in place after: Late
|
||||||
|
// writes the file as it was before Early's save, Early's states are gone from it
|
||||||
|
void interleave(odometer::store &Early, odometer::store &Late, std::filesystem::path const &Path) {
|
||||||
|
auto const view{Path.parent_path() / "late_view" / Path.filename()};
|
||||||
|
std::filesystem::create_directories(view.parent_path());
|
||||||
|
std::filesystem::copy_file(Path, view, std::filesystem::copy_options::overwrite_existing);
|
||||||
|
REQUIRE(odometer::save(Early, Path));
|
||||||
|
REQUIRE(odometer::save(Late, view));
|
||||||
|
std::filesystem::rename(view, Path);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST_CASE("odometer: a save another simulation's save put its file over is written again") {
|
||||||
|
scratch_directory directory{"odometer_lost_save"};
|
||||||
|
auto const path{directory.path / "eu07_odometers.txt"};
|
||||||
|
write_file(path, "1000 shared\n");
|
||||||
|
auto first{load_store(path)};
|
||||||
|
auto second{load_store(path)};
|
||||||
|
first.record("first only", run(70.0, 1000.0));
|
||||||
|
second.record("second only", run(80.0, 1000.0));
|
||||||
|
|
||||||
|
interleave(second, first, path);
|
||||||
|
// the race of the two saves: the second one's state is gone from the file
|
||||||
|
REQUIRE_FALSE(load_store(path).find("second only").has_value());
|
||||||
|
REQUIRE(load_store(path).find("first only").has_value());
|
||||||
|
|
||||||
|
// the second one's next save, nothing recorded since, finds the file without its state and writes it again
|
||||||
|
CHECK_FALSE(second.modified());
|
||||||
|
REQUIRE(odometer::save(second, path));
|
||||||
|
auto const saved{load_store(path)};
|
||||||
|
CHECK(saved.size() == 3);
|
||||||
|
CHECK(saved.find("second only").value_or(0.0) == doctest::Approx(81.0));
|
||||||
|
CHECK(saved.find("first only").value_or(0.0) == doctest::Approx(71.0));
|
||||||
|
CHECK(saved.find("shared").value_or(0.0) == 1000.0);
|
||||||
|
|
||||||
|
// both find their states in the file now: their next saves don't write it
|
||||||
|
mark(path);
|
||||||
|
for (auto round{0}; round < 3; ++round) {
|
||||||
|
REQUIRE(odometer::save(first, path));
|
||||||
|
REQUIRE(odometer::save(second, path));
|
||||||
|
}
|
||||||
|
CHECK(marked(path));
|
||||||
|
CHECK(load_store(path).size() == 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("odometer: two simulations with different states of a vehicle settle on the newer one") {
|
||||||
|
scratch_directory directory{"odometer_lost_save_conflict"};
|
||||||
|
auto const path{directory.path / "eu07_odometers.txt"};
|
||||||
|
write_file(path, "1000 shared\n~20000 guessed by both\n");
|
||||||
|
auto first{load_store(path)};
|
||||||
|
auto second{load_store(path)};
|
||||||
|
// the first one ran the shared vehicle further, and the made up one further from its made up state; the second
|
||||||
|
// one has a known state of the made up one, from a cab's counter
|
||||||
|
first.record("shared", run(1000.0, 10000.0));
|
||||||
|
second.record("shared", run(1000.0, 5000.0));
|
||||||
|
odometer::reading guessed;
|
||||||
|
guessed.begin(20000.0, odometer::origin::guessed);
|
||||||
|
guessed.run(9000.0);
|
||||||
|
first.record("guessed by both", guessed);
|
||||||
|
odometer::reading seeded;
|
||||||
|
seeded.begin(300.0, odometer::origin::counter);
|
||||||
|
second.record("guessed by both", seeded);
|
||||||
|
|
||||||
|
// the second one's save is lost to the first one's, which holds the made up state
|
||||||
|
interleave(second, first, path);
|
||||||
|
REQUIRE(load_store(path).guessed("guessed by both"));
|
||||||
|
|
||||||
|
// the second one writes its known state again. the first one takes it, and the second one the larger state of
|
||||||
|
// the shared vehicle the first one saved
|
||||||
|
REQUIRE(odometer::save(second, path));
|
||||||
|
REQUIRE(odometer::save(first, path));
|
||||||
|
auto const settled{load_store(path)};
|
||||||
|
CHECK(settled.find("shared").value_or(0.0) == doctest::Approx(1010.0));
|
||||||
|
CHECK(settled.find("guessed by both").value_or(0.0) == 300.0);
|
||||||
|
CHECK_FALSE(settled.guessed("guessed by both"));
|
||||||
|
CHECK(first.find("guessed by both").value_or(0.0) == 300.0);
|
||||||
|
CHECK(second.find("shared").value_or(0.0) == doctest::Approx(1010.0));
|
||||||
|
|
||||||
|
// neither one keeps writing the file over to have its own state there
|
||||||
|
mark(path);
|
||||||
|
for (auto round{0}; round < 3; ++round) {
|
||||||
|
REQUIRE(odometer::save(first, path));
|
||||||
|
REQUIRE(odometer::save(second, path));
|
||||||
|
}
|
||||||
|
CHECK(marked(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("odometer: a save with nothing recorded since doesn't write the file again") {
|
||||||
|
scratch_directory directory{"odometer_saved_once"};
|
||||||
|
auto const path{directory.path / "eu07_odometers.txt"};
|
||||||
|
odometer::store store;
|
||||||
|
// states the file holds rounded to its digits
|
||||||
|
store.record("known", run(1000.0, 1234.56789));
|
||||||
|
odometer::reading guessed;
|
||||||
|
guessed.begin(12345.678912, odometer::origin::guessed);
|
||||||
|
guessed.run(98.76543);
|
||||||
|
store.record("guessed", guessed);
|
||||||
|
REQUIRE(odometer::save(store, path));
|
||||||
|
CHECK_FALSE(store.modified());
|
||||||
|
|
||||||
|
// the next save finds the file holding the states, rounded as they are: it doesn't write it
|
||||||
|
mark(path);
|
||||||
|
REQUIRE(odometer::save(store, path));
|
||||||
|
CHECK(marked(path));
|
||||||
|
// and the saves after it don't look at the file any more
|
||||||
|
std::filesystem::remove(path);
|
||||||
|
REQUIRE(odometer::save(store, path));
|
||||||
|
CHECK_FALSE(std::filesystem::exists(path));
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("odometer: a test run neither reads nor saves the player's store") {
|
TEST_CASE("odometer: a test run neither reads nor saves the player's store") {
|
||||||
if (false == Global.test.enabled)
|
if (false == Global.test.enabled)
|
||||||
CHECK(odometer::persistent());
|
CHECK(odometer::persistent());
|
||||||
|
|||||||
@@ -176,6 +176,13 @@ void store::merge(store const &Other) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool store::holds(store const &Other) const {
|
||||||
|
return std::all_of(Other.m_states.begin(), Other.m_states.end(), [this](auto const &Entry) {
|
||||||
|
auto const lookup{m_states.find(Entry.first)};
|
||||||
|
return lookup != m_states.end() && false == newer(Entry.second, lookup->second);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
std::optional<double> read_legacy(std::istream &Input) {
|
std::optional<double> read_legacy(std::istream &Input) {
|
||||||
std::stringstream content;
|
std::stringstream content;
|
||||||
content << Input.rdbuf();
|
content << Input.rdbuf();
|
||||||
@@ -277,8 +284,21 @@ store &states() {
|
|||||||
return odometers;
|
return odometers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// the states as the file holds them once written, the km rounded to the digits written
|
||||||
|
store as_written(store const &States) {
|
||||||
|
std::stringstream text;
|
||||||
|
States.write(text);
|
||||||
|
store result;
|
||||||
|
result.read(text);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
bool save(store &States, std::filesystem::path const &File) {
|
bool save(store &States, std::filesystem::path const &File) {
|
||||||
if (false == States.modified())
|
if (false == States.modified() && false == States.unconfirmed())
|
||||||
return true;
|
return true;
|
||||||
std::error_code error;
|
std::error_code error;
|
||||||
if (File.has_parent_path())
|
if (File.has_parent_path())
|
||||||
@@ -286,11 +306,19 @@ bool save(store &States, std::filesystem::path const &File) {
|
|||||||
// other simulations on the machine (multiplayer, parallel runs) keep their states in the same file: what they
|
// other simulations on the machine (multiplayer, parallel runs) keep their states in the same file: what they
|
||||||
// saved since this one read it is merged in, not overwritten
|
// saved since this one read it is merged in, not overwritten
|
||||||
{
|
{
|
||||||
|
store saved;
|
||||||
std::ifstream input{File, std::ios::binary};
|
std::ifstream input{File, std::ios::binary};
|
||||||
if (input.is_open()) {
|
if (input.is_open())
|
||||||
store saved;
|
|
||||||
saved.read(input);
|
saved.read(input);
|
||||||
States.merge(saved);
|
States.merge(saved);
|
||||||
|
// another simulation that read the file before the last save of this one and put its own in place after it
|
||||||
|
// dropped the states this one wrote. when the file holds them all, the same or newer, there's nothing to write
|
||||||
|
if (false == States.modified()) {
|
||||||
|
if (saved.holds(as_written(States))) {
|
||||||
|
States.confirmed();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
WriteLog("Odometer states saved to \"" + File.string() + "\" are missing from it, saving them again");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// written aside first, a crash in the middle of a write doesn't lose the states. the file aside is this process'
|
// written aside first, a crash in the middle of a write doesn't lose the states. the file aside is this process'
|
||||||
|
|||||||
@@ -46,10 +46,19 @@ public:
|
|||||||
// the odometers only count up, so of two states of a vehicle the larger is the newer one. they aren't added
|
// the odometers only count up, so of two states of a vehicle the larger is the newer one. they aren't added
|
||||||
// up: two simulations on one machine (multiplayer) count the same runs of the same vehicles
|
// up: two simulations on one machine (multiplayer) count the same runs of the same vehicles
|
||||||
void merge(store const &Other);
|
void merge(store const &Other);
|
||||||
|
// this store holds every state of the other one, the same or a newer one
|
||||||
|
bool holds(store const &Other) const;
|
||||||
|
|
||||||
// there are states recorded since they were last saved
|
// there are states recorded since they were last saved
|
||||||
bool modified() const { return m_modified; }
|
bool modified() const { return m_modified; }
|
||||||
void saved() { m_modified = false; }
|
// the states were written to the file. until a later save finds them there (confirmed()), they may have been lost to
|
||||||
|
// another simulation's save, which read the file before this one wrote it and put its own in place after
|
||||||
|
void saved() {
|
||||||
|
m_modified = false;
|
||||||
|
m_unconfirmed = true;
|
||||||
|
}
|
||||||
|
bool unconfirmed() const { return m_unconfirmed; }
|
||||||
|
void confirmed() { m_unconfirmed = false; }
|
||||||
std::size_t size() const { return m_states.size(); }
|
std::size_t size() const { return m_states.size(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -62,6 +71,7 @@ private:
|
|||||||
|
|
||||||
std::map<std::string, state> m_states;
|
std::map<std::string, state> m_states;
|
||||||
bool m_modified{false};
|
bool m_modified{false};
|
||||||
|
bool m_unconfirmed{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
// the vehicle is one the store keeps: one with a driver's cab, which can show its odometer on the cab's counter or a
|
// the vehicle is one the store keeps: one with a driver's cab, which can show its odometer on the cab's counter or a
|
||||||
@@ -113,8 +123,9 @@ bool persistent();
|
|||||||
store &states();
|
store &states();
|
||||||
std::filesystem::path file();
|
std::filesystem::path file();
|
||||||
// writes the store to the file when anything was recorded since, merged with what other simulations saved in the
|
// writes the store to the file when anything was recorded since, merged with what other simulations saved in the
|
||||||
// file meanwhile. it's written to a file of this process aside first, then put in place. returns false when the
|
// file meanwhile. it's written to a file of this process aside first, then put in place. with nothing recorded since,
|
||||||
// write failed
|
// the file is read once more after a write, and written again when it lacks any of the states (another simulation's
|
||||||
|
// save put its file over this one's); otherwise left as it is. returns false when the write failed
|
||||||
bool save(store &States, std::filesystem::path const &File);
|
bool save(store &States, std::filesystem::path const &File);
|
||||||
// saves the store of the simulation to its file. a test run doesn't save it
|
// saves the store of the simulation to its file. a test run doesn't save it
|
||||||
bool save();
|
bool save();
|
||||||
|
|||||||
Reference in New Issue
Block a user