1#include "watchdog/internal.h"
2
3#include <string.h>
4#include <test/test.h>
5
6TEST_GROUP_DECLARE(watchdog);
7
8static void watchdog_populate_full_window(struct watchdog_buckets *buckets) {
9 memset(buckets, 0, sizeof(*buckets));
10 seqcount_init(s: &buckets->seq);
11 buckets->idx = 9;
12 buckets->last_heartbeat_ms = 10000;
13 for (size_t i = 1; i <= 8; i++)
14 buckets->buckets_internal[i].heartbeats = 20;
15}
16
17TEST_DECLARE_UNIT(watchdog, frozen_window_missing_heartbeats) {
18 struct watchdog_buckets buckets;
19 watchdog_populate_full_window(buckets: &buckets);
20
21 size_t heartbeats = SIZE_MAX;
22 size_t expected = SIZE_MAX;
23 fx32_32_t score = -1;
24 bool ready = watchdog_count_heartbeats_at(buckets: &buckets, window_buckets: 8, now: 19000, bucket_interval_ms: 1000, expected_per_bucket: 20,
25 out_heartbeats: &heartbeats, out_expected: &expected, out_score: &score);
26
27 TEST_ASSERT(ready);
28 TEST_ASSERT_EQ(heartbeats, 0);
29 TEST_ASSERT_EQ(expected, 160);
30 TEST_ASSERT_EQ(score, FX_ONE);
31 return TEST_SUCCESS;
32}
33
34TEST_DECLARE_UNIT(watchdog, current_window_recent_heartbeats) {
35 struct watchdog_buckets buckets;
36 watchdog_populate_full_window(buckets: &buckets);
37
38 size_t heartbeats = SIZE_MAX;
39 size_t expected = SIZE_MAX;
40 fx32_32_t score = -1;
41 bool ready = watchdog_count_heartbeats_at(buckets: &buckets, window_buckets: 8, now: 10000, bucket_interval_ms: 1000, expected_per_bucket: 20,
42 out_heartbeats: &heartbeats, out_expected: &expected, out_score: &score);
43
44 TEST_ASSERT(ready);
45 TEST_ASSERT_EQ(heartbeats, 160);
46 TEST_ASSERT_EQ(expected, 160);
47 TEST_ASSERT_EQ(score, 0);
48 return TEST_SUCCESS;
49}
50
51TEST_DECLARE_UNIT(watchdog, ewma_fixed_point_sample) {
52 struct ewma score;
53 ewma_init(e: &score, FX(0.15));
54 score.ewma = FX(0.25);
55
56 fx32_32_t updated = ewma_update(e: &score, FX_ONE);
57 TEST_ASSERT_GT_S(updated, FX(0.25));
58 TEST_ASSERT_LT_S(updated, FX_ONE);
59 return TEST_SUCCESS;
60}
61