vile / scheme / bounded_exit_cleanup_buffers_test.v
61 lines · 56 sloc · 2.35 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4import os
5import time
6
7fn close_pending_exit_without_storage(mut vm VM, machine &Machine, committed bool) {
8 mut denied := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 })
9 denied.push()
10 defer {
11 denied.pop()
12 denied.free()
13 }
14 if committed {
15 request := machine.abandon_process_exit_cleanup(mut vm, &vm_chunk_failure)
16 assert request is ResourceLimitRequest
17 } else {
18 vm.cleanup_pending_exit_files(machine, &vm_chunk_failure)
19 }
20}
21
22fn bounded_exit_cleanup_case(committed bool) ! {
23 path := os.join_path(os.vtmp_dir(), 'vile-exit-quota-${os.getpid()}-${time.sys_mono_now()}')
24 defer { os.rm(path) or {} }
25 mut instance := new_bounded(Options{ max_open_files: 1 }, arena.ChunkLimits{ max_live_bytes: 8 * 1024 * 1024 })!
26 defer { instance.close() or { panic(err) } }
27 instance.eval('(define (saved) 42)')!
28 assert instance.run(fn [path, committed] (mut vm VM) !Value {
29 mut system := vm.system_state()
30 system.exit_function = &process_exit_bridge_function
31 vm.wind_transfer_function = &wind_transfer_bridge_function
32 port := vm.open_output_file(path)!
33 context := vm.next_context()
34 entries := [cons(port, integer(i64(context)))]
35 mut machine := Machine{ context: context }
36 if committed {
37 state := &ProcessExitCleanup{ request: ExitRequest{ status: 0 }, entries: entries, context: context }
38 machine.function = &process_exit_cleanup_bridge
39 machine.env = &Environment{ slots: [payload_value(.port_operation, state)] }
40 } else {
41 target := &Continuation{ owner: vm.owner, context: context, function: system.exit_function, stack: entries }
42 machine.function = vm.wind_transfer_function
43 machine.env = &Environment{ slots: [payload_value(.wind_plan, target), integer(0), payload_value(.wind_frame, unsafe { &WindFrame(nil) })] }
44 }
45 assert vm.owner.file_budget.used.load() == 1
46 close_pending_exit_without_storage(mut vm, &machine, committed)
47 assert port.output_pointer().closed
48 assert vm.owner.file_budget.used.load() == 0
49 assert vm.owner.bounded_files.interrupted == none
50 return vm.call(vm.lookup('saved')!, [])
51 })!.as_integer()! == 42
52 assert instance.eval('(saved)')!.as_integer()! == 42
53}
54
55fn test_committed_exit_cleanup_closes_owned_files_under_zero_allocation_budget() {
56 bounded_exit_cleanup_case(true)!
57}
58
59fn test_pending_exit_cleanup_closes_owned_files_under_zero_allocation_budget() {
60 bounded_exit_cleanup_case(false)!
61}
62