This repository was archived by the owner on Oct 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_watchdog.c
More file actions
104 lines (78 loc) · 2.44 KB
/
Copy pathtest_watchdog.c
File metadata and controls
104 lines (78 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "delay.h"
#include "interrupt.h"
#include "soft_timer.h"
#include "test_helpers.h"
#include "unity.h"
#include "watchdog.h"
WatchdogStorage s_watchdog;
#define TIMEOUT_MS 50
static bool s_expiry_called = false;
static void *s_passed_context;
static void prv_expiry_callback(void *context) {
s_expiry_called = true;
s_passed_context = context;
}
const WatchdogSettings settings = {
.timeout_ms = TIMEOUT_MS,
.callback = prv_expiry_callback,
};
static void prv_reset_callback(void) {
s_expiry_called = false;
s_passed_context = (void *)0;
}
void setup_test(void) {
interrupt_init();
soft_timer_init();
prv_reset_callback();
}
void teardown_test(void) {}
void test_watchdog_expiry(void) {
uint32_t context_data = 0xdeadbeef;
watchdog_start(&s_watchdog, TIMEOUT_MS, prv_expiry_callback, &context_data);
TEST_ASSERT_FALSE(s_expiry_called);
delay_ms(TIMEOUT_MS + 50);
TEST_ASSERT_TRUE(s_expiry_called);
TEST_ASSERT_EQUAL(&context_data, s_passed_context);
}
void test_watchdog_kick(void) {
uint32_t context_data = 0xdeadbeef;
watchdog_start(&s_watchdog, TIMEOUT_MS, prv_expiry_callback, &context_data);
delay_ms(TIMEOUT_MS - 5);
TEST_ASSERT_FALSE(s_expiry_called);
watchdog_kick(&s_watchdog);
delay_ms(10);
TEST_ASSERT_FALSE(s_expiry_called);
delay_ms(TIMEOUT_MS);
TEST_ASSERT_TRUE(s_expiry_called);
TEST_ASSERT_EQUAL(&context_data, s_passed_context);
}
void test_watchdog_expired_does_not_call_callback_multiple_times(void) {
uint32_t context_data = 0xdeadbeef;
watchdog_start(&s_watchdog, TIMEOUT_MS, prv_expiry_callback, &context_data);
delay_ms(TIMEOUT_MS + 20);
TEST_ASSERT_TRUE(s_expiry_called);
TEST_ASSERT_EQUAL(&context_data, s_passed_context);
prv_reset_callback();
delay_ms(TIMEOUT_MS + 5);
TEST_ASSERT_FALSE(s_expiry_called);
}
void test_watchdog_expired_can_start_again(void) {
uint32_t context_data = 0xdeadbeef;
watchdog_start(&s_watchdog, TIMEOUT_MS, prv_expiry_callback, &context_data);
// its expired
delay_ms(TIMEOUT_MS + 20);
TEST_ASSERT_TRUE(s_expiry_called);
prv_reset_callback();
// started again
watchdog_start(&s_watchdog, TIMEOUT_MS, prv_expiry_callback, &context_data);
delay_ms(TIMEOUT_MS - 5);
// kicked
watchdog_kick(&s_watchdog);
delay_ms(10);
// not expired cuz kicked
TEST_ASSERT_FALSE(s_expiry_called);
// expired again
delay_ms(TIMEOUT_MS + 5);
TEST_ASSERT_TRUE(s_expiry_called);
TEST_ASSERT_EQUAL(&context_data, s_passed_context);
}