code wiki / (root) / nx_sweep_daemon_gate.nx

nx_sweep_daemon_gate.nx source

↩ module page · 118 lines · 6800 B

1// nx_sweep_daemon_gate.nx -- gates the standing sweep on a fixture registry (runner: cd a scratch dir). 2// THE SOTA PROPERTY under test: a check that overruns its deadline is KILLED and scored TIMEOUT -- the 3// sweep never hangs. Also: PASS/FAIL verdicts are correct, the registry is data-driven (3 rows parsed), 4// consecutive-red counts, and the plain-English status renders the right chips. Uses /bin/true, /bin/false, 5// and /bin/sleep as the fixture victims (testing OUR killer -- the coreutils are the controlled subjects). 6// Exit 0 only on all-PASS. license_tier: ORIGINAL expect_exit: 0 7import "nx_sweep_daemon_lib.nx" 8import "nx_seg_store.nx" // ss_writefile -- fixture author 9 10const GG_CHECKS: i64 = 12 11const GG_STALE: i64 = 1 // subtract from stored mtime to force the "changed" reload path 12const GG_PTR: i64 = 16 13const GG_HANG_MS: i64 = 5000 // "did not hang" bound 14const GG_HTML_CAP: i64 = 262144 15const GG_FIX_N: i64 = 3 // fixture check count 16const GG_YES_LEN: i64 = 3 // "YES" chip substring length 17const GG_ASCII_Y: i64 = 89 // 'Y' 18const GG_ASCII_E: i64 = 69 // 'E' 19const GG_ASCII_S: i64 = 83 // 'S' 20 21func sd_app_stub(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 22func gg_check(name: *u8, ok: i64, pass: *i64) -> i64 { 23 sd_app_stub("T " as *u8); sd_app_stub(name); sd_app_stub(" -> " as *u8) 24 if ok == 1 { sd_app_stub("PASS\n" as *u8); pass[0] = pass[0] + 1 } else { sd_app_stub("FAIL\n" as *u8) } 25 return 0 26} 27func gg_writefile(path: *u8, body: *u8) -> i64 { var n: i64 = 0; while body[n] != (0 as u8) { n = n + 1 } return ss_writefile(path, body, n) } 28// find loaded check index by name (linear; fixtures are few) 29func gg_idx(ctx: *i64, name: *u8) -> i64 { 30 let a_name: *i64 = sc_arr(ctx, SC_NAME) 31 var i: i64 = 0 32 while i < sc_n(ctx) { 33 var m: i64 = 1; var j: i64 = 0 34 let nm: *u8 = a_name[i] as *u8 35 while nm[j] != (0 as u8) { if nm[j] != name[j] { m = 0; j = sd_len(nm) } else { j = j + 1 } } 36 if m == 1 { if name[j] == (0 as u8) { return i } } 37 i = i + 1 38 } 39 return 0 - 1 40} 41 42func main() -> i64 { 43 let pass: *i64 = sys_mmap(GG_PTR) as *i64 44 pass[0] = 0 45 // fixture registry: a passer (true=0), a failer (false=1 but expect 0 -> FAIL), and a runaway (sleep 30 46 // under a 400ms deadline -> TIMEOUT). Field order: name|question|cwd|argv|timeout_ms|expect_exit 47 gg_writefile("checks.reg" as *u8, 48 "# fixture\npasser|Does the passer pass?|.|/bin/true|3000|0\nfailer|Does the failer fail?|.|/bin/false|3000|0\nrunaway|Is the runaway bounded?|.|/bin/sleep 30|400|0\n" as *u8) 49 // T1 registry is data-driven: exactly 3 rows parsed (comment skipped) 50 let ctx: *i64 = sd_new() 51 let n: i64 = sd_load(ctx, "checks.reg" as *u8) 52 gg_check("registry-3-rows-parsed" as *u8, (n == GG_FIX_N) as i64, pass) 53 let a_v: *i64 = sc_arr(ctx, SC_VERDICT) 54 let t0: i64 = sw_mono_ms() 55 let bad: i64 = sd_run_all(ctx) 56 let elapsed: i64 = sw_mono_ms() - t0 57 // T2 passer -> PASS 58 let ip: i64 = gg_idx(ctx, "passer" as *u8) 59 gg_check("passer-verdict-pass" as *u8, (a_v[ip] == SW_V_PASS) as i64, pass) 60 // T3 failer -> FAIL (exit 1 != expect 0) 61 let if2: i64 = gg_idx(ctx, "failer" as *u8) 62 gg_check("failer-verdict-fail" as *u8, (a_v[if2] == SW_V_FAIL) as i64, pass) 63 // T4 runaway -> TIMEOUT (THE SOTA property: killed, not hung) 64 let ir: i64 = gg_idx(ctx, "runaway" as *u8) 65 gg_check("runaway-verdict-timeout" as *u8, (a_v[ir] == SW_V_TIMEOUT) as i64, pass) 66 // T5 the sweep did NOT hang: a 30s sleep under a 400ms deadline returned in well under 5s total 67 gg_check("sweep-bounded-not-hung" as *u8, (elapsed < GG_HANG_MS) as i64, pass) 68 // T6 bad-count = 2 (failer + runaway) 69 gg_check("bad-count-two" as *u8, (bad == GG_FIX_N - 1) as i64, pass) 70 // T7 plain-English status renders a headline + a YES chip for the passer 71 let html: *u8 = sys_mmap(GG_HTML_CAP) 72 let hl: i64 = sd_render_status(ctx, html, GG_HTML_CAP) 73 var okyes: i64 = 0 74 var i: i64 = 0 75 while i + GG_YES_LEN <= hl { if html[i] == (GG_ASCII_Y as u8) { if html[i+1] == (GG_ASCII_E as u8) { if html[i + GG_YES_LEN - 1] == (GG_ASCII_S as u8) { okyes = 1; i = hl } } } i = i + 1 } 76 gg_check("status-renders-yes-chip" as *u8, okyes, pass) 77 // T8 LAST-GOOD PRESERVED: a read-fail (absent registry) must NOT clear the loaded set 78 var ok8: i64 = 0 79 if sd_load(ctx, "absent.reg" as *u8) == 0 - 1 { if sc_n(ctx) == GG_FIX_N { ok8 = 1 } } 80 gg_check("last-good-preserved-on-readfail" as *u8, ok8, pass) 81 // T9 HOT-RELOAD GATE: mtime probe works (present>0, absent=-1) and an UNCHANGED mtime skips the reload 82 var ok9: i64 = 0 83 let mt: i64 = sd_mtime(ctx, "checks.reg" as *u8) 84 if mt > 0 { if sd_mtime(ctx, "absent.reg" as *u8) == 0 - 1 { 85 ctx[SC_MTIME] = mt 86 if sd_reload_if_changed(ctx, "checks.reg" as *u8) == 0 { ok9 = 1 } // unchanged -> no reparse 87 } } 88 gg_check("hot-reload-mtime-gated" as *u8, ok9, pass) 89 // T10 CHANGED path: with a STALE stored mtime, reload MUST fire and re-parse (deterministic, no touch). 90 var ok10: i64 = 0 91 let mt2: i64 = sd_mtime(ctx, "checks.reg" as *u8) 92 if mt2 > 0 { 93 ctx[SC_MTIME] = mt2 - GG_STALE // pretend we loaded an older version 94 if sd_reload_if_changed(ctx, "checks.reg" as *u8) == 1 { // detects change -> reloads 95 if sc_n(ctx) == GG_FIX_N { if ctx[SC_MTIME] == mt2 { ok10 = 1 } } // count restored + mtime updated 96 } 97 } 98 gg_check("hot-reload-changed-fires" as *u8, ok10, pass) 99 // T11/T12 HISTORY (TSDB): re-run (T10 reloaded -> verdicts cleared), sample once -> passer uptime 1000 100 // permille (1/1 SERVING), failer 0 (0/1). Proves append -> single-pass scan -> permille end to end. 101 sd_run_all(ctx) 102 sd_sample(ctx, "knowledge/test.ring" as *u8, sys_now_realtime_sec(), SD_UPWINDOW_S) 103 let a_up: *i64 = sc_arr(ctx, SC_UPTIME) 104 let ip2: i64 = gg_idx(ctx, "passer" as *u8) 105 let if3: i64 = gg_idx(ctx, "failer" as *u8) 106 gg_check("history-uptime-passer-1000" as *u8, (a_up[ip2] == SD_PERMILLE) as i64, pass) 107 gg_check("history-uptime-failer-0" as *u8, (a_up[if3] == 0) as i64, pass) 108 109 sd_app_stub("SWEEP-DAEMON-GATE pass=" as *u8) 110 // multi-digit-safe score via the lib's allocation-free renderer 111 let b: *u8 = sys_mmap(GG_HTML_CAP); let bop: *i64 = sys_mmap(GG_PTR) as *i64; bop[0] = 0 112 sd_appn(b, bop, GG_HTML_CAP, pass[0]); sd_app(b, bop, GG_HTML_CAP, "/" as *u8); sd_appn(b, bop, GG_HTML_CAP, GG_CHECKS) 113 sys_write(1, b, bop[0]) 114 if pass[0] == GG_CHECKS { sd_app_stub(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 115 sd_app_stub(" verdict=RED\n" as *u8) 116 sys_exit(1) 117 return 1 118}