nx_session_test.nx source
↩ module page · 109 lines · 4868 B
1// nx_session_test.nx -- smoke for the composite session primitive.
2
3import "nx_syscalls.nx"
4import "nx_tier.nx"
5import "nx_actor.nx"
6import "nx_message.nx"
7import "nx_stream_multiplexer.nx"
8import "nx_resource_arbiter.nx"
9import "nx_interrupt_broker.nx"
10import "nx_session.nx"
11
12func main() -> i64 {
13 let now: nx_size = 1000000
14
15 // 1: verdict enum
16 if nx_se_v_is_valid(NX_SE_V_OK) != 1 { return 1 }
17 if nx_se_v_is_valid(NX_SE_V_BAD_CAPACITY) != 1 { return 2 }
18 if nx_se_v_is_valid(NX_SE_V_NULL) != 1 { return 3 }
19 if nx_se_v_is_valid(NX_SE_V_N) != 0 { return 4 }
20 if nx_se_v_is_valid(-1) != 0 { return 5 }
21
22 // 2: default constructor produces a fully-wired session
23 let s: *NxSession = nx_session_new_default(now)
24 if (s as i64) == 0 { return 6 }
25 if nx_session_verdict(s) != NX_SE_V_OK { return 7 }
26 if nx_session_is_ready(s) != 1 { return 8 }
27 if nx_session_actor_count(s) != 0 { return 9 }
28
29 // 3: each sub-substrate is reachable
30 if (s.scheduler as i64) == 0 { return 10 }
31 if (s.bus as i64) == 0 { return 11 }
32 if (s.multiplexer as i64) == 0 { return 12 }
33 if (s.arbiter as i64) == 0 { return 13 }
34 if (s.broker as i64) == 0 { return 14 }
35
36 // 4: bad-capacity rejection on explicit constructor
37 let s_bad: *NxSession = nx_session_new(0, 8, 16, 16, 32, 16, 8, 1000000, 16, 50000, now)
38 if (s_bad as i64) == 0 { return 15 }
39 if nx_session_verdict(s_bad) != NX_SE_V_BAD_CAPACITY { return 16 }
40 if nx_session_is_ready(s_bad) != 0 { return 17 }
41
42 let s_bad2: *NxSession = nx_session_new(8, 0, 16, 16, 32, 16, 8, 1000000, 16, 50000, now)
43 if nx_session_verdict(s_bad2) != NX_SE_V_BAD_CAPACITY { return 18 }
44
45 let s_bad_q: *NxSession = nx_session_new(8, 8, 16, 16, 32, 16, 8, 1000000, 0, 50000, now)
46 if nx_session_verdict(s_bad_q) != NX_SE_V_BAD_QUANTUM { return 19 }
47
48 // 5: spawn actor through composite helper -- one call registers scheduler + mailbox
49 if nx_session_spawn_actor(s, 1001, 1, 80, 0, now) != NX_SE_V_OK { return 20 }
50 if nx_session_actor_count(s) != 1 { return 21 }
51
52 // Mailbox should exist
53 let mb: *NxMailbox = nx_ms_find_mailbox(s.bus, 1001)
54 if (mb as i64) == 0 { return 22 }
55
56 // Scheduler actor exists with right priority
57 let a: *NxActor = nx_ac_find(s.scheduler, 1001)
58 if (a as i64) == 0 { return 23 }
59 if a.priority != 80 { return 24 }
60
61 // 6: subscribe through composite helper
62 if nx_session_spawn_actor(s, 1002, 4, 50, 0, now) != NX_SE_V_OK { return 25 }
63 if nx_session_subscribe(s, 1002, NX_MS_KIND_LLM_TOKEN) != NX_SE_V_OK { return 26 }
64 if nx_ms_subscription_count(s.bus, NX_MS_KIND_LLM_TOKEN) != 1 { return 27 }
65
66 // Invalid kind rejected
67 if nx_session_subscribe(s, 1002, 99) != NX_SE_V_INVALID { return 28 }
68
69 // 7: SUB-substrates work end-to-end through the session
70 // Send a message from 1001 to subscriber 1002
71 if nx_ms_send_fanout(s.bus, 1001, NX_MS_KIND_LLM_TOKEN, 0xCAFE, 8, now) != NX_MS_V_DELIVERED { return 29 }
72 if nx_ms_pending(s.bus, 1002) != 1 { return 30 }
73 let msg: *NxMessage = nx_ms_receive(s.bus, 1002)
74 if msg.payload_handle != 0xCAFE { return 31 }
75
76 // Push to multiplexer
77 nx_sm_push(s.multiplexer, 1001, NX_SM_CLASS_LLM_TEXT, 1, 8, 0xBEEF, now)
78 if nx_sm_pending_count(s.multiplexer) != 1 { return 32 }
79
80 // Arbiter accounting
81 nx_ra_set_budget(s.arbiter, NX_RA_KIND_VRAM_BYTES, 1000000)
82 nx_ra_set_max_share(s.arbiter, NX_RA_KIND_VRAM_BYTES, 1024)
83 let out_buf: *u8 = sys_mmap(8)
84 let out_g: *i64 = out_buf as *i64
85 if nx_ra_request(s.arbiter, 1001, NX_RA_KIND_VRAM_BYTES, 500000, 80, 0, out_g, now) != NX_RA_V_GRANTED_FULL { return 33 }
86
87 // Interrupt broker accepts a critical interrupt
88 if nx_ib_file(s.broker, 9001, NX_IB_KIND_OPERATOR_INPUT, NX_IB_DL_REALTIME, 95, 0, now) != NX_IB_V_YIELD { return 34 }
89
90 // 8: spawn on broken session rejected
91 if nx_session_spawn_actor(s_bad, 1, 1, 50, 0, now) != NX_SE_V_INVALID { return 35 }
92 if nx_session_subscribe(s_bad, 1, NX_MS_KIND_LLM_TOKEN) != NX_SE_V_INVALID { return 36 }
93
94 // 9: capacity-exceeded actor spawn rejected
95 let s_small: *NxSession = nx_session_new(2, 8, 16, 16, 32, 16, 8, 1000000, 16, 50000, now)
96 nx_session_spawn_actor(s_small, 1, 1, 50, 0, now)
97 nx_session_spawn_actor(s_small, 2, 1, 50, 0, now)
98 if nx_session_spawn_actor(s_small, 3, 1, 50, 0, now) != NX_SE_V_INVALID { return 37 }
99
100 // 10: null guards
101 let null_s: *NxSession = (0 as i64) as *NxSession
102 if nx_session_verdict(null_s) != NX_SE_V_NULL { return 38 }
103 if nx_session_is_ready(null_s) != 0 { return 39 }
104 if nx_session_actor_count(null_s) != 0 { return 40 }
105 if nx_session_spawn_actor(null_s, 1, 1, 50, 0, now) != NX_SE_V_NULL { return 41 }
106 if nx_session_subscribe(null_s, 1, NX_MS_KIND_LLM_TOKEN) != NX_SE_V_NULL { return 42 }
107
108 return 0
109}