#
# Test script for the batch_queue library 
#
# File: batch_queue/tests/test_all.tcl
# 
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
# 
# ------------------------------------------------------------------------------------

package require batch_queue

# Note that we're not using shellscripts now, rather tcl procs defined in server
# 
#set echo_shellscript [file join [pwd] [file dirname [info script]] echo.sh]
#set segv_shellscript [file join [pwd] [file dirname [info script]] sigsegv.sh]
set echo_shellscript "batch_queue_icbqs_server_test_echo 20"
set segv_shellscript "batch_queue_icbqs_server_test_sigsegv"

set bqs "IcBQS"

# ------------------------------------------------------------------------------------


set bqid1 [batch_queue::init_queue $bqs -servers [list dorcas helios]]
if {"" == $bqid1} {
    puts "TEST--ERROR-->failed to init the \"$bqs\" server: $bqid1"
    return 1
}


set job_token1 [batch_queue::submit_job $bqid1 "$echo_shellscript" -priority 1 -dispatch 0]


if [batch_queue::is_job_running $job_token1] {
    puts "TEST--ERROR-->why does queue think that job \"$job_token\" is running?"
}


if [batch_queue::are_jobs_queued $bqid1] {
    
    set big_job [batch_queue::highest_priority_queued_job $bqid1]
    
    if ![string equal $big_job $job_token1] {
	puts "TEST--ERROR-->I kind if expected the big job to be $job_token1, but it was $big_job"
    }
}


batch_queue::queue_summary $bqid1


#
# --> Now start another queue: bqid2
#


set bqid2 [batch_queue::init_queue $bqs -servers [list maxi]]
if {"" == $bqid2} {
    puts "TEST--ERROR-->failed to init the \"$bqs\" server: $bqid2"
    return 1
}


#
# --> Submit a job to bqid2 for immediate dispatch (no args)
#

set job_token2 [batch_queue::submit_job $bqid2 "$echo_shellscript"]

batch_queue::queue_summary $bqid2


#
# --> Exec the job that we submitted without the "-dispatch 0" directive
#


set job_res1 [batch_queue::exec_job $job_token1]


#
# --> Let's queue a job (-dispatch 0), and then use batch_queue::exec_queued_jobs
#


set job_token1 [batch_queue::submit_job $bqid1 "$echo_shellscript" -priority 3 -dispatch 0]

batch_queue::queue_summary $bqid1

batch_queue::exec_queued_jobs $bqid1

batch_queue::queue_summary $bqid1

#
# --> No jobs should be queued in bqid1 at this point; if so, error
#


if [batch_queue::are_jobs_queued $bqid1] {
    puts "TEST--ERROR--> tests are still queued in queue \"$bqid1\"!"
    return 1
}


#
# --> Kill any remaining RUNNING jobs in bqid1
#


if [batch_queue::are_jobs_running $bqid1] {
    
    set running_jobs [batch_queue::running_job_token_list $bqid1]
    foreach job $running_jobs {
	
	puts "job is $job"
	if {[batch_queue_cmd_success] != [batch_queue::delete_job $job]} {
	    puts "job is $job"
	    
	    puts "TEST--ERROR--> could not kill job \"$job\" running in queue \"$bqid1\"!"
	    return 1
	}
    }
    
}


#
# --> Dispatch some jobs to *particular* servers (useful for regression test per plat)
#     The first one should get killed because it can only run for 5 seconds, and the second
#     should complete normally, assuming echo cmd has arg for sleeping for 10 seconds (see above).
#


set job_tokenx [batch_queue::submit_job $bqid1 "$echo_shellscript" -server dorcas -max_time 5]
set job_tokenx [batch_queue::submit_job $bqid1 "$echo_shellscript" -server helios -max_time 20]


#
# --> Queue a job in order that we can testing deleting it.
#


set job_token3 [batch_queue::submit_job $bqid1 "$echo_shellscript" -priority 1 -dispatch 0]

if [batch_queue::are_jobs_queued $bqid1] {
    
    batch_queue::queue_summary $bqid1
    
    set queued_jobs [batch_queue::queued_job_token_list $bqid1]
    
    foreach job $queued_jobs {
	
	if {[batch_queue_cmd_success] != [batch_queue::delete_job $job]} {
	    
	    puts "TEST--ERROR--> could not kill job \"$job\" queued in queue \"$bqid1\"!"
	    return 1
	}
    }
}


#
# --> While away the time around while jobs are still messing around
#

    
batch_queue::queue_summary $bqid1
batch_queue::queue_summary $bqid2

while { [batch_queue::are_jobs_running $bqid1] } {
    puts "(...waiting 1000 ms for queue \"$bqid1\" to finish...)"
    after 1000
}

while { [batch_queue::are_jobs_running $bqid2] } {
    puts "(...waiting 1000 ms for queue \"$bqid2\" to finish...)"
    after 1000
}
puts "(...jobs are done...)"

batch_queue::queue_summary $bqid1
batch_queue::queue_summary $bqid2


#
# --> Dispatch a job which will have a segmentation fault
#


set job_token5 [batch_queue::submit_job $bqid1 "$segv_shellscript"]

set status [batch_queue::get_job_status $job_token5]
while { [batch_queue::is_job_running $job_token5] } {
    puts "(...waiting 1000 ms for queue \"$job_token5\" to finish...it has status \"[batch_queue::get_job_status $job_token5]\")"
    after 1000
}
set status [batch_queue::get_job_status $job_token5]
puts "(...\"$job_token5\" has status \"[batch_queue::get_job_status $job_token5]\"...)"


#
# --> Terminate the queues, closing sockets and destroying job info
#


set status [batch_queue::delete_queue $bqid1]
if {[batch_queue_cmd_success] != $status} {
    puts "TEST--ERROR--> failed to terminate queue \"$bqid1\": $status"
    return 1
}

set status [batch_queue::delete_queue $bqid2]
if {[batch_queue_cmd_success] != $status} {
    puts "TEST--ERROR--> failed to terminate queue \"$bqid2\": $status"
    return 1
}


batch_queue::queue_summary $bqid1
batch_queue::queue_summary $bqid2


puts "\n[batch_queue_cmd_success]"

return 0
