#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test that multiple srun programs execute simultaneously in the
#          background.
############################################################################
# Copyright (C) 2011-2013 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm 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.
#
# Slurm 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 Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set file_in     "test$test_id.bash"
set test_timer  "test$test_id.test_timer"
set test_srun   "test$test_id.test_srun"
set run_timer   2
set exit_code   0

proc cleanup {} {
	global bin_rm test_timer test_srun file_in

	exec $bin_rm -f $test_timer $test_srun $file_in
}

set nb_nodes [get_partition_param [default_partition] "TotalNodes"]
if {[check_config_select "linear"]} {
	if {$nb_nodes < 2} {
		skip "This test is incompatible with select/linear and only one node"
	}
} elseif {[default_part_exclusive]} {
	if {$nb_nodes < 2} {
		skip "This test is incompatible with exclusive node allocations and only one node"
	}
}

if {[param_contains [get_config_param "SelectTypeParameters"] "CR_*MEMORY"]} {
	set mem_opt "--mem=10"
} else {
	set mem_opt "--comment=no_mem"
}

# remove any remaining files
exec $bin_rm -f $test_timer $test_srun $file_in

make_bash_script $test_timer "
for i in \{1..10\}
do
	echo \"timer \$a time \$i\"
	sleep 5
done
echo \"Timer \$a finished\"
"

make_bash_script $test_srun "
set -m
for a in \{1..$run_timer\}
do
  export a
  $srun -v $mem_opt -t1 ./$test_timer &
done
"

make_bash_script $file_in "
bash -i ./$test_srun
"

# We make sure that all timers run at the same time by making sure that
# all timer's report "time 3" before any of them report "time 4", etc.
set timeout $max_job_delay
set tmp_time 1
set time_num 1
set fini_num 1
set timer_match 0
set time_match 0
set fini_match 0
spawn bash -i $file_in
expect {

	-re "timer ($number) time ($number)" {
		set tr_num $expect_out(1,string)
		set t_num  $expect_out(2,string)

		for {set i 1} {$run_timer>=$i} {incr i} {
			if {$tr_num == $i} {
				incr timer_match
			}
		}
		if {$t_num == $time_num} {
			incr time_match
		} else {
			set exit_code 1
		}
		if {$tmp_time == $run_timer} {
			incr time_num
			set tmp_time 0
		}

		incr tmp_time
		exp_continue
	}
	-re "Timer ($number) finished" {
		set tmp_fini $expect_out(1,string)
		for {set i 1} {$run_timer>=$i} {incr i} {
			if {$tmp_fini == $i} {
				incr fini_match
			}
		}
		incr fini_num
		exp_continue
	}
	timeout {
		fail "srun is not responding"
	}
	eof {
		wait
	}
}


if {$timer_match != [expr $run_timer*10]} {
	fail "srun did not execute all timers ($timer_match != [expr $run_timer*10])"
}
if {$time_match != [expr $run_timer*10]} {
	fail "srun is not generating output ($time_match != [expr $run_timer*10])"
}
if {$fini_match != $run_timer} {
	fail "srun did not finish the program submitted ($fini_match != $run_timer)"
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
