#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test change to maximum running task count in a job array.
#
# 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-2015 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       "28.10"
set exit_code     0
set array_size    4
set file_script   "test$test_id.sh"
set job_id        0
set task_limit    1

print_header $test_id

if {[get_array_config] < [expr $array_size + 1]} {
	send_user "\nWARNING: MaxArraySize is too small\n"
	exit 0
}

# Submit a job array with 4 elements
make_bash_script $file_script "sleep 20"

spawn $sbatch -n1 --array=0-[expr $array_size -1]%$task_limit --output=/dev/null -t1 $file_script
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "error" {
		send_user "\nFAILURE: sbatch did not submit jobs\n"
		set exit_code 1
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: sbatch did not submit jobs\n"
	exit 1
}

if {[wait_for_job ${job_id}_0 "RUNNING"] != 0} {
	send_user "\nFAILURE: waiting for job to start\n"
	cancel_job $job_id
	exit 1
}

set inx 0

while { $inx < 10 } {
	$bin_sleep 6
	set run_count 0
	spawn $scontrol show job $job_id
	expect {
		-re "RUNNING" {
			incr run_count
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$run_count > $task_limit} {
		send_user "\nFAILURE: too many tasks in the job array are running ($run_count > $task_limit)\n"
		set exit_code 1
	}
	send_user "\n+++ $run_count running tasks +++\n"
	if {$run_count == 0} {
		break
	}
	if {$run_count == $task_limit} {
		incr task_limit
		spawn $scontrol update jobid=$job_id arraytaskthrottle $task_limit
		expect {
			timeout {
				send_user "\nFAILURE: scontrol not responding\n"
				set exit_code 1
			}
			eof {
				wait
			}
		}
	}
	incr inx
}

if {[cancel_job $job_id] != 0} {
	set exit_code 1
}

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