| 1 | module scheme |
| 2 | |
| 3 | import context |
| 4 | import arena |
| 5 | import os |
| 6 | import time |
| 7 | |
| 8 | fn assert_cancellation_request(err IError) { |
| 9 | assert err is CancellationRequest, err.msg() |
| 10 | assert is_control_transfer(err) |
| 11 | if err is CancellationRequest { |
| 12 | assert err.reason !is none |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | fn test_precanceled_host_scope_does_not_run_work_and_restores_plain_evaluation() { |
| 17 | mut background := context.background() |
| 18 | mut ctx, cancel := context.with_cancel(mut background) |
| 19 | defer { cancel() } |
| 20 | cancel() |
| 21 | mut vm := new() |
| 22 | if _ := vm.with_context(mut ctx, fn (mut _vm VM) !Value { |
| 23 | return failure('pre-canceled work ran') |
| 24 | }) { |
| 25 | assert false |
| 26 | } else { |
| 27 | assert_cancellation_request(err) |
| 28 | } |
| 29 | assert isnil(vm.system.evaluation) |
| 30 | assert vm.eval('(+ 20 22)') or { panic(err) }.whole() == 42 |
| 31 | } |
| 32 | |
| 33 | fn test_cancellation_after_host_callback_stops_both_frontends() { |
| 34 | for alt in [false, true] { |
| 35 | mut background := context.background() |
| 36 | mut ctx, cancel := context.with_cancel(mut background) |
| 37 | defer { cancel() } |
| 38 | mut vm := new_with_options(Options{ max_steps: 10000 }) or { panic(err) } |
| 39 | vm.register('stop-now', fn [cancel] (mut _vm VM, _args []Value) !Value { |
| 40 | cancel() |
| 41 | return unspecified() |
| 42 | }) or { panic(err) } |
| 43 | vm.eval('(define observed #f)') or { panic(err) } |
| 44 | if _ := vm.with_context(mut ctx, fn [alt] (mut vm VM) !Value { |
| 45 | return if alt { |
| 46 | vm.eval_alt('stop-now()\nobserved=#t') |
| 47 | } else { |
| 48 | vm.eval('(stop-now) (set! observed #t)') |
| 49 | } |
| 50 | }) { |
| 51 | assert false |
| 52 | } else { |
| 53 | assert_cancellation_request(err) |
| 54 | } |
| 55 | assert !vm.eval('observed') or { panic(err) }.is_true() |
| 56 | assert vm.active.len == 0 && isnil(vm.system.evaluation) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn test_saved_continuation_uses_the_current_host_context() { |
| 61 | mut background := context.background() |
| 62 | mut ctx, cancel := context.with_cancel(mut background) |
| 63 | defer { cancel() } |
| 64 | mut vm := new() |
| 65 | result := vm.with_context(mut ctx, fn (mut vm VM) !Value { |
| 66 | return vm.eval('(define saved #f) (+ 100 (call/cc (lambda(k)(set! saved k) 1)))') |
| 67 | }) or { panic(err) } |
| 68 | assert result.whole() == 101 |
| 69 | cancel() |
| 70 | assert vm.call(vm.lookup('saved') or { panic(err) }, [integer(2)]) or { panic(err) }.whole() == 102 |
| 71 | } |
| 72 | |
| 73 | @[heap] |
| 74 | struct CancellationProbe { |
| 75 | mut: |
| 76 | queries int |
| 77 | done_queries int |
| 78 | work_done int |
| 79 | } |
| 80 | |
| 81 | struct ProbeContext { |
| 82 | done_signal chan int |
| 83 | mut: |
| 84 | probe &CancellationProbe |
| 85 | reason string |
| 86 | } |
| 87 | |
| 88 | fn (mut ctx ProbeContext) done() chan int { |
| 89 | ctx.probe.done_queries++ |
| 90 | return ctx.done_signal |
| 91 | } |
| 92 | |
| 93 | fn (mut ctx ProbeContext) err() IError { |
| 94 | ctx.probe.queries++ |
| 95 | return error(ctx.reason) |
| 96 | } |
| 97 | |
| 98 | fn test_context_error_is_queried_only_after_signal_and_uses_live_host_state() { |
| 99 | probe := &CancellationProbe{} |
| 100 | mut ctx := ProbeContext{ probe: probe, reason: 'old reason' } |
| 101 | mut vm := new() |
| 102 | result := vm.with_context(mut ctx, fn (mut vm VM) !Value { return vm.eval('42') }) or { panic(err) } |
| 103 | assert result.whole() == 42 && probe.queries == 0 |
| 104 | mut ctx_ref := &ctx |
| 105 | vm.register('signal-stop', fn [mut ctx_ref] (mut _vm VM, _args []Value) !Value { |
| 106 | ctx_ref.reason = 'new reason' |
| 107 | ctx_ref.done_signal.close() |
| 108 | return unspecified() |
| 109 | }) or { panic(err) } |
| 110 | if _ := vm.with_context(mut ctx, fn (mut vm VM) !Value { return vm.eval('(signal-stop) 99') }) { |
| 111 | assert false |
| 112 | } else { |
| 113 | assert_cancellation_request(err) |
| 114 | assert err.msg().contains('new reason'), err.msg() |
| 115 | } |
| 116 | assert probe.queries == 1 |
| 117 | } |
| 118 | |
| 119 | fn test_cached_context_signal_is_acquired_once_and_open_context_never_queries_error() { |
| 120 | mut ctx := &ProbeContext{ probe: &CancellationProbe{}, reason: 'not canceled' } |
| 121 | mut vm := new() |
| 122 | result := vm.with_context(mut ctx, fn (mut vm VM) !Value { |
| 123 | vm.eval('(let loop ((n 1000)) (if (= n 0) 0 (loop (- n 1))))')! |
| 124 | vm.eval_alt('20+20')! |
| 125 | return vm.call(vm.lookup('+')!, [integer(20), integer(22)]) |
| 126 | }) or { panic(err) } |
| 127 | assert result.whole() == 42 |
| 128 | assert ctx.probe.done_queries == 1 |
| 129 | assert ctx.probe.queries == 0 |
| 130 | assert isnil(vm.system.evaluation) |
| 131 | } |
| 132 | |
| 133 | fn test_independent_nested_context_cancel_restores_live_parent_scope() { |
| 134 | mut outer := &ProbeContext{ probe: &CancellationProbe{}, reason: 'outer canceled' } |
| 135 | mut inner := &ProbeContext{ probe: &CancellationProbe{}, reason: 'inner canceled' } |
| 136 | mut vm := new() |
| 137 | vm.register('stop-inner', fn [mut inner] (mut _vm VM, _args []Value) !Value { |
| 138 | inner.done_signal.close() |
| 139 | return unspecified() |
| 140 | }) or { panic(err) } |
| 141 | result := vm.with_context(mut outer, fn [mut inner] (mut vm VM) !Value { |
| 142 | parent := vm.system.evaluation |
| 143 | if _ := vm.with_context(mut inner, fn (mut vm VM) !Value { |
| 144 | return vm.eval('(stop-inner) 0') |
| 145 | }) { |
| 146 | return failure('inner context did not cancel') |
| 147 | } else { |
| 148 | assert_cancellation_request(err) |
| 149 | assert err.msg().contains('inner canceled') |
| 150 | } |
| 151 | assert voidptr(vm.system.evaluation) == voidptr(parent) |
| 152 | return vm.eval('42') |
| 153 | }) or { panic(err) } |
| 154 | assert result.whole() == 42 |
| 155 | assert outer.probe.queries == 0 && inner.probe.queries == 1 |
| 156 | assert isnil(vm.system.evaluation) && vm.active.len == 0 |
| 157 | } |
| 158 | |
| 159 | fn test_inner_independent_scope_observes_canceled_active_parent() { |
| 160 | mut outer := &ProbeContext{ probe: &CancellationProbe{}, reason: 'parent reason' } |
| 161 | mut inner := &ProbeContext{ probe: &CancellationProbe{}, reason: 'child reason' } |
| 162 | mut vm := new() |
| 163 | vm.register('stop-parent', fn [mut outer] (mut _vm VM, _args []Value) !Value { |
| 164 | outer.done_signal.close() |
| 165 | return unspecified() |
| 166 | }) or { panic(err) } |
| 167 | if _ := vm.with_context(mut outer, fn [mut inner] (mut vm VM) !Value { |
| 168 | return vm.with_context(mut inner, fn (mut vm VM) !Value { |
| 169 | return vm.eval('(stop-parent) 99') |
| 170 | }) |
| 171 | }) { |
| 172 | assert false |
| 173 | } else { |
| 174 | assert_cancellation_request(err) |
| 175 | assert err.msg().contains('parent reason') |
| 176 | } |
| 177 | assert inner.probe.queries == 0 && outer.probe.queries == 1 |
| 178 | assert !inner.done_signal.closed |
| 179 | assert isnil(vm.system.evaluation) && vm.active.len == 0 |
| 180 | assert vm.eval('42') or { panic(err) }.whole() == 42 |
| 181 | } |
| 182 | |
| 183 | fn test_derived_standard_contexts_cancel_with_their_parent() { |
| 184 | mut background := context.background() |
| 185 | mut parent, cancel_parent := context.with_cancel(mut background) |
| 186 | defer { cancel_parent() } |
| 187 | mut child, cancel_child := context.with_cancel(mut parent) |
| 188 | defer { cancel_child() } |
| 189 | mut vm := new() |
| 190 | vm.register('stop-parent', fn [cancel_parent] (mut _vm VM, _args []Value) !Value { |
| 191 | cancel_parent() |
| 192 | return unspecified() |
| 193 | }) or { panic(err) } |
| 194 | if _ := vm.with_context(mut parent, fn [mut child] (mut vm VM) !Value { |
| 195 | return vm.with_context(mut child, fn (mut vm VM) !Value { |
| 196 | return vm.eval('(stop-parent) 99') |
| 197 | }) |
| 198 | }) { |
| 199 | assert false |
| 200 | } else { |
| 201 | assert_cancellation_request(err) |
| 202 | } |
| 203 | mut parent_done := parent.done() |
| 204 | mut child_done := child.done() |
| 205 | assert parent_done.closed && child_done.closed |
| 206 | assert isnil(vm.system.evaluation) |
| 207 | } |
| 208 | |
| 209 | struct WorkFailure { |
| 210 | Error |
| 211 | marker int |
| 212 | } |
| 213 | |
| 214 | fn (failure &WorkFailure) msg() string { return 'original work failure' } |
| 215 | |
| 216 | fn test_work_failure_wins_over_postwork_cancellation_and_scope_restores_on_exit() { |
| 217 | mut ctx := &ProbeContext{ probe: &CancellationProbe{}, reason: 'canceled after failure' } |
| 218 | mut vm := new() |
| 219 | if _ := vm.with_context(mut ctx, fn [mut ctx] (mut _vm VM) !Value { |
| 220 | ctx.done_signal.close() |
| 221 | return WorkFailure{ marker: 42 } |
| 222 | }) { |
| 223 | assert false |
| 224 | } else { |
| 225 | assert err is WorkFailure |
| 226 | if err is WorkFailure { |
| 227 | assert err.marker == 42 |
| 228 | } |
| 229 | } |
| 230 | assert ctx.probe.queries == 0 |
| 231 | assert isnil(vm.system.evaluation) |
| 232 | mut native_ctx := &ProbeContext{ probe: &CancellationProbe{}, reason: 'native canceled' } |
| 233 | vm.register('fail-and-cancel', fn [mut native_ctx] (mut _vm VM, _args []Value) !Value { |
| 234 | native_ctx.done_signal.close() |
| 235 | return WorkFailure{ marker: 43 } |
| 236 | }) or { panic(err) } |
| 237 | if _ := vm.with_context(mut native_ctx, fn (mut vm VM) !Value { |
| 238 | return vm.call(vm.lookup('fail-and-cancel')!, []) |
| 239 | }) { |
| 240 | assert false |
| 241 | } else { |
| 242 | assert err is WorkFailure |
| 243 | if err is WorkFailure { |
| 244 | assert err.marker == 43 |
| 245 | } |
| 246 | } |
| 247 | assert native_ctx.probe.queries == 0 && isnil(vm.system.evaluation) |
| 248 | mut open := &ProbeContext{ probe: &CancellationProbe{}, reason: 'still open' } |
| 249 | if _ := vm.with_context(mut open, fn (mut vm VM) !Value { return vm.eval('(exit 7)') }) { |
| 250 | assert false |
| 251 | } else { |
| 252 | assert err is ExitRequest |
| 253 | if err is ExitRequest { |
| 254 | assert err.status == 7 |
| 255 | } |
| 256 | } |
| 257 | assert open.probe.queries == 0 && isnil(vm.system.evaluation) |
| 258 | assert vm.eval('42') or { panic(err) }.whole() == 42 |
| 259 | } |
| 260 | |
| 261 | fn test_postwork_empty_evaluation_and_direct_native_call_detect_cancellation() { |
| 262 | for mode in 0 .. 3 { |
| 263 | mut ctx := &ProbeContext{ probe: &CancellationProbe{}, reason: 'direct scope cancel' } |
| 264 | mut vm := new() |
| 265 | vm.register('stop-native', fn [mut ctx] (mut _vm VM, _args []Value) !Value { |
| 266 | ctx.done_signal.close() |
| 267 | ctx.probe.work_done++ |
| 268 | return integer(99) |
| 269 | }) or { panic(err) } |
| 270 | if _ := vm.with_context(mut ctx, fn [mode, mut ctx] (mut vm VM) !Value { |
| 271 | if mode == 2 { return vm.call(vm.lookup('stop-native')!, []) } |
| 272 | ctx.done_signal.close() |
| 273 | return if mode == 0 { integer(99) } else { vm.eval('')! } |
| 274 | }) { |
| 275 | assert false, mode.str() |
| 276 | } else { |
| 277 | assert_cancellation_request(err) |
| 278 | } |
| 279 | assert ctx.probe.queries == 1 |
| 280 | if mode == 2 { |
| 281 | assert ctx.probe.work_done == 1 |
| 282 | } |
| 283 | assert isnil(vm.system.evaluation) && vm.active.len == 0 |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | fn test_native_reentry_inherits_the_context_and_continuations_do_not_capture_it() { |
| 288 | mut old := &ProbeContext{ probe: &CancellationProbe{}, reason: 'old context' } |
| 289 | mut vm := new() |
| 290 | vm.with_context(mut old, fn (mut vm VM) !Value { |
| 291 | return vm.eval('(define saved #f) (define hook (lambda () 0)) |
| 292 | (+ 100 (begin (call/cc (lambda (k) (set! saved k) 1)) (hook)))') |
| 293 | }) or { panic(err) } |
| 294 | old.done_signal.close() |
| 295 | assert vm.call(vm.lookup('saved') or { panic(err) }, [integer(1)]) or { panic(err) }.whole() == 100 |
| 296 | mut current := &ProbeContext{ probe: &CancellationProbe{}, reason: 'new context' } |
| 297 | vm.register('hook', fn [mut current] (mut _vm VM, _args []Value) !Value { |
| 298 | current.done_signal.close() |
| 299 | return integer(2) |
| 300 | }) or { panic(err) } |
| 301 | vm.register('reenter', fn (mut vm VM, _args []Value) !Value { |
| 302 | return vm.call(vm.lookup('saved')!, [integer(2)]) |
| 303 | }) or { panic(err) } |
| 304 | if _ := vm.with_context(mut current, fn (mut vm VM) !Value { return vm.eval('(reenter)') }) { |
| 305 | assert false |
| 306 | } else { |
| 307 | assert_cancellation_request(err) |
| 308 | assert err.msg().contains('new context') |
| 309 | } |
| 310 | assert old.probe.queries == 0 && current.probe.queries == 1 |
| 311 | assert isnil(vm.system.evaluation) && vm.active.len == 0 |
| 312 | assert vm.eval('42') or { panic(err) }.whole() == 42 |
| 313 | } |
| 314 | |
| 315 | fn test_file_entries_preserve_typed_cancellation_in_both_frontends() { |
| 316 | path := os.join_path(os.temp_dir(), 'vile-cancel-source-${os.getpid()}-${time.sys_mono_now()}') |
| 317 | defer { os.rm(path) or {} } |
| 318 | for alt in [false, true] { |
| 319 | os.write_file(path, if alt { |
| 320 | 'stop-now()\nobserved=#t' |
| 321 | } else { |
| 322 | '(stop-now) (set! observed #t)' |
| 323 | }) or { panic(err) } |
| 324 | mut ctx := &ProbeContext{ probe: &CancellationProbe{}, reason: 'file canceled' } |
| 325 | mut vm := new() |
| 326 | vm.eval('(define observed #f)') or { panic(err) } |
| 327 | vm.register('stop-now', fn [mut ctx] (mut _vm VM, _args []Value) !Value { |
| 328 | ctx.done_signal.close() |
| 329 | return unspecified() |
| 330 | }) or { panic(err) } |
| 331 | if _ := vm.with_context(mut ctx, fn [path, alt] (mut vm VM) !Value { |
| 332 | return vm.eval_file_as(path, if alt { .vile } else { .scheme }) |
| 333 | }) { |
| 334 | assert false |
| 335 | } else { |
| 336 | assert_cancellation_request(err) |
| 337 | } |
| 338 | assert !vm.eval('observed') or { panic(err) }.is_true() |
| 339 | assert isnil(vm.system.evaluation) && vm.active.len == 0 |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | fn test_cancellation_closes_managed_files_skips_user_winders_and_cleans_pending_exit() { |
| 344 | path := os.join_path(os.temp_dir(), 'vile-cancel-output-${os.getpid()}-${time.sys_mono_now()}') |
| 345 | defer { os.rm(path) or {} } |
| 346 | for pending_exit in [false, true] { |
| 347 | mut ctx := &ProbeContext{ probe: &CancellationProbe{}, reason: 'file scope canceled' } |
| 348 | mut hooks := &CancellationProbe{} |
| 349 | mut vm := new_with_options(Options{ |
| 350 | exit_handler: fn [mut hooks] (mut _vm VM, _request ExitRequest) ! { hooks.queries++ } |
| 351 | }) or { panic(err) } |
| 352 | base := vm.open_output_string() or { panic(err) } |
| 353 | vm.set_output_port(base) or { panic(err) } |
| 354 | vm.define('path', string_value(path)) or { panic(err) } |
| 355 | vm.eval("(define held #f) (define log '())") or { panic(err) } |
| 356 | vm.register('stop-now', fn [mut ctx] (mut _vm VM, _args []Value) !Value { |
| 357 | ctx.done_signal.close() |
| 358 | return unspecified() |
| 359 | }) or { panic(err) } |
| 360 | mut descriptors := 0 |
| 361 | $if linux { |
| 362 | descriptors = os.ls('/proc/self/fd') or { panic(err) }.len |
| 363 | } |
| 364 | if _ := vm.with_context(mut ctx, fn [pending_exit] (mut vm VM) !Value { |
| 365 | return vm.eval(if pending_exit { |
| 366 | '(dynamic-wind (lambda () 0) |
| 367 | (lambda () (call-with-output-file path (lambda (p) |
| 368 | (set! held p) (display "buffered" p) (exit 7)))) |
| 369 | (lambda () (stop-now)))' |
| 370 | } else { |
| 371 | '(dynamic-wind (lambda () (set! log (cons \'before log))) |
| 372 | (lambda () (call-with-output-file path (lambda (p) |
| 373 | (set! held p) (display "buffered" p) (stop-now)))) |
| 374 | (lambda () (set! log (cons \'after log))))' |
| 375 | }) |
| 376 | }) { |
| 377 | assert false |
| 378 | } else { |
| 379 | assert_cancellation_request(err) |
| 380 | } |
| 381 | assert hooks.queries == 0 |
| 382 | held := vm.lookup('held') or { panic(err) } |
| 383 | assert held.output_pointer().closed && !held.output_pointer().file.is_opened |
| 384 | assert os.read_file(path) or { panic(err) } == 'buffered' |
| 385 | assert eqv(vm.current_output_port() or { panic(err) }, base) |
| 386 | assert isnil(vm.ports.bindings) && isnil(vm.system.evaluation) && vm.active.len == 0 |
| 387 | if !pending_exit { |
| 388 | assert write(vm.eval('log') or { panic(err) }) or { panic(err) } == '(before)' |
| 389 | } |
| 390 | $if linux { |
| 391 | assert os.ls('/proc/self/fd') or { panic(err) }.len == descriptors |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | fn test_threaded_standard_cancellation_and_deadlines_interrupt_instruction_execution() { |
| 397 | mut background := context.background() |
| 398 | for deadline in [false, true] { |
| 399 | mut ctx, cancel := if deadline { |
| 400 | context.with_timeout(mut background, 20 * time.millisecond) |
| 401 | } else { |
| 402 | context.with_cancel(mut background) |
| 403 | } |
| 404 | defer { cancel() } |
| 405 | started := chan int{cap: 1} |
| 406 | cancel_worker := spawn fn (started chan int, cancel context.CancelFn, deadline bool) { |
| 407 | if deadline { return } |
| 408 | if deadline { return } |
| 409 | _ := <-started |
| 410 | if !deadline { |
| 411 | time.sleep(5 * time.millisecond) |
| 412 | cancel() |
| 413 | } |
| 414 | }(started, cancel, deadline) |
| 415 | mut vm := new_with_options(Options{ max_steps: 50000000, max_frames: 10 }) or { panic(err) } |
| 416 | vm.register('started', fn [started] (mut _vm VM, _args []Value) !Value { |
| 417 | started <- 1 |
| 418 | return unspecified() |
| 419 | }) or { panic(err) } |
| 420 | if _ := vm.with_context(mut ctx, fn (mut vm VM) !Value { |
| 421 | return vm.eval('(started) (let loop () (loop))') |
| 422 | }) { |
| 423 | assert false |
| 424 | } else { |
| 425 | assert_cancellation_request(err) |
| 426 | assert err.msg().contains(if deadline { 'deadline' } else { 'canceled' }) |
| 427 | } |
| 428 | cancel_worker.wait() |
| 429 | assert isnil(vm.system.evaluation) && vm.active.len == 0 |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | fn cancellation_region_text(region &arena.Arena, mut ctx ProbeContext) !string { |
| 434 | region.push() |
| 435 | defer { region.pop() } |
| 436 | mut vm := new() |
| 437 | value := vm.with_context(mut ctx, fn (mut vm VM) !Value { |
| 438 | gc_collect() |
| 439 | return vm.eval('(list "region" (+ 20 22))') |
| 440 | })! |
| 441 | return write(value) |
| 442 | } |
| 443 | |
| 444 | fn test_host_context_outside_gc_free_vm_regions_remains_live_after_resets() { |
| 445 | mut ctx := &ProbeContext{ probe: &CancellationProbe{}, reason: 'host alive' } |
| 446 | mut region := arena.new(chunk_size: 1024 * 1024) |
| 447 | defer { region.free() } |
| 448 | for _ in 0 .. 16 { |
| 449 | assert !region.owns(ctx) |
| 450 | borrowed := cancellation_region_text(®ion, mut ctx) or { panic(err) } |
| 451 | owned := borrowed.clone() |
| 452 | region.reset() |
| 453 | assert region.used() == 0 && region.capacity() == 1024 * 1024 |
| 454 | assert owned == '("region" 42)' |
| 455 | unsafe { owned.free() } |
| 456 | } |
| 457 | assert ctx.probe.done_queries == 16 && ctx.probe.queries == 0 |
| 458 | ctx.done_signal.close() |
| 459 | region.push() |
| 460 | mut vm := new() |
| 461 | mut borrowed := '' |
| 462 | if _ := vm.with_context(mut ctx, fn (mut vm VM) !Value { return vm.eval('99') }) { |
| 463 | assert false |
| 464 | } else { |
| 465 | assert_cancellation_request(err) |
| 466 | borrowed = err.msg() |
| 467 | } |
| 468 | region.pop() |
| 469 | owned := borrowed.clone() |
| 470 | region.free() |
| 471 | assert owned == 'evaluation canceled: host alive' |
| 472 | unsafe { owned.free() } |
| 473 | } |
| 474 | |
| 475 | fn test_standard_timer_context_outlives_a_freed_vm_region_after_cancel_returns() { |
| 476 | // The standard timer goroutine sleeps until its original deadline even |
| 477 | // after cancel() returns. Its context and channel must stay outside region. |
| 478 | mut background := context.background() |
| 479 | mut ctx, cancel := context.with_timeout(mut background, 30 * time.millisecond) |
| 480 | defer { cancel() } |
| 481 | cancel() |
| 482 | mut region := arena.new() |
| 483 | region.push() |
| 484 | mut vm := new() |
| 485 | if _ := vm.with_context(mut ctx, fn (mut vm VM) !Value { |
| 486 | return failure('pre-canceled work ran') |
| 487 | }) { |
| 488 | assert false |
| 489 | } else { |
| 490 | assert_cancellation_request(err) |
| 491 | } |
| 492 | region.pop() |
| 493 | region.free() |
| 494 | time.sleep(40 * time.millisecond) |
| 495 | mut done := ctx.done() |
| 496 | assert done.closed |
| 497 | assert ctx.err().msg().contains('canceled') |
| 498 | } |
| 499 | |
| 500 | fn test_open_context_polling_allocates_no_arena_storage() { |
| 501 | mut ctx := &ProbeContext{ probe: &CancellationProbe{}, reason: 'open context' } |
| 502 | mut region := arena.new() |
| 503 | defer { region.free() } |
| 504 | region.push() |
| 505 | defer { region.pop() } |
| 506 | region_ref := ®ion |
| 507 | mut vm := new() |
| 508 | result := vm.with_context(mut ctx, fn [region_ref] (mut vm VM) !Value { |
| 509 | control := vm.system.evaluation |
| 510 | before := region_ref.used() |
| 511 | for _ in 0 .. 10000 { control.check_control()! } |
| 512 | assert region_ref.used() == before |
| 513 | return integer(42) |
| 514 | }) or { panic(err) } |
| 515 | assert result.whole() == 42 |
| 516 | assert ctx.probe.done_queries == 1 && ctx.probe.queries == 0 |
| 517 | } |
| 518 | |