#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test that multiple srun programs execute simultaneously in the
#          background.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# 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 <http://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 test_id     "1.69"
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

print_header $test_id

set select_type [test_select_type]
if {![string compare $select_type "bluegene"]} {
	send_user "\nWARNING: This test is incompatible with Bluegene systems\n"
	exit $exit_code
} elseif {![string compare $select_type "alps"]} {
	send_user "\nWARNING: This test is incompatible with Cray/ALPS systems\n"
	exit $exit_code
} elseif {![string compare $select_type "linear"]} {
	set def_part_name [default_partition]
	set nb_nodes [get_node_cnt_in_part $def_part_name]
	if {$nb_nodes < 2} {
		send_user "\nWARNING: This test is incompatible with select/linear and only one node\n"
		exit $exit_code
	}
} elseif {[test_launch_poe]} {
	send_user "\nWARNING: This test is incompatible with POE systems\n"
	exit $exit_code
}
if {[test_select_type_params "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 {
		send_user "\nFAILURE: srun is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}


if {$timer_match != [expr $run_timer*10]} {
	send_user "\nFAILURE: srun did not execute all timers ($timer_match != [expr $run_timer*10])\n"
	set exit_code 1
}
if {$time_match != [expr $run_timer*10]} {
	send_user "\nFAILURE: srun is not generating output ($time_match != [expr $run_timer*10])\n"
	set exit_code 1
}
if {$fini_match != $run_timer} {
	send_user "\nFAILURE: srun did not finish the program submitted ($fini_match != $run_timer)\n"
	set exit_code 1
}

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
	exec $bin_rm -f $test_timer $test_srun $file_in
}
exit $exit_code
