Skip to content

Commit 3d7409f

Browse files
petermmampcode-com
andcommitted
Add RP2 test for scheduler watchdog NIFs
Same test coverage as ESP32 and generic_unix: status proplist shape, mode toggling, badarg validation, and heartbeat age population via debug_stall/1. Signed-off-by: Peter M <petermm@gmail.com> Amp-Thread-ID: https://ampcode.com/threads/T-019cffb9-55d6-70d2-8382-88fd927bdc77 Co-authored-by: Amp <amp@ampcode.com>
1 parent 84883f1 commit 3d7409f

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

src/platforms/rp2/tests/test_erl_sources/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ endfunction()
6161

6262
compile_erlang(test_clocks "")
6363
compile_erlang(test_smp "")
64+
compile_erlang(test_scheduler_watchdog "")
6465
compile_erlang(test_crypto ../../../esp32/test/main/test_erl_sources/)
6566

6667
set(erlang_test_beams
6768
test_clocks.beam
6869
test_smp.beam
70+
test_scheduler_watchdog.beam
6971
test_crypto.beam
7072
)
7173

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2026 Peter M <petermm@gmail.com>
5+
%
6+
% Licensed under the Apache License, Version 2.0 (the "License");
7+
% you may not use this file except in compliance with the License.
8+
% You may obtain a copy of the License at
9+
%
10+
% http://www.apache.org/licenses/LICENSE-2.0
11+
%
12+
% Unless required by applicable law or agreed to in writing, software
13+
% distributed under the License is distributed on an "AS IS" BASIS,
14+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
% See the License for the specific language governing permissions and
16+
% limitations under the License.
17+
%
18+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
-module(test_scheduler_watchdog).
22+
-export([start/0]).
23+
24+
start() ->
25+
ok = test_status_returns_proplist(),
26+
ok = test_mode_switch(),
27+
ok = test_mode_badarg(),
28+
ok = test_stall_updates_ages(),
29+
ok.
30+
31+
%% Verify scheduler_watchdog_status/0 returns a well-formed proplist
32+
test_status_returns_proplist() ->
33+
Status = atomvm:scheduler_watchdog_status(),
34+
true = is_list(Status),
35+
{mode, Mode} = lists:keyfind(mode, 1, Status),
36+
true = (Mode =:= restart orelse Mode =:= log_only),
37+
{timeout_ms, TimeoutMs} = lists:keyfind(timeout_ms, 1, Status),
38+
true = is_integer(TimeoutMs),
39+
{poll_interval_ms, PollMs} = lists:keyfind(poll_interval_ms, 1, Status),
40+
true = is_integer(PollMs),
41+
{active_mask, Mask} = lists:keyfind(active_mask, 1, Status),
42+
true = is_integer(Mask),
43+
{ages, Ages} = lists:keyfind(ages, 1, Status),
44+
true = is_list(Ages),
45+
ok.
46+
47+
%% Verify mode can be toggled and status reflects the change
48+
test_mode_switch() ->
49+
ok = atomvm:scheduler_watchdog_mode(log_only),
50+
S1 = atomvm:scheduler_watchdog_status(),
51+
{mode, log_only} = lists:keyfind(mode, 1, S1),
52+
53+
ok = atomvm:scheduler_watchdog_mode(restart),
54+
S2 = atomvm:scheduler_watchdog_status(),
55+
{mode, restart} = lists:keyfind(mode, 1, S2),
56+
ok.
57+
58+
%% Verify badarg on invalid mode
59+
test_mode_badarg() ->
60+
ok =
61+
try
62+
atomvm:scheduler_watchdog_mode(invalid),
63+
fail
64+
catch
65+
error:badarg -> ok
66+
end,
67+
ok =
68+
try
69+
atomvm:scheduler_watchdog_mode(42),
70+
fail
71+
catch
72+
error:badarg -> ok
73+
end,
74+
ok.
75+
76+
%% Stall a scheduler briefly and verify the ages list is populated
77+
test_stall_updates_ages() ->
78+
ok = atomvm:scheduler_watchdog_mode(log_only),
79+
80+
%% Stall this scheduler for 100ms to generate heartbeat activity
81+
ok = atomvm:debug_stall(100),
82+
83+
Status = atomvm:scheduler_watchdog_status(),
84+
{active_mask, Mask} = lists:keyfind(active_mask, 1, Status),
85+
true = Mask > 0,
86+
{ages, Ages} = lists:keyfind(ages, 1, Status),
87+
true = length(Ages) > 0,
88+
lists:foreach(
89+
fun({SlotId, AgeMs}) ->
90+
true = is_integer(SlotId),
91+
true = SlotId >= 0,
92+
true = is_integer(AgeMs),
93+
true = AgeMs >= 0
94+
end,
95+
Ages
96+
),
97+
ok = atomvm:scheduler_watchdog_mode(restart),
98+
ok.

src/platforms/rp2/tests/test_main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ TEST_CASE(test_smp)
168168
TEST_ASSERT_EQUAL_INT(OK_ATOM, ret_value);
169169
}
170170

171+
TEST_CASE(test_scheduler_watchdog)
172+
{
173+
term ret_value = avm_test_case("test_scheduler_watchdog.beam");
174+
TEST_ASSERT_EQUAL_INT(OK_ATOM, ret_value);
175+
}
176+
171177
/* newlib stubs to get AVM_ABORT to work */
172178
pid_t _getpid()
173179
{

0 commit comments

Comments
 (0)