Skip to content

Commit 84883f1

Browse files
petermmampcode-com
andcommitted
Add generic_unix test for scheduler watchdog NIFs
Add test_scheduler_watchdog to the eavmlib test suite covering 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 81faaf2 commit 84883f1

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

tests/libs/eavmlib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(ERLANG_MODULES
2929
test_http_server
3030
test_mdns
3131
test_port
32+
test_scheduler_watchdog
3233
test_timer_manager
3334
)
3435

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
23+
-export([test/0]).
24+
25+
test() ->
26+
ok = test_status_returns_proplist(),
27+
ok = test_mode_switch(),
28+
ok = test_mode_badarg(),
29+
ok = test_stall_updates_ages(),
30+
ok.
31+
32+
%% Verify scheduler_watchdog_status/0 returns a well-formed proplist
33+
test_status_returns_proplist() ->
34+
Status = atomvm:scheduler_watchdog_status(),
35+
true = is_list(Status),
36+
{mode, Mode} = lists:keyfind(mode, 1, Status),
37+
true = (Mode =:= restart orelse Mode =:= log_only),
38+
{timeout_ms, TimeoutMs} = lists:keyfind(timeout_ms, 1, Status),
39+
true = is_integer(TimeoutMs),
40+
{poll_interval_ms, PollMs} = lists:keyfind(poll_interval_ms, 1, Status),
41+
true = is_integer(PollMs),
42+
{active_mask, Mask} = lists:keyfind(active_mask, 1, Status),
43+
true = is_integer(Mask),
44+
{ages, Ages} = lists:keyfind(ages, 1, Status),
45+
true = is_list(Ages),
46+
ok.
47+
48+
%% Verify mode can be toggled and status reflects the change
49+
test_mode_switch() ->
50+
ok = atomvm:scheduler_watchdog_mode(log_only),
51+
S1 = atomvm:scheduler_watchdog_status(),
52+
{mode, log_only} = lists:keyfind(mode, 1, S1),
53+
54+
ok = atomvm:scheduler_watchdog_mode(restart),
55+
S2 = atomvm:scheduler_watchdog_status(),
56+
{mode, restart} = lists:keyfind(mode, 1, S2),
57+
ok.
58+
59+
%% Verify badarg on invalid mode
60+
test_mode_badarg() ->
61+
ok =
62+
try
63+
atomvm:scheduler_watchdog_mode(invalid),
64+
fail
65+
catch
66+
error:badarg -> ok
67+
end,
68+
ok =
69+
try
70+
atomvm:scheduler_watchdog_mode(42),
71+
fail
72+
catch
73+
error:badarg -> ok
74+
end,
75+
ok.
76+
77+
%% Stall a scheduler briefly and verify the ages list is populated
78+
test_stall_updates_ages() ->
79+
ok = atomvm:scheduler_watchdog_mode(log_only),
80+
81+
%% Stall this scheduler for 100ms to generate heartbeat activity
82+
ok = atomvm:debug_stall(100),
83+
84+
Status = atomvm:scheduler_watchdog_status(),
85+
{active_mask, Mask} = lists:keyfind(active_mask, 1, Status),
86+
true = Mask > 0,
87+
{ages, Ages} = lists:keyfind(ages, 1, Status),
88+
true = length(Ages) > 0,
89+
lists:foreach(
90+
fun({SlotId, AgeMs}) ->
91+
true = is_integer(SlotId),
92+
true = SlotId >= 0,
93+
true = is_integer(AgeMs),
94+
true = AgeMs >= 0
95+
end,
96+
Ages
97+
),
98+
ok = atomvm:scheduler_watchdog_mode(restart),
99+
ok.

tests/libs/eavmlib/tests.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ start() ->
2929
test_http_server,
3030
test_mdns,
3131
test_port,
32+
test_scheduler_watchdog,
3233
test_timer_manager,
3334
test_ahttp_client
3435
]).

0 commit comments

Comments
 (0)