#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of srun signal forwarding
#
# 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.
#
# Note:    This script generates and then deletes files in the working directory
#          named test1.32.prog
############################################################################
# Copyright (C) 2002 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# UCRL-CODE-217948.
# 
# This file is part of SLURM, a resource management program.
# For details, see <http://www.llnl.gov/linux/slurm/>.
#  
# 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.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
source ./globals

set test_id     "1.32"
set exit_code   0
set file_prog   "test$test_id.prog"
set matches     0
set usr1cnt     0
set usr2cnt     0

print_header $test_id

#
# Delete left-over program and rebuild it
#
exec $bin_rm -f $file_prog
exec $bin_make -f /dev/null $file_prog
exec $bin_chmod 700 $file_prog

#
# Get uid
#
spawn $bin_id -u
expect {
	-re "($number)" {
		set uid $expect_out(1,string)
	}
	eof {
		wait
	}
}

#
# Spawn initial program via srun
# Note: For systems supporting proper pthreads, instead use
#       exec $bin_kill -USR1 $srun_pid, otherwise we need pkill 
#       and can get multiple signals delivered
#
set timeout $max_job_delay
spawn $srun -N1 -t1 --unbuffered $file_prog
set srun_pid [exp_pid]
expect {
	-re "WAITING" {
		if {$matches == 0} {
			set matches 1
			exec $bin_kill -USR1 $srun_pid
#			exec $bin_pkill -USR1 -n -u $uid srun
		}
		exp_continue
	}
	-re "SIGUSR1" {
		set usr1cnt [expr $usr1cnt + 1]
		if {$matches == 1} {
			set matches 2
			exec $bin_kill -USR2 $srun_pid
#			exec $bin_pkill -USR2 -n -u $uid srun
		}
		exp_continue
	}
	-re "SIGUSR2" {
		set usr2cnt [expr $usr2cnt + 1]
		if {$matches == 2} {
			set matches 3
		}
		exp_continue
	}
	-re "error.*not running" {
		send_user "\nDon't worry about the error...\n"
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		kill_srun
		set exit_code 1
		exp_continue
	}
	eof {
		wait
	}
}
if {$matches != 3} {
	send_user "\nFAILURE: srun failed to forward signals\n"
	set exit_code 1
}
if {($usr1cnt > 1) || ($usr2cnt > 1)} {
	send_user "\nFAILURE: $file_prog received more than one copy of the same signal\n"
	set exit_code 1
}

#
# Post-processing
#
if {$exit_code == 0} {
	exec $bin_rm -f $file_prog
	send_user "\nSUCCESS\n"
}
exit $exit_code
