module scheme import context import os import time fn test_cancelled_big_hash_callbacks_release_table_guards_and_managed_files() { directory := os.join_path(os.temp_dir(), 'vile-control-integration-${os.getpid()}-${time.sys_mono_now()}') os.mkdir_all(directory) or { panic(err) } defer { os.rmdir_all(directory) or {} } for alt in [false, true] { mut background := context.background() mut ctx, cancel := context.with_cancel(mut background) defer { cancel() } mut vm := new() path := os.join_path(directory, alt.str()) vm.define('path', string_value(path)) or { panic(err) } vm.register('cancel-now', fn [cancel] (mut _vm VM, _args []Value) !Value { cancel() return unspecified() }) or { panic(err) } vm.eval('(define h (make-hashtable (lambda (key) (cancel-now) 18446744073709551616) equal?))') or { panic(err) } if _ := vm.with_context(mut ctx, fn [alt] (mut vm VM) !Value { if alt { return vm.eval_alt('call-with-output-file(path,fn(p){ display("buffered",p) hashtable-set!(h,340282366920938463463374607431768211457,42)})') } return vm.eval('(call-with-output-file path (lambda (p) (display "buffered" p) (hashtable-set! h 340282366920938463463374607431768211457 42)))') }) { assert false } else { assert err is CancellationRequest, err.msg() } assert vm.active.len == 0 && isnil(vm.system.evaluation) assert isnil(vm.ports.bindings) assert os.read_file(path) or { panic(err) } == 'buffered' assert vm.eval('(hashtable-size h)') or { panic(err) }.whole() == 0 // Reusing the same hash callback outside the expired scope must neither // retain cancellation nor leave the abandoned target guard active. vm.eval('(hashtable-set! h 340282366920938463463374607431768211457 42)') or { panic(err) } assert write(vm.eval('(list (hashtable-size h) (hashtable-ref h 340282366920938463463374607431768211457 #f))') or { panic(err) }) or { panic(err) } == '(1 42)' } } fn test_captured_big_hash_resumes_after_context_expiry_without_replaying_hash_body() { mut background := context.background() mut ctx, cancel := context.with_cancel(mut background) defer { cancel() } mut vm := new() vm.eval('(define saved-hash #f) (define hash-runs 0) (define h (make-hashtable (lambda (key) (set! hash-runs (+ hash-runs 1)) (call/cc (lambda (k) (set! saved-hash k) 18446744073709551616))) equal?))') or { panic(err) } vm.with_context(mut ctx, fn (mut vm VM) !Value { return vm.eval('(hashtable-set! h 340282366920938463463374607431768211457 42)') }) or { panic(err) } saved := vm.lookup('saved-hash') or { panic(err) } hash := vm.eval('18446744073709551616') or { panic(err) } cancel() gc_collect() vm.call(saved, [hash]) or { panic(err) } assert write(vm.eval('(list (hashtable-size h) hash-runs)') or { panic(err) }) or { panic(err) } == '(1 1)' assert vm.eval('(hashtable-ref h 340282366920938463463374607431768211457 #f)') or { panic(err) }.whole() == 42 assert isnil(vm.system.evaluation) }