|
| 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. |
0 commit comments