// RepeatAggregator: window reset, full-then-summary threshold, summary/notify // cadence, per-key independence, eviction. Deterministic `now` values (not real // time) so the cadence arithmetic is exact. The pipeline's // test_repeated_detections_are_summarized / test_repeated_would_block_is_summarized // cover it end-to-end; this pins the unit behaviour. #include "strawberry/repeat_aggregator.hpp" #include #include #include using namespace strawberry; #define CHECK(cond) \ do { \ if (!(cond)) { \ std::cerr << "CHECK FAILED: " #cond " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; \ std::abort(); \ } \ } while (0) int main() { // full=3, window=10000, summary=100, notify=200 (ns). RepeatAggregator agg(3, 10000, 100, 200); const std::string k = "would_block|hash|rule"; // counts 1..3 are kept in full; the 3rd is exactly at the threshold. for (int i = 1; i <= 3; ++i) { const auto o = agg.observe(k, 1000, 1000, 100 + i); CHECK(o.full && !o.aggregated && !o.emit_summary && !o.notify); CHECK(o.at_threshold == (i == 3)); CHECK(o.state->count == static_cast(i)); } CHECK(agg.aggregated_total() == 0); // count 4: first aggregated -> summary due (now-0>=100) and notify due (>=200). { const auto o = agg.observe(k, 1000, 1000, 200); CHECK(o.aggregated && !o.full && o.emit_summary && o.notify); CHECK(o.state->aggregated == 1 && o.state->count == 4); CHECK(o.state->first_pid == 200 && o.state->first_seen_ns == 1000); CHECK(o.state->last_pid == 200 && o.state->last_seen_ns == 1000); } CHECK(agg.aggregated_total() == 1); // count 5 at +50: neither summary (50<100) nor notify (50<200) due. { const auto o = agg.observe(k, 1050, 1050, 201); CHECK(o.aggregated && !o.emit_summary && !o.notify); CHECK(o.state->aggregated == 2 && o.state->last_pid == 201 && o.state->last_seen_ns == 1050); CHECK(o.state->first_pid == 200 && o.state->first_seen_ns == 1000); // first_* unchanged } // count 6 at +100 since last summary: summary due, notify still not (100<200). { const auto o = agg.observe(k, 1100, 1100, 202); CHECK(o.aggregated && o.emit_summary && !o.notify); } // count 7: notify now due (300>=200) and summary due (200>=100). { const auto o = agg.observe(k, 1300, 1300, 203); CHECK(o.aggregated && o.emit_summary && o.notify); } CHECK(agg.aggregated_total() == 4); // Window reset: a gap larger than the window starts a fresh full record. { const auto o = agg.observe(k, 20000, 20000, 300); CHECK(o.full && !o.aggregated && o.state->count == 1 && o.state->window_start_ns == 20000); } // notify_ns == 0: never notifies, still summarizes. RepeatAggregator nonotify(1, 10000, 100, 0); CHECK(nonotify.observe("x", 1000, 1000, 1).full); { const auto o = nonotify.observe("x", 2000, 2000, 2); CHECK(o.aggregated && o.emit_summary && !o.notify); } // Per-key independence; aggregated_total sums across keys. RepeatAggregator multi(1, 10000, 1, 0); CHECK(multi.observe("a", 1000, 1000, 1).full); CHECK(multi.observe("a", 1001, 1001, 1).aggregated); CHECK(multi.observe("b", 1000, 1000, 1).full); CHECK(multi.observe("b", 1001, 1001, 1).aggregated); CHECK(multi.aggregated_total() == 2); std::cout << "RepeatAggregator: PASSED" << std::endl; return 0; }