Testing: add bounded fault-file plan port (M-TESTING-06)
This commit is contained in:
1 parent
4b2bc8b25f
commit
48e5e223af
3 files changed
+445
No files matched your search
@@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Testing
|
||||
{
|
||||
struct FaultFileLimits {
|
||||
std::size_t max_bytes{64ULL * 1024ULL};
|
||||
std::size_t max_entries{256};
|
||||
std::size_t max_boundary_bytes{64};
|
||||
};
|
||||
|
||||
enum class FaultAction : std::uint8_t {
|
||||
/// Fail the operation at this boundary.
|
||||
Error,
|
||||
/// Write at most `partial_bytes` then fail.
|
||||
PartialWrite,
|
||||
/// Block at this boundary until released.
|
||||
Pause
|
||||
};
|
||||
|
||||
struct FaultSpec {
|
||||
std::string boundary{};
|
||||
/// 1-based hit among matching boundary occurrences.
|
||||
std::uint64_t hit_index{1};
|
||||
FaultAction action{FaultAction::Error};
|
||||
std::optional<std::size_t> partial_bytes{};
|
||||
std::string error_code{};
|
||||
};
|
||||
|
||||
/// Parse a fault file. One `boundary hit action [partial=N] [code=NAME]`
|
||||
/// per line; `#` starts a comment and blank lines are ignored.
|
||||
[[nodiscard]] Void::Result::Result<std::vector<FaultSpec>>
|
||||
parse_fault_file(std::string_view text, FaultFileLimits limits = {});
|
||||
|
||||
struct FaultHit {
|
||||
bool triggered{false};
|
||||
FaultAction action{FaultAction::Error};
|
||||
std::optional<std::size_t> partial_bytes{};
|
||||
std::string error_code{};
|
||||
};
|
||||
|
||||
struct FaultTotals {
|
||||
std::size_t configured{0};
|
||||
std::size_t fired{0};
|
||||
std::size_t remaining{0};
|
||||
};
|
||||
|
||||
/// Applies a parsed fault list at named boundaries.
|
||||
///
|
||||
/// Each spec fires only on its `hit_index`-th occurrence of its boundary.
|
||||
/// Boundary names are bounded so a hostile or buggy caller cannot grow
|
||||
/// memory without limit.
|
||||
class FaultPlan
|
||||
{
|
||||
public:
|
||||
|
||||
explicit FaultPlan(std::vector<FaultSpec> specs,
|
||||
std::size_t max_boundaries = 1024);
|
||||
|
||||
/// Count one occurrence of `boundary` and report an injected fault
|
||||
/// when one is due.
|
||||
[[nodiscard]] FaultHit check(std::string_view boundary);
|
||||
|
||||
/// Configured, fired and remaining counts (they reconcile:
|
||||
/// configured == fired + remaining).
|
||||
[[nodiscard]] FaultTotals totals() const noexcept;
|
||||
|
||||
[[nodiscard]] std::size_t occurrences(std::string_view boundary) const;
|
||||
|
||||
private:
|
||||
|
||||
struct Entry {
|
||||
FaultSpec spec{};
|
||||
bool fired{false};
|
||||
};
|
||||
|
||||
std::map<std::string, std::vector<Entry>, std::less<>> m_entries;
|
||||
std::map<std::string, std::size_t, std::less<>> m_occurrences;
|
||||
};
|
||||
} // namespace Void::Testing
|
||||
Reference in new issue
Block a user