vile / scheme / arena_usage_context_region_test.v
78 lines · 72 sloc · 2.27 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4import context
5
6struct UsageStaticContext {
7 signal chan int
8mut:
9 done_queries int
10 err_queries int
11 reason string
12}
13
14fn (mut ctx UsageStaticContext) done() chan int {
15 ctx.done_queries++
16 return ctx.signal
17}
18
19fn (mut ctx UsageStaticContext) err() IError {
20 ctx.err_queries++
21 return error(ctx.reason)
22}
23
24fn usage_static_context_work(mut vm VM) !Value { return vm.eval('42') }
25
26fn test_static_context_adapters_survive_256_freed_vm_regions_without_closure_prewarm() {
27 // This file creates no capturing V callbacks and never initializes their
28 // global registry. The context adapter itself must likewise use no closure.
29 mut custom := UsageStaticContext{ reason: 'live reason after repeated scopes' }
30 mut background := context.background()
31 for _ in 0 .. 256 {
32 mut region := arena.new()
33 region.push()
34 mut vm := new()
35 assert vm.with_context(mut custom, usage_static_context_work) or { panic(err) }.whole() == 42
36 assert vm.with_context(mut background, usage_static_context_work) or { panic(err) }.whole() == 42
37 assert isnil(vm.system.evaluation) && vm.active.len == 0
38 region.pop()
39 region.reset()
40 assert region.used() == 0
41 region.free()
42 }
43 assert custom.done_queries == 256 && custom.err_queries == 0
44 custom.reason = 'current mutable reason'
45 mut signal := custom.signal
46 signal.close()
47 mut region := arena.new()
48 region.push()
49 mut vm := new()
50 if _ := vm.with_context(mut custom, usage_static_context_work) {
51 assert false
52 } else {
53 assert err is CancellationRequest
54 assert err.msg().contains('current mutable reason')
55 }
56 assert custom.err_queries == 1 && isnil(vm.system.evaluation)
57 region.pop()
58 region.free()
59 // Construct the standard canceled interface in host storage after the
60 // unseeded static-loop proof. Its error dispatch must preserve the interface.
61 mut canceled, cancel := context.with_cancel(mut background)
62 defer { cancel() }
63 cancel()
64 for _ in 0 .. 64 {
65 mut canceled_region := arena.new()
66 canceled_region.push()
67 mut canceled_vm := new()
68 if _ := canceled_vm.with_context(mut canceled, usage_static_context_work) {
69 assert false
70 } else {
71 assert err is CancellationRequest
72 assert err.msg().contains('context canceled')
73 }
74 assert isnil(canceled_vm.system.evaluation)
75 canceled_region.pop()
76 canceled_region.free()
77 }
78}
79