#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test that multiple srun programs execute simultaneously in the
#          background.
############################################################################
# Copyright (C) SchedMD LLC.
#
# 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_dir/script"
set test_timer  "$test_dir/test_timer"
set test_srun   "$test_dir/test_srun"
set run_timer   2

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"
}

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 "
$bin_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 {
			log_error "Timer value $t_num did not match expected value $time_num"
		}
		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
	}
}


subtest {$timer_match == [expr $run_timer*10]} "srun should execute all timers" "$timer_match != [expr $run_timer*10]"
subtest {$time_match == [expr $run_timer*10]} "srun should generate the right output" "$time_match != [expr $run_timer*10]"
subtest {$fini_match == $run_timer} "srun should finish the submitted program" "$fini_match != $run_timer"
