nx_progress_watchdog_test.nx source
↩ module page · 49 lines · 1720 B
1// nx_progress_watchdog_test.nx -- smoke for stall-detection liveness monitor.
2
3import "nx_syscalls.nx"
4import "nx_tier.nx"
5import "nx_progress_watchdog.nx"
6
7func busy_wait_ms(ms: nx_int) -> nx_int {
8 let target_ns: nx_int = ms * NX_NS_PER_MS
9 let t0: nx_int = nx_clock_monotonic_ns()
10 var go: nx_int = 1
11 while go == 1 {
12 let now: nx_int = nx_clock_monotonic_ns()
13 if now - t0 > target_ns { go = 0 }
14 }
15 return 0
16}
17
18func main() -> nx_exit {
19 let w: *NxWatchdog = nx_progress_watchdog_new(60000)
20 if nx_progress_watchdog_n_heartbeats(w) != 0 { return 11 }
21 if nx_progress_watchdog_n_stalls(w) != 0 { return 12 }
22 if nx_progress_watchdog_check(w) != NX_WATCHDOG_OK { return 13 }
23
24 nx_progress_watchdog_heartbeat(w)
25 if nx_progress_watchdog_n_heartbeats(w) != 1 { return 21 }
26 if nx_progress_watchdog_check(w) != NX_WATCHDOG_OK { return 22 }
27
28 nx_progress_watchdog_heartbeat(w)
29 nx_progress_watchdog_heartbeat(w)
30 if nx_progress_watchdog_n_heartbeats(w) != 3 { return 23 }
31
32 if nx_progress_watchdog_stall_timeout_ms(w) != 60000 { return 31 }
33
34 let w2: *NxWatchdog = nx_progress_watchdog_new(1)
35 busy_wait_ms(10)
36 if nx_progress_watchdog_check(w2) != NX_WATCHDOG_STALLED { return 41 }
37 if nx_progress_watchdog_n_stalls(w2) != 1 { return 42 }
38 if nx_progress_watchdog_check(w2) != NX_WATCHDOG_STALLED { return 43 }
39 if nx_progress_watchdog_n_stalls(w2) != 2 { return 44 }
40
41 nx_progress_watchdog_heartbeat(w2)
42 if nx_progress_watchdog_check(w2) != NX_WATCHDOG_OK { return 51 }
43
44 if nx_progress_watchdog_elapsed_ms(w2) < 0 { return 61 }
45 busy_wait_ms(3)
46 if nx_progress_watchdog_elapsed_ms(w2) < 2 { return 62 }
47
48 return 0
49}