nx_circulation_test.nx source
↩ module page · 64 lines · 2204 B
1// nx_circulation_test.nx -- smoke for nx_circulation.
2
3import "nx_syscalls.nx"
4import "nx_circulation.nx"
5
6func main() -> i64 {
7 if nx_cr_v_is_valid(NX_CR_V_OK) != 1 { return 1 }
8 if nx_cr_v_is_valid(-1) != 0 { return 2 }
9 if nx_cr_v_is_valid(4) != 0 { return 3 }
10
11 if nx_cr_stream_new(1, 0) != (0 as *NxStream) { return 4 }
12
13 let s: *NxStream = nx_cr_stream_new(42, 1000)
14 if s.stream_id != 42 { return 5 }
15 if s.window_size != 1000 { return 6 }
16 if s.bytes_in_flight != 0 { return 7 }
17
18 // Window available initially = full window
19 if nx_cr_available_window(s) != 1000 { return 8 }
20
21 // Send 600 bytes
22 if nx_cr_send(s, 600) != NX_CR_V_OK { return 9 }
23 if s.bytes_in_flight != 600 { return 10 }
24 if nx_cr_available_window(s) != 400 { return 11 }
25
26 // Send 500 -- exceeds window (600+500=1100 > 1000), blocked
27 if nx_cr_send(s, 500) != NX_CR_V_BLOCKED { return 12 }
28
29 // Send 400 -- exactly fills window
30 if nx_cr_send(s, 400) != NX_CR_V_OK { return 13 }
31 if nx_cr_available_window(s) != 0 { return 14 }
32
33 // Send 1 more -- blocked
34 if nx_cr_send(s, 1) != NX_CR_V_BLOCKED { return 15 }
35
36 // Ack 300 -- window opens
37 if nx_cr_ack(s, 300) != NX_CR_V_OK { return 16 }
38 if s.bytes_in_flight != 700 { return 17 }
39 if s.bytes_acked != 300 { return 18 }
40 if nx_cr_available_window(s) != 300 { return 19 }
41
42 // Cannot ack more than in-flight
43 if nx_cr_ack(s, 9999) != NX_CR_V_BLOCKED { return 20 }
44
45 // Receive
46 if nx_cr_receive(s, 500) != NX_CR_V_OK { return 21 }
47 if s.bytes_received_total != 500 { return 22 }
48
49 // Close
50 if nx_cr_close(s) != NX_CR_V_OK { return 23 }
51 if s.state != NX_CR_STATE_CLOSED { return 24 }
52 if nx_cr_send(s, 1) != NX_CR_V_CLOSED { return 25 }
53 if nx_cr_receive(s, 1) != NX_CR_V_CLOSED { return 26 }
54
55 // Null
56 let null_s: *NxStream = (0 as i64) as *NxStream
57 if nx_cr_send(null_s, 1) != NX_CR_V_NULL { return 27 }
58 if nx_cr_ack(null_s, 1) != NX_CR_V_NULL { return 28 }
59 if nx_cr_receive(null_s, 1) != NX_CR_V_NULL { return 29 }
60 if nx_cr_close(null_s) != NX_CR_V_NULL { return 30 }
61 if nx_cr_available_window(null_s) != 0 { return 31 }
62
63 return 0
64}