#! /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 Check::SimpleJob;
use strict;
use warnings;

my $testname = "job_core_niceuser_van";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
    DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
    NUM_CPUS = 1
    NEGOTIATOR_INTERVAL = 5
CONDOR_TESTREQ_CONFIG
#endtestreq

CondorTest::runCondorTool("condor_off -negotiator");

my $debuglevel = 2;
my $pid = $$;

# ---  TESTING STRATEGY ----
#  submit a cluster with at least 2 procs to a single slot pool
#  proc0 has nice_user=true, proc1 does not
#  verify that proc1 runs before proc0 by having the jobs
#  append a message to a common file, and then reading the file
#  on completion of the cluster
#

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

my @vdata =
(
"rude",
"rude",
"nice-user",
"nice-user"
);

my @skiplines = ();
my $datafile = "$testname$pid.data";
my $count = 4;

my $SubmitSuccess = sub
{
	my @submitters = ();
	# Make sure both submitters ads (nice and rude) are in the collector
	while ($#submitters  < 1) {
		CondorTest::runCondorTool("condor_status -af Name -submitters", \@submitters, 2, {emit_output=>0});
		sleep(2);
}

	CondorTest::runCondorTool("condor_on -negotiator");
};

my $ExitSuccess = sub 
{
	my %info = @_;

	my $jid = $info{ 'cluster' } . "." . $info{ 'job' };
	$count = $count - 1;
	TLOG("Job $jid Completed..... remaining jobs: $count\n");

	if( $count != 0 )
	{
		return(0);
	}

	my $res = CondorTest::CompareText( $datafile, \@vdata, @skiplines );
	if($res == 0) {
		RegisterResult(0, "text_name", "$testname");
	} else {
		RegisterResult(1, "text_name", "$testname");
	}
};

CreateEmptyFile("$datafile");

SimpleJob::RunCheck(
    runthis=>"x_do_niceuser.pl",
	# "Duration" means "arguments"
	duration=>'$(accounting_group) ' . $datafile,
	should_transfer_files=>"YES",
	when_to_transfer_output=>"ON_EXIT",
	transfer_input_files=>"$datafile",
	transfer_output_files=>"$datafile",
	on_success=>$ExitSuccess,
	on_submit=>$SubmitSuccess,
	queue_sz=>"accounting_group in (nice-user, rude, rude, nice-user)"
);

CondorTest::EndTest();
print scalar localtime() . "\n";
my $result = 0;

if( $result != 0 )
{
    exit(1);
}

CondorTest::debug("$testname SUCCESS\n",1);
exit(0);
