###############################################################################
#
# tcl-reset-cancel2.eagle --
#
# Extensible Adaptable Generalized Logic Engine (Eagle)
#
# Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $
#
###############################################################################

source [file join [file normalize [file dirname [info script]]] prologue.eagle]

###############################################################################

runTest {test tclResetCancel-2.1 {Tcl TIP #285, no reset error} -setup {
  unset -nocomplain interp
  set savedFlags [after flags]
  after flags =Immediate
  set tcl_loaded 0
  if {![tcl ready]} then {tcl load; set tcl_loaded 1}
  set interp [tcl create]
} -body {
  #
  # NOTE: This will not work as "expected".  No Tcl evals are used
  #       for this command; therefore, when Tcl_CancelEval is used,
  #       it will cause the next script evaluated in the interpreter
  #       to be pre-emptively canceled (per the TIP).
  #
  after 1000 [list tcl cancel $interp]
  after 2000 [list interp cancel]
  catch {while {1} {tcl update}}
  tcl update; # make sure the cancellation is delivered.
  tcl eval $interp pid
} -cleanup {
  if {[info exists interp]} then {tcl delete $interp}
  if {$tcl_loaded} then {tcl unload}
  after flags =$savedFlags
  unset -nocomplain savedFlags tcl_loaded interp
} -constraints {eagle tip285 native dynamic tclLibrary86 command.tcl\
compile.NATIVE compile.TCL primaryThread} -returnCodes 1 -result \
{eval canceled}}

###############################################################################

runTest {test tclResetCancel-2.2 {Tcl TIP #285, reset ok} -setup {
  unset -nocomplain interp
  set savedFlags [after flags]
  after flags =Immediate
  set tcl_loaded 0
  if {![tcl ready]} then {tcl load; set tcl_loaded 1}
  set interp [tcl create]
} -body {
  #
  # NOTE: This will work as "expected".  No Tcl evals are used for
  #       this command; therefore, when Tcl_CancelEval is used, it
  #       will cause the next script evaluated in the interpreter
  #       to be pre-emptively canceled (per the TIP).  The solution
  #       presented here is to use the [tcl resetcancel] command on
  #       the interpreter to force the script cancellation flags to
  #       be reset prior to attempting an evaluation.
  #
  after 1000 [list tcl cancel $interp]
  after 2000 [list interp cancel]
  catch {while {1} {tcl update}}
  tcl update; # make sure the cancellation is delivered before the reset.
  tcl resetcancel $interp
  tcl eval $interp pid
} -cleanup {
  if {[info exists interp]} then {tcl delete $interp}
  if {$tcl_loaded} then {tcl unload}
  after flags =$savedFlags
  unset -nocomplain savedFlags tcl_loaded interp
} -constraints {eagle tip285 native dynamic tclLibrary86 command.tcl\
compile.NATIVE compile.TCL primaryThread} -result [pid]}

###############################################################################

source [file join [file normalize [file dirname [info script]]] epilogue.eagle]
