/*
 *  issue-311.sli
 *
 *  This file is part of NEST.
 *
 *  Copyright (C) 2004 The NEST Initiative
 *
 *  NEST is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  NEST is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with NEST.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/* BeginDocumentation

Name: testsuite::issue-311 Ensure NEST behaves well after exception during update

Synopsis: (issue-311) run -> NEST exits if test fails

Description:
This test ensures that NEST behaves properly when a model triggers and
exception during update.

Author: Hans Ekkehard Plesser, 2016-11-02
 */

(unittest) run
/unittest using

M_ERROR setverbosity

modeldict /aeif_cond_alpha known not { /skipped exit_test_gracefully } if

% Execute in stopped context so exception does not propagate out
% pathological parameters to trigger numerical exception
/nrn /aeif_cond_alpha << /I_e 10000000. /g_L 0.01 >> Create def
{ 
  {
    100 Simulate
  } 
  stopped
} assert_or_die  % stopped must return true, otherwise no exception was triggered

% clean up after error
errordict begin /newerror false def end
clear

% Test 1: We can still inspect the kernel after an exception
{  
  % this calls get_time() and triggers assertion prior to fix for #311
  0 GetStatus ;
} pass_or_die

% Test 2: We cannot continue simulation after an exception
{
  % set neuron parameters to values that should stabilize numerics
  nrn << /V_m -70.0 /w 0.0 /I_e 0.0 >> SetStatus
  0.1 Simulate
} fail_or_die

% Test 3: We can simulate again after a ResetKernel
ResetKernel
{
  /aeif_cond_alpha << /I_e 1000. >> Create ;
  100 Simulate
} pass_or_die
