yams/tests/common/p2p_test_helpers.h
Trevon af609a1e63
refactor(p2p): tighten tests, share helpers, and guard test hooks (#77)
Compile the direct-P2P test-only machinery out of production builds
(YAMS_DAEMON_TEST_HOOKS_ENABLED now wraps the reconnect-loop hook storage,
invoker, and call site, matching the already-guarded setter). Replace the
single shared writer key in p2p_manager tests with one distinct key pair per
writer identity so origin-binding regressions (a record claiming origin X but
signed by Y's key) fail verification. Extract the copy-pasted bytes/text/TempDir
helpers into tests/common/p2p_test_helpers.h and reuse them across the
transport/delta/manager suites. Replace a timing-sensitive fixed sleep in the
transport session-expiry test with a bounded poll that accepts both deadline
manifestations, and gate the memory-sync-service concurrent-lifecycle stress
case behind YAMS_RUN_STRESS_TESTS=1 per the existing FTS5 pattern. Deduplicate
the byte-identical commitment-JSON serialization inside MemorySyncLoop
(coldBootstrapRoot / persistReplicationCheckpoint) into one static helper.

Signed-off-by: trvon <git@trevon.dev>
2026-08-30 11:27:12 -06:00

48 lines
1.5 KiB
C++

#pragma once
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2026 YAMS Contributors
// Shared helpers for direct-P2P unit tests. bytes/text/TempDir were previously
// copy-pasted across daemon/p2p test executables; keep them here so behavior
// stays identical across suites.
#include <chrono>
#include <cstring>
#include <filesystem>
#include <string>
#include <string_view>
#include <vector>
namespace yams::p2p_test {
inline std::vector<std::byte> bytes(std::string_view text) {
std::vector<std::byte> result(text.size());
std::memcpy(result.data(), text.data(), text.size());
return result;
}
inline std::string text(const std::vector<std::byte>& data) {
return std::string(reinterpret_cast<const char*>(data.data()), data.size());
}
/// Scoped directory under the system temp dir, removed on destruction. The
/// per-run timestamp suffix keeps concurrent test processes from colliding.
class TempDir {
public:
explicit TempDir(std::string_view label) {
path = std::filesystem::temp_directory_path() /
("yams-p2p-test-" + std::string(label) + "-" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
std::filesystem::create_directories(path);
}
TempDir(const TempDir&) = delete;
TempDir& operator=(const TempDir&) = delete;
~TempDir() {
std::error_code error;
std::filesystem::remove_all(path, error);
}
std::filesystem::path path;
};
} // namespace yams::p2p_test