#! /usr/bin/env perl
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************
##
## This test will submit 10000 sleep jobs to get a longer negotiation
## cycle and when they are all submitted will do four things:
##  1. Find the next start of a negotiation cycle
##  2. run a condor_userprio
##	3. find the next end of a negotation cycle
##  4. determine if condor_userprio happened during negotiation cycle
##
##**************************************************************

use CondorTest;
use CondorUtils;
use Check::SimpleJob;
use Check::CondorLog;


my $testname = "job_basic_suspend_continue_test";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	MAX_NEGOTIATOR_LOG = 5000000000
	MAX_SCHEDD_LOG = 5000000000
	SUSPEND = TotalClaimRunTime > 20 && (TotalClaimSuspendTime is undefined || TotalClaimSuspendTime < 20)
	CONTINUE = TotalClaimSuspendTime > 20
	WANT_SUSPEND = True
	PREEMPT = FALSE
	KILL = FALSE
CONDOR_TESTREQ_CONFIG
#endtestreq

	#NEGOTIATE_ALL_JOBS_IN_CLUSTER = True

my $suspend_count = 0;
my $continue_count = 0;


my $on_execute = sub {
	TLOG("Job Executing\n");
};

my $on_suspend = sub {
	$suspend_count += 1;
	TLOG("Job Suspended ($suspend_count times)\n");
};

my $on_unsuspend = sub {
	$continue_count += 1;
	TLOG("Job Continued ($continue_count times)\n");
};

my $on_disconnect = sub {
	TLOG("Job Disconnected\n");
};

my $on_reconnect = sub {
	TLOG("Job Reconnected\n");
};


my $result = 1;
my $test_name = "job_basic_suspend_continue_test";


#Do a job before setting tesing exit codes
print "First test basic job\n";
$result = SimpleJob::RunCheck(); # jobid 1

$result = SimpleJob::RunCheck(
	test_name => "$test_name",
	on_suspended => $on_suspend,
	on_unsuspended => $on_unsuspend,
	on_execute => $on_execute,
	on_disconnected => $on_disconnect,
	on_reconnected => $on_reconnect,
	duration => 60,
);

RegisterResult($suspend_count, check_name => "Job Was Suspended");
RegisterResult($continue_count, check_name => "Job Was Continued");

CondorTest::EndTest();

