vile / scheme / port_backend_lookup_test.v
227 lines · 210 sloc · 9.68 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4import os
5import rand
6
7fn static_cleanup_require(ok bool, message string) {
8 if !ok { panic(message) }
9}
10
11fn static_cleanup_no_growth(storage &arena.Arena, before int, site int) {
12 after := storage.used()
13 if after != before { panic('cleanup site ${site} allocated ${after - before} bytes') }
14}
15
16@[heap]
17struct StaticCleanupFailure {
18 identity int
19}
20
21fn (_ &StaticCleanupFailure) msg() string { return 'prepared host flush failure' }
22
23fn (problem &StaticCleanupFailure) code() int { return problem.identity }
24
25fn static_cleanup_same_error(err IError, problem &StaticCleanupFailure) {
26 if err is StaticCleanupFailure {
27 static_cleanup_require(voidptr(err) == voidptr(problem), 'cleanup replaced the prepared error')
28 static_cleanup_require(err.code() == problem.identity, 'cleanup changed the error code')
29 } else {
30 panic('cleanup lost the prepared host error type')
31 }
32}
33
34// Fixed storage and a prepared error keep the close allocation oracle about
35// cleanup, rather than allocations made by a growing sink or error factory.
36@[heap]
37struct StaticCleanupHost {
38 input string
39 problem &StaticCleanupFailure
40mut:
41 offset int
42 reads int
43 bytes [64]u8
44 used int
45 writes int
46 write_limit int
47 fail_write_at int
48 flushes int
49 fail_flush bool
50}
51
52fn (mut host StaticCleanupHost) read(mut destination []u8) !int {
53 host.reads++
54 count := int_min(destination.len, host.input.len - host.offset)
55 for i in 0 .. count { destination[i] = host.input[host.offset + i] }
56 host.offset += count
57 return count
58}
59
60fn (mut host StaticCleanupHost) write(bytes []u8) !int {
61 host.writes++
62 if host.writes == host.fail_write_at { return host.problem }
63 count := if host.write_limit > 0 { int_min(host.write_limit, bytes.len) } else { bytes.len }
64 if count > host.bytes.len - host.used { panic('test sink capacity exceeded') }
65 for i in 0 .. count { host.bytes[host.used + i] = bytes[i] }
66 host.used += count
67 return count
68}
69
70fn (mut host StaticCleanupHost) flush() ! {
71 host.flushes++
72 if host.fail_flush { return host.problem }
73}
74
75fn static_cleanup_bytes(host &StaticCleanupHost, expected string) {
76 static_cleanup_require(host.used == expected.len, 'wrong number of committed output bytes')
77 for i in 0 .. expected.len {
78 static_cleanup_require(host.bytes[i] == expected[i], 'output prefix was lost or duplicated')
79 }
80}
81
82fn static_cleanup_put(mut vm VM, port Value, text string) ! {
83 port_put_string(mut vm, [port, string_value(text)])!
84}
85
86fn static_cleanup_detached(group &CombinedTranscodedPort, port Value) {
87 static_cleanup_require(port.input_pointer().closed && port.output_pointer().closed, 'facade stayed open')
88 static_cleanup_require(!port.input_pointer().busy && !port.output_pointer().busy, 'facade stayed busy')
89 static_cleanup_require(port.input_pointer().source is ClosedPort, 'facade retained its reader')
90 static_cleanup_require(port.output_pointer().sink is ClosedPort, 'facade retained its writer')
91 static_cleanup_require(group.input.raw.source is ClosedPort, 'raw input retained the host')
92 static_cleanup_require(group.output.raw.sink is ClosedPort, 'raw output retained the host')
93 static_cleanup_require(group.input.raw.buffer.len == 0, 'raw input buffer survived final close')
94 static_cleanup_require(group.output.pending.len == 0, 'pending suffix survived final close')
95}
96
97fn test_static_cleanup_combined_flush_error_retains_raw_sink_until_final_retry() {
98 mut storage := arena.new()
99 storage.push()
100 defer {
101 storage.pop()
102 storage.free()
103 }
104 mut vm := new()
105 problem := &StaticCleanupFailure{ identity: 303 }
106 mut host := &StaticCleanupHost{ input: 'RX', problem: problem, fail_flush: true }
107 binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err) }
108 port := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) }
109 group := combined_transcoded_resource(port)
110 static_cleanup_put(mut vm, port, 'hello') or { panic(err) }
111 static_cleanup_require((vm.peek_char(port) or { panic(err) }).whole() == `R`, 'input did not warm')
112 before := storage.used()
113 if _ := vm.close_output_port(port) {
114 panic('combined flush failure was discarded')
115 } else {
116 static_cleanup_same_error(err, problem)
117 }
118 static_cleanup_no_growth(&storage, before, 6)
119 static_cleanup_require(port.output_pointer().closed && !port.input_pointer().closed, 'failed half-close closed the wrong facets')
120 static_cleanup_require(port.output_pointer().sink is ClosedPort, 'closed TX facade retained its adapter')
121 static_cleanup_require(group.output.raw.sink is StaticCleanupHost, 'failed half-close discarded the retry capability')
122 static_cleanup_require(group.output.pending.len == 0, 'flush error reintroduced acknowledged bytes')
123 static_cleanup_require(host.flushes == 1 && host.writes == 1, 'unexpected host activity on half-close')
124 static_cleanup_require((vm.read_char(port) or { panic(err) }).whole() == `R`, 'RX stopped working after failed TX close')
125 static_cleanup_require(host.flushes == 1, 'RX retried TX flush')
126 host.fail_flush = false
127 before_final := storage.used()
128 vm.close_input_port(port) or { panic(err) }
129 static_cleanup_no_growth(&storage, before_final, 7)
130 static_cleanup_detached(group, port)
131 static_cleanup_require(host.flushes == 2 && host.writes == 1, 'final cleanup did not retry only the failed flush')
132 static_cleanup_bytes(host, 'hello')
133 before_repeat := storage.used()
134 vm.close_port(port) or { panic(err) }
135 static_cleanup_no_growth(&storage, before_repeat, 8)
136 static_cleanup_require(host.flushes == 2, 'final cleanup restarted after close')
137}
138
139fn test_static_cleanup_combined_pending_suffix_is_drained_once_without_close_allocations() {
140 mut storage := arena.new()
141 storage.push()
142 defer {
143 storage.pop()
144 storage.free()
145 }
146 mut vm := new()
147 problem := &StaticCleanupFailure{ identity: 404 }
148 mut host := &StaticCleanupHost{ input: 'λRX', problem: problem, write_limit: 2, fail_write_at: 2 }
149 binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err) }
150 port := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) }
151 group := combined_transcoded_resource(port)
152 if _ := static_cleanup_put(mut vm, port, 'abcde') {
153 panic('prepared partial write did not fail')
154 } else {
155 static_cleanup_same_error(err, problem)
156 }
157 static_cleanup_bytes(host, 'ab')
158 static_cleanup_require(group.output.pending.len == 3 && group.output.pending_offset == 0, 'unsent suffix was not retained')
159 host.fail_write_at = 3
160 before := storage.used()
161 if _ := vm.close_output_port(port) {
162 panic('pending-suffix flush did not fail')
163 } else {
164 static_cleanup_same_error(err, problem)
165 }
166 static_cleanup_no_growth(&storage, before, 9)
167 static_cleanup_require(group.output.raw.sink is StaticCleanupHost, 'failed half-close discarded the pending writer')
168 static_cleanup_require(group.output.pending.len == 3 && group.output.pending_offset == 0, 'failed half-close lost its suffix')
169 static_cleanup_require((vm.read_char(port) or { panic(err) }).whole() == `λ`, 'independent RX stopped after TX error')
170 static_cleanup_require(host.writes == 3, 'RX retried pending TX')
171 host.fail_write_at = 0
172 before_final := storage.used()
173 vm.close_input_port(port) or { panic(err) }
174 static_cleanup_no_growth(&storage, before_final, 10)
175 static_cleanup_detached(group, port)
176 static_cleanup_bytes(host, 'abcde')
177 static_cleanup_require(host.writes == 5 && host.flushes == 1, 'pending suffix was not drained exactly once')
178}
179
180fn test_combined_file_lookup_borrows_the_surviving_facet_without_allocation() {
181 path := os.join_path(os.temp_dir(), 'vile-backend-lookup-' + rand.ulid())
182 os.write_file(path, 'preserved') or { panic(err) }
183 defer { os.rm(path) or {} }
184 mut storage := arena.new()
185 storage.push()
186 defer {
187 storage.pop()
188 storage.free()
189 }
190 mut vm := new_with_options(Options{ max_open_files: 1 }) or { panic(err) }
191 port := vm.open_file_input_output_port(path,
192 no_create: true
193 no_truncate: true
194 buffer_mode: .none
195 ) or { panic(err) }
196 file := combined_file_resource(port)
197 static_cleanup_require(!isnil(file), 'file backend was not recognized')
198 budget := vm.owner.file_budget
199 static_cleanup_require(budget.used.load() == 1, 'file did not retain its quota slot')
200 before_lookup := storage.used()
201 for _ in 0 .. 16 {
202 static_cleanup_require(voidptr(combined_file_resource(port)) == voidptr(file), 'input lookup copied the backend')
203 }
204 static_cleanup_no_growth(&storage, before_lookup, 20)
205 before_input_close := storage.used()
206 vm.close_input_port(port) or { panic(err) }
207 static_cleanup_no_growth(&storage, before_input_close, 21)
208 static_cleanup_require(port.input_pointer().source is ClosedPort, 'input did not detach')
209 static_cleanup_require(!port.output_pointer().closed, 'closing input closed output')
210 static_cleanup_require(budget.used.load() == 1, 'half-close released the descriptor quota')
211 before_output_lookup := storage.used()
212 for _ in 0 .. 16 {
213 static_cleanup_require(voidptr(combined_file_resource(port)) == voidptr(file), 'output lookup lost the surviving backend')
214 }
215 static_cleanup_no_growth(&storage, before_output_lookup, 22)
216 before_final_close := storage.used()
217 vm.close_output_port(port) or { panic(err) }
218 static_cleanup_no_growth(&storage, before_final_close, 23)
219 static_cleanup_require(port.output_pointer().sink is ClosedPort, 'output did not detach')
220 static_cleanup_require(budget.used.load() == 0, 'final close retained its descriptor quota')
221 before_repeat := storage.used()
222 static_cleanup_require(isnil(combined_file_resource(port)), 'closed facets retained their backend')
223 vm.close_port(port) or { panic(err) }
224 static_cleanup_no_growth(&storage, before_repeat, 24)
225 static_cleanup_require(budget.used.load() == 0, 'repeated close changed the quota')
226 static_cleanup_require((os.read_file(path) or { panic(err) }) == 'preserved', 'closing changed file contents')
227}
228