diff --git a/tests/unit/storage_v2_durability_inmemory.cpp b/tests/unit/storage_v2_durability_inmemory.cpp index 7cb1fc17a..d839a8518 100644 --- a/tests/unit/storage_v2_durability_inmemory.cpp +++ b/tests/unit/storage_v2_durability_inmemory.cpp @@ -736,34 +736,49 @@ TEST_P(DurabilityTest, SnapshotPeriodic) { // NOLINTNEXTLINE(hicpp-special-member-functions) TEST_P(DurabilityTest, SnapshotFallback) { // Create snapshot. - std::size_t current_number_of_snapshots = 0; + std::size_t number_to_save; { + // DEVNOTE_1: assumes that snapshot disk write takes less than this + auto const expected_write_time = std::chrono::milliseconds(750); + auto const snapshot_interval = std::chrono::milliseconds(3000); + std::unique_ptr store(new memgraph::storage::InMemoryStorage( {.items = {.properties_on_edges = GetParam()}, - .durability = {.storage_directory = storage_directory, - .snapshot_wal_mode = memgraph::storage::Config::Durability::SnapshotWalMode::PERIODIC_SNAPSHOT, - .snapshot_interval = std::chrono::milliseconds(3000)}})); - CreateBaseDataset(store.get(), GetParam()); - std::this_thread::sleep_for(std::chrono::milliseconds(3500)); - current_number_of_snapshots = GetSnapshotsList().size(); - ASSERT_GE(current_number_of_snapshots, 1); - CreateExtendedDataset(store.get()); - std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + .durability = { + .storage_directory = storage_directory, + .snapshot_wal_mode = memgraph::storage::Config::Durability::SnapshotWalMode::PERIODIC_SNAPSHOT, + .snapshot_interval = snapshot_interval, + .snapshot_retention_count = 10, // We don't anticipate that we make this many + }})); + + auto const ensure_snapshot_is_written = [&](auto &&func) { + auto const pre_count = GetSnapshotsList().size(); + func(); + // wait long enough to ensure at least one CreateSnapshot has been invoked + // DEVNOTE_2: no guarantee that it completed, see DEVNOTE_1 + std::this_thread::sleep_for(snapshot_interval + expected_write_time); + auto const post_count = GetSnapshotsList().size(); + // validate at least one snapshot has happened...hence must have written the writes from func + ASSERT_GT(post_count, pre_count) << "No snapshot exists to capture the last transaction"; + // TODO: maybe double check by looking at InMemoryStorage's commit log, + // its oldest active should be newer than the transaction used when running `func` + }; + + ensure_snapshot_is_written([&]() { CreateBaseDataset(store.get(), GetParam()); }); + number_to_save = GetSnapshotsList().size(); + ensure_snapshot_is_written([&]() { CreateExtendedDataset(store.get()); }); } - auto prev_number_of_snapshots = current_number_of_snapshots; - auto snapshots = GetSnapshotsList(); - current_number_of_snapshots = snapshots.size(); - ASSERT_GE(current_number_of_snapshots, prev_number_of_snapshots + 1); ASSERT_EQ(GetBackupSnapshotsList().size(), 0); ASSERT_EQ(GetWalsList().size(), 0); ASSERT_EQ(GetBackupWalsList().size(), 0); // Destroy snapshots. { - // protect the last, destroy the rest + auto snapshots = GetSnapshotsList(); + // snapshots order newest first, destroy the newest, preserve number_to_save so that we ONLY_BASE auto it = snapshots.begin(); - auto const e = snapshots.end() - 1; + auto const e = snapshots.end() - number_to_save; for (; it != e; ++it) { CorruptSnapshot(*it); }