vile / scheme / runtime_control_integration_test.v
70 lines · 67 sloc · 2.93 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import context
4import os
5import time
6
7fn test_cancelled_big_hash_callbacks_release_table_guards_and_managed_files() {
8 directory := os.join_path(os.temp_dir(), 'vile-control-integration-${os.getpid()}-${time.sys_mono_now()}')
9 os.mkdir_all(directory) or { panic(err) }
10 defer { os.rmdir_all(directory) or {} }
11 for alt in [false, true] {
12 mut background := context.background()
13 mut ctx, cancel := context.with_cancel(mut background)
14 defer { cancel() }
15 mut vm := new()
16 path := os.join_path(directory, alt.str())
17 vm.define('path', string_value(path)) or { panic(err) }
18 vm.register('cancel-now', fn [cancel] (mut _vm VM, _args []Value) !Value {
19 cancel()
20 return unspecified()
21 }) or { panic(err) }
22 vm.eval('(define h (make-hashtable
23 (lambda (key) (cancel-now) 18446744073709551616) equal?))') or { panic(err) }
24 if _ := vm.with_context(mut ctx, fn [alt] (mut vm VM) !Value {
25 if alt {
26 return vm.eval_alt('call-with-output-file(path,fn(p){
27 display("buffered",p)
28 hashtable-set!(h,340282366920938463463374607431768211457,42)})')
29 }
30 return vm.eval('(call-with-output-file path (lambda (p)
31 (display "buffered" p)
32 (hashtable-set! h 340282366920938463463374607431768211457 42)))')
33 }) {
34 assert false
35 } else {
36 assert err is CancellationRequest, err.msg()
37 }
38 assert vm.active.len == 0 && isnil(vm.system.evaluation)
39 assert isnil(vm.ports.bindings)
40 assert os.read_file(path) or { panic(err) } == 'buffered'
41 assert vm.eval('(hashtable-size h)') or { panic(err) }.whole() == 0
42 // Reusing the same hash callback outside the expired scope must neither
43 // retain cancellation nor leave the abandoned target guard active.
44 vm.eval('(hashtable-set! h 340282366920938463463374607431768211457 42)') or { panic(err) }
45 assert write(vm.eval('(list (hashtable-size h)
46 (hashtable-ref h 340282366920938463463374607431768211457 #f))') or { panic(err) }) or { panic(err) } == '(1 42)'
47 }
48}
49
50fn test_captured_big_hash_resumes_after_context_expiry_without_replaying_hash_body() {
51 mut background := context.background()
52 mut ctx, cancel := context.with_cancel(mut background)
53 defer { cancel() }
54 mut vm := new()
55 vm.eval('(define saved-hash #f) (define hash-runs 0)
56 (define h (make-hashtable
57 (lambda (key) (set! hash-runs (+ hash-runs 1))
58 (call/cc (lambda (k) (set! saved-hash k) 18446744073709551616))) equal?))') or { panic(err) }
59 vm.with_context(mut ctx, fn (mut vm VM) !Value {
60 return vm.eval('(hashtable-set! h 340282366920938463463374607431768211457 42)')
61 }) or { panic(err) }
62 saved := vm.lookup('saved-hash') or { panic(err) }
63 hash := vm.eval('18446744073709551616') or { panic(err) }
64 cancel()
65 gc_collect()
66 vm.call(saved, [hash]) or { panic(err) }
67 assert write(vm.eval('(list (hashtable-size h) hash-runs)') or { panic(err) }) or { panic(err) } == '(1 1)'
68 assert vm.eval('(hashtable-ref h 340282366920938463463374607431768211457 #f)') or { panic(err) }.whole() == 42
69 assert isnil(vm.system.evaluation)
70}
71