Step 3
This commit is contained in:
112
tests/PersistenceWriterTests.cpp
Normal file
112
tests/PersistenceWriterTests.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "PersistenceWriter.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
int gFailures = 0;
|
||||
|
||||
void Expect(bool condition, const char* message)
|
||||
{
|
||||
if (condition)
|
||||
return;
|
||||
|
||||
std::cerr << "FAIL: " << message << "\n";
|
||||
++gFailures;
|
||||
}
|
||||
|
||||
PersistenceSnapshot MakeRuntimeSnapshot(const std::string& contents)
|
||||
{
|
||||
PersistenceSnapshot snapshot;
|
||||
snapshot.targetKind = PersistenceTargetKind::RuntimeState;
|
||||
snapshot.targetPath = std::filesystem::temp_directory_path() / "video-shader-persistence-writer-test.json";
|
||||
snapshot.contents = contents;
|
||||
snapshot.reason = "test";
|
||||
snapshot.debounceKey = "runtime-state";
|
||||
snapshot.debounceAllowed = true;
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
void TestDebouncedRequestsCoalesceToNewestSnapshot()
|
||||
{
|
||||
std::mutex mutex;
|
||||
std::vector<PersistenceSnapshot> writtenSnapshots;
|
||||
PersistenceWriter writer(
|
||||
std::chrono::milliseconds(1000),
|
||||
[&](const PersistenceSnapshot& snapshot, std::string&) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
writtenSnapshots.push_back(snapshot);
|
||||
return true;
|
||||
});
|
||||
|
||||
std::string error;
|
||||
Expect(writer.EnqueueSnapshot(MakeRuntimeSnapshot("first"), error), "first debounced snapshot enqueues");
|
||||
Expect(writer.EnqueueSnapshot(MakeRuntimeSnapshot("second"), error), "second debounced snapshot enqueues");
|
||||
|
||||
PersistenceWriterMetrics metrics = writer.GetMetrics();
|
||||
Expect(metrics.pendingCount == 1, "debounced snapshots share one pending slot");
|
||||
Expect(metrics.enqueuedCount == 1, "first debounced snapshot counts as enqueue");
|
||||
Expect(metrics.coalescedCount == 1, "second debounced snapshot counts as coalesced");
|
||||
|
||||
writer.StopAndFlush();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
Expect(writtenSnapshots.size() == 1, "flush writes one coalesced snapshot");
|
||||
Expect(!writtenSnapshots.empty() && writtenSnapshots[0].contents == "second", "coalesced writer keeps newest snapshot");
|
||||
}
|
||||
|
||||
metrics = writer.GetMetrics();
|
||||
Expect(metrics.pendingCount == 0, "flush drains pending debounced snapshot");
|
||||
Expect(metrics.writtenCount == 1, "flush records one successful write");
|
||||
}
|
||||
|
||||
void TestImmediateRequestsAreNotCoalesced()
|
||||
{
|
||||
std::mutex mutex;
|
||||
std::vector<PersistenceSnapshot> writtenSnapshots;
|
||||
PersistenceWriter writer(
|
||||
std::chrono::milliseconds(1000),
|
||||
[&](const PersistenceSnapshot& snapshot, std::string&) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
writtenSnapshots.push_back(snapshot);
|
||||
return true;
|
||||
});
|
||||
|
||||
PersistenceSnapshot first = MakeRuntimeSnapshot("first");
|
||||
first.debounceAllowed = false;
|
||||
PersistenceSnapshot second = MakeRuntimeSnapshot("second");
|
||||
second.debounceAllowed = false;
|
||||
|
||||
std::string error;
|
||||
Expect(writer.EnqueueSnapshot(first, error), "first immediate snapshot enqueues");
|
||||
Expect(writer.EnqueueSnapshot(second, error), "second immediate snapshot enqueues");
|
||||
writer.StopAndFlush();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
Expect(writtenSnapshots.size() == 2, "immediate snapshots are written independently");
|
||||
Expect(writtenSnapshots.size() == 2 && writtenSnapshots[0].contents == "first" && writtenSnapshots[1].contents == "second",
|
||||
"immediate snapshots preserve order");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
TestDebouncedRequestsCoalesceToNewestSnapshot();
|
||||
TestImmediateRequestsAreNotCoalesced();
|
||||
|
||||
if (gFailures != 0)
|
||||
{
|
||||
std::cerr << gFailures << " persistence writer test(s) failed.\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Persistence writer tests passed.\n";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user