module scheme import arena import os import rand fn static_cleanup_require(ok bool, message string) { if !ok { panic(message) } } fn static_cleanup_no_growth(storage &arena.Arena, before int, site int) { after := storage.used() if after != before { panic('cleanup site ${site} allocated ${after - before} bytes') } } @[heap] struct StaticCleanupFailure { identity int } fn (_ &StaticCleanupFailure) msg() string { return 'prepared host flush failure' } fn (problem &StaticCleanupFailure) code() int { return problem.identity } fn static_cleanup_same_error(err IError, problem &StaticCleanupFailure) { if err is StaticCleanupFailure { static_cleanup_require(voidptr(err) == voidptr(problem), 'cleanup replaced the prepared error') static_cleanup_require(err.code() == problem.identity, 'cleanup changed the error code') } else { panic('cleanup lost the prepared host error type') } } // Fixed storage and a prepared error keep the close allocation oracle about // cleanup, rather than allocations made by a growing sink or error factory. @[heap] struct StaticCleanupHost { input string problem &StaticCleanupFailure mut: offset int reads int bytes [64]u8 used int writes int write_limit int fail_write_at int flushes int fail_flush bool } fn (mut host StaticCleanupHost) read(mut destination []u8) !int { host.reads++ count := int_min(destination.len, host.input.len - host.offset) for i in 0 .. count { destination[i] = host.input[host.offset + i] } host.offset += count return count } fn (mut host StaticCleanupHost) write(bytes []u8) !int { host.writes++ if host.writes == host.fail_write_at { return host.problem } count := if host.write_limit > 0 { int_min(host.write_limit, bytes.len) } else { bytes.len } if count > host.bytes.len - host.used { panic('test sink capacity exceeded') } for i in 0 .. count { host.bytes[host.used + i] = bytes[i] } host.used += count return count } fn (mut host StaticCleanupHost) flush() ! { host.flushes++ if host.fail_flush { return host.problem } } fn static_cleanup_bytes(host &StaticCleanupHost, expected string) { static_cleanup_require(host.used == expected.len, 'wrong number of committed output bytes') for i in 0 .. expected.len { static_cleanup_require(host.bytes[i] == expected[i], 'output prefix was lost or duplicated') } } fn static_cleanup_put(mut vm VM, port Value, text string) ! { port_put_string(mut vm, [port, string_value(text)])! } fn static_cleanup_detached(group &CombinedTranscodedPort, port Value) { static_cleanup_require(port.input_pointer().closed && port.output_pointer().closed, 'facade stayed open') static_cleanup_require(!port.input_pointer().busy && !port.output_pointer().busy, 'facade stayed busy') static_cleanup_require(port.input_pointer().source is ClosedPort, 'facade retained its reader') static_cleanup_require(port.output_pointer().sink is ClosedPort, 'facade retained its writer') static_cleanup_require(group.input.raw.source is ClosedPort, 'raw input retained the host') static_cleanup_require(group.output.raw.sink is ClosedPort, 'raw output retained the host') static_cleanup_require(group.input.raw.buffer.len == 0, 'raw input buffer survived final close') static_cleanup_require(group.output.pending.len == 0, 'pending suffix survived final close') } fn test_static_cleanup_combined_flush_error_retains_raw_sink_until_final_retry() { mut storage := arena.new() storage.push() defer { storage.pop() storage.free() } mut vm := new() problem := &StaticCleanupFailure{ identity: 303 } mut host := &StaticCleanupHost{ input: 'RX', problem: problem, fail_flush: true } binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err) } port := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) } group := combined_transcoded_resource(port) static_cleanup_put(mut vm, port, 'hello') or { panic(err) } static_cleanup_require((vm.peek_char(port) or { panic(err) }).whole() == `R`, 'input did not warm') before := storage.used() if _ := vm.close_output_port(port) { panic('combined flush failure was discarded') } else { static_cleanup_same_error(err, problem) } static_cleanup_no_growth(&storage, before, 6) static_cleanup_require(port.output_pointer().closed && !port.input_pointer().closed, 'failed half-close closed the wrong facets') static_cleanup_require(port.output_pointer().sink is ClosedPort, 'closed TX facade retained its adapter') static_cleanup_require(group.output.raw.sink is StaticCleanupHost, 'failed half-close discarded the retry capability') static_cleanup_require(group.output.pending.len == 0, 'flush error reintroduced acknowledged bytes') static_cleanup_require(host.flushes == 1 && host.writes == 1, 'unexpected host activity on half-close') static_cleanup_require((vm.read_char(port) or { panic(err) }).whole() == `R`, 'RX stopped working after failed TX close') static_cleanup_require(host.flushes == 1, 'RX retried TX flush') host.fail_flush = false before_final := storage.used() vm.close_input_port(port) or { panic(err) } static_cleanup_no_growth(&storage, before_final, 7) static_cleanup_detached(group, port) static_cleanup_require(host.flushes == 2 && host.writes == 1, 'final cleanup did not retry only the failed flush') static_cleanup_bytes(host, 'hello') before_repeat := storage.used() vm.close_port(port) or { panic(err) } static_cleanup_no_growth(&storage, before_repeat, 8) static_cleanup_require(host.flushes == 2, 'final cleanup restarted after close') } fn test_static_cleanup_combined_pending_suffix_is_drained_once_without_close_allocations() { mut storage := arena.new() storage.push() defer { storage.pop() storage.free() } mut vm := new() problem := &StaticCleanupFailure{ identity: 404 } mut host := &StaticCleanupHost{ input: 'λRX', problem: problem, write_limit: 2, fail_write_at: 2 } binary := vm.input_output_port(host, host, binary: true, read_size: 1) or { panic(err) } port := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err) } group := combined_transcoded_resource(port) if _ := static_cleanup_put(mut vm, port, 'abcde') { panic('prepared partial write did not fail') } else { static_cleanup_same_error(err, problem) } static_cleanup_bytes(host, 'ab') static_cleanup_require(group.output.pending.len == 3 && group.output.pending_offset == 0, 'unsent suffix was not retained') host.fail_write_at = 3 before := storage.used() if _ := vm.close_output_port(port) { panic('pending-suffix flush did not fail') } else { static_cleanup_same_error(err, problem) } static_cleanup_no_growth(&storage, before, 9) static_cleanup_require(group.output.raw.sink is StaticCleanupHost, 'failed half-close discarded the pending writer') static_cleanup_require(group.output.pending.len == 3 && group.output.pending_offset == 0, 'failed half-close lost its suffix') static_cleanup_require((vm.read_char(port) or { panic(err) }).whole() == `λ`, 'independent RX stopped after TX error') static_cleanup_require(host.writes == 3, 'RX retried pending TX') host.fail_write_at = 0 before_final := storage.used() vm.close_input_port(port) or { panic(err) } static_cleanup_no_growth(&storage, before_final, 10) static_cleanup_detached(group, port) static_cleanup_bytes(host, 'abcde') static_cleanup_require(host.writes == 5 && host.flushes == 1, 'pending suffix was not drained exactly once') } fn test_combined_file_lookup_borrows_the_surviving_facet_without_allocation() { path := os.join_path(os.temp_dir(), 'vile-backend-lookup-' + rand.ulid()) os.write_file(path, 'preserved') or { panic(err) } defer { os.rm(path) or {} } mut storage := arena.new() storage.push() defer { storage.pop() storage.free() } mut vm := new_with_options(Options{ max_open_files: 1 }) or { panic(err) } port := vm.open_file_input_output_port(path, no_create: true no_truncate: true buffer_mode: .none ) or { panic(err) } file := combined_file_resource(port) static_cleanup_require(!isnil(file), 'file backend was not recognized') budget := vm.owner.file_budget static_cleanup_require(budget.used.load() == 1, 'file did not retain its quota slot') before_lookup := storage.used() for _ in 0 .. 16 { static_cleanup_require(voidptr(combined_file_resource(port)) == voidptr(file), 'input lookup copied the backend') } static_cleanup_no_growth(&storage, before_lookup, 20) before_input_close := storage.used() vm.close_input_port(port) or { panic(err) } static_cleanup_no_growth(&storage, before_input_close, 21) static_cleanup_require(port.input_pointer().source is ClosedPort, 'input did not detach') static_cleanup_require(!port.output_pointer().closed, 'closing input closed output') static_cleanup_require(budget.used.load() == 1, 'half-close released the descriptor quota') before_output_lookup := storage.used() for _ in 0 .. 16 { static_cleanup_require(voidptr(combined_file_resource(port)) == voidptr(file), 'output lookup lost the surviving backend') } static_cleanup_no_growth(&storage, before_output_lookup, 22) before_final_close := storage.used() vm.close_output_port(port) or { panic(err) } static_cleanup_no_growth(&storage, before_final_close, 23) static_cleanup_require(port.output_pointer().sink is ClosedPort, 'output did not detach') static_cleanup_require(budget.used.load() == 0, 'final close retained its descriptor quota') before_repeat := storage.used() static_cleanup_require(isnil(combined_file_resource(port)), 'closed facets retained their backend') vm.close_port(port) or { panic(err) } static_cleanup_no_growth(&storage, before_repeat, 24) static_cleanup_require(budget.used.load() == 0, 'repeated close changed the quota') static_cleanup_require((os.read_file(path) or { panic(err) }) == 'preserved', 'closing changed file contents') }