#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of "scontrol notify <jobid> <message>"
############################################################################
# Copyright (C) 2007 The Regents of the University of California.
# Copyright (C) 2008-2010 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# 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_err       "$test_dir/error"
set file_in        "$test_dir/input"
set file_out       "$test_dir/output"
set job_id         0
set timeout        $max_job_delay

proc cleanup {} {
	global job_id

	cancel_job $job_id
}

make_bash_script $file_in "
$bin_echo Job \$SLURM_JOB_ID is running
$bin_sleep 120
"

#
# Start and subtest a batch job
#
set job_id [submit_job -fail "--output=$file_out --error=$file_err -t1 $file_in"]

# Wait for the job to start running.
# If the slurmd gets the job notify request before the tasks have actually been
# started, the asynchronous call can fail to write to the job's standard error.
wait_for_job -fail $job_id "RUNNING"
wait_for_file -fail $file_out
wait_for_command_match -fail "$bin_cat $file_out" "is running"

#
# Send the batch job a message
#
run_command -fail "$scontrol notify $job_id TEST_NOTIFY"

#
# Test stderr file
#
wait_for_file -fail $file_err
set got_message 0
spawn cat $file_err
expect {
	-re "TEST_NOTIFY" {
		set got_message 1
		exp_continue
	}
	eof {
		wait
	}
}
subtest {$got_message} "Message TEST_NOTIFY should be received by batch job"
cancel_job $job_id

#
# Start and subtest srun
#
set job_id         0
spawn $srun -N1 -t1 -v $bin_sleep 10
set srun_spawn_id $spawn_id
expect {
	-re "jobid ($number)" {
		set job_id $expect_out(1,string)
	}
	timeout {
		fail "srun not responding"
	}
}
if {$job_id == 0} {
	fail "srun failed to create job"
}

#
# Send the program a message
#
run_command -fail "$scontrol notify $job_id TEST_NOTIFY"

#
# Look for the message
#
set got_message 0
set spawn_id $srun_spawn_id
expect {
	-re "TEST_NOTIFY" {
		set got_message 1
	}
	timeout {
		fail "srun not responding"
	}
	eof {
		wait
	}
}
subtest {$got_message} "Message TEST_NOTIFY should be received by srun"
cancel_job $job_id

#
# Start and subtest salloc
#
set job_id         0
spawn $salloc -N1 -t1 $bin_sleep 10
set salloc_spawn_id $spawn_id
expect {
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
	}
	timeout {
		fail "salloc not responding"
	}
}
if {$job_id == 0} {
	fail "salloc failed to create job"
}

#
# Send the program a message
#
run_command -fail "$scontrol notify $job_id TEST_NOTIFY"

#
# Look for the message
#
set got_message 0
set spawn_id $salloc_spawn_id
expect {
	-re "TEST_NOTIFY" {
		set got_message 1
# 		exp_send "exit\n"
	}
	timeout {
		fail "salloc not responding"
	}
	eof {
		wait
	}
}
subtest {$got_message} "Message TEST_NOTIFY should be received by salloc"
