#! /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.
##
##**************************************************************

use CondorTest;
use CondorUtils;
use Cwd;
use Check::SimpleJob;

$testname = "job_core_max-running_local";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
    DAEMON_LIST = MASTER,SCHEDD
    SHARED_PORT_PORT = 0
    WANT_SUSPEND = FALSE
    START_LOCAL_UNIVERSE = TotalLocalJobsRunning < 1
CONDOR_TESTREQ_CONFIG
#endtestreq

my $mypid = $$;
TLOG "Test $testname started pid=$mypid\n";

my $testoutput = "max-running$mypid";
my $testjobs = 4;
my $jobscompleted = 0;

print "My PID is $mypid\n";


##
## Sleep time 
## This needs to match the arguements value that is in the .cmd file
##
my $SLEEP_TIME = 10; # seconds
	
##
##
## If the runs succeed, but when we analyze the jobs' runtimes
## and see that they are running when they shouldn't, we will store
## the error message in here
##
my $testFailure = "";

##
## Each job will write to its own file, so we need to just check
## to the time that they said they completed is greater than or equal
## to the time of the last job
##
$successCallback = sub {
	my %info = @_;
	my $cluster = $info{"cluster"};
	my $job = $info{"job"};
	
	$jobscompleted += 1;
	if($jobscompleted < $testjobs) {
		return(0);
	}
	##
	## This is the time that they said they ran on the execute machine
	##
	my $outfile = $testoutput;
	open(FILE, "<$outfile") || die("Failed to open output file '$outfile'");
	my @output = <FILE>;
	close(FILE);

	my $lasttime = 0;
	my $status = 0;
	my $diff = 0;

	foreach my $time (@output) {
		if($lasttime == 0) {
			$lasttime = $time; # initiaize time start bracket
		} else {
			$diff = $time - $lasttime;
			print "time between jobs starting: $diff\n";
			## SLEEP_TIME - 1 to avoid rounding errors
			if( $diff < ($SLEEP_TIME - 1)) {
				print "More then one job ran\n";
				RegisterResult(0,"test_name","$testname");
				return(1);
			}
			$lasttime = $time; # initiaize time start bracket
		}
	}
	#$LAST_RUN_TIME = $reportTime;
	print "No overlapping local universe jobs\n";
	RegisterResult(1,"test_name","$testname");
	return ( 0 );
};
	
$executed = sub {
	my $cmd = "condor_q";
	my @cmdarray = ();
	my $status = CondorTest::runCondorTool($cmd,\@cmdarray,2,{emit_output=>1});
};

$submitted = sub {
};
	
my $success = 1;

print "$ENV{CONDOR_CONFIG}\n\n";

my $result = SimpleJob::RunCheck(
	test_name => $testname,
	runthis => 'x_time.pl',
	duration => "$SLEEP_TIME $testoutput",
	queue_sz => $testjobs,
	universe => "local",
	on_execute => $executed,
	on_submit => $submitted,
	on_success => $successCallback,
	);

CondorTest::EndTest();

exit(0);
