From 7be5f05486dc972422c0243f7e0affe7c0dad523 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Wed, 23 Apr 2014 11:23:30 +0300 Subject: [PATCH] Fix tests --- lib/Gruntmaster/Daemon.pm | 43 ++++++++++++++++++++--------------- t/01-jobs.t | 33 +++++++++++---------------- t/problems/increment/meta.yml | 2 +- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/Gruntmaster/Daemon.pm b/lib/Gruntmaster/Daemon.pm index 7fef7f6..6cc6431 100644 --- a/lib/Gruntmaster/Daemon.pm +++ b/lib/Gruntmaster/Daemon.pm @@ -32,7 +32,7 @@ use constant FORMAT_EXTENSION => { ################################################## -my $db = Gruntmaster::Data->connect('dbi:Pg:'); +my $db; my $ua = LWP::UserAgent->new; my @purge_hosts = exists $ENV{PURGE_HOSTS} ? split ' ', $ENV{PURGE_HOSTS} : (); @@ -68,24 +68,28 @@ sub process{ my $meta; our $errors = ''; try { - $meta = { - problem => $jobr->problem->id, - files => { - prog => { - name => 'prog.' . $jobr->extension, - format => $jobr->format, - content => $jobr->source, + if (ref $job) { + $meta = $job; + } else { + $meta = { + problem => $jobr->problem->id, + files => { + prog => { + name => 'prog.' . $jobr->extension, + format => $jobr->format, + content => $jobr->source, + }, }, - }, - map { $_ => $jobr->problem->get_column($_) } qw/generator runner judge testcnt timeout olimit/ - }; - $meta->{tests} = decode_json $jobr->problem->tests if $meta->{runner} eq 'File'; + map { $_ => $jobr->problem->get_column($_) } qw/generator runner judge testcnt timeout olimit/ + }; + $meta->{tests} = decode_json $jobr->problem->tests if $meta->{runner} eq 'File'; - $meta->{files}{ver} = { - name => 'ver.' . FORMAT_EXTENSION->{$jobr->problem->verformat}, - format => $jobr->problem->verformat, - content => $jobr->problem->versource, - } if $jobr->problem->verformat; + $meta->{files}{ver} = { + name => 'ver.' . FORMAT_EXTENSION->{$jobr->problem->verformat}, + format => $jobr->problem->verformat, + content => $jobr->problem->versource, + } if $jobr->problem->verformat; + } prepare_files $meta; chomp $errors; @@ -123,6 +127,7 @@ sub process{ my %results = $judge->(@results); $meta->{$_} = $results{$_} for keys %results; + $meta->{results} = \@full_results } catch { s,(.*) at .*,$1,; chomp; @@ -131,10 +136,11 @@ sub process{ }; get_logger->info("Job result: " . $meta->{result_text}); + return unless $jobr; $jobr->update({ result => $meta->{result}, result_text => $meta->{result_text}, - results => encode_json \@full_results, + results => encode_json $meta->{results}, $errors ? (errors => $errors) : () }); @@ -164,6 +170,7 @@ sub got_job{ } sub run{ + $db = Gruntmaster::Data->connect('dbi:Pg:'); Log::Log4perl->init('/etc/gruntmasterd/gruntmasterd-log.conf'); get_logger->info("gruntmasterd $VERSION started"); chdir tempdir 'gruntmasterd.XXXX', CLEANUP => 1, TMPDIR => 1; diff --git a/t/01-jobs.t b/t/01-jobs.t index 23a6c35..16a8824 100644 --- a/t/01-jobs.t +++ b/t/01-jobs.t @@ -11,6 +11,7 @@ use Cwd qw/cwd/; use File::Basename qw/fileparse/; use File::Slurp qw/read_file/; use File::Temp qw/tempdir/; +use Hash::Merge qw/merge/; use List::Util qw/sum/; use Log::Log4perl; use Test::More; @@ -31,12 +32,7 @@ Log::Log4perl->init(\$log_conf); $ENV{PATH}.=':' . cwd; sub check_job{ - my $job = shift; - my $jobh = get_job $job; - my $meta = job_inmeta $job; - $meta->{result} = $jobh->{result}; - $meta->{result_text} = $jobh->{result_text}; - $meta->{results} = $jobh->{results}; + my $meta = shift; if (defined $meta->{results}) { delete $meta->{results}[$_]{time} for keys $meta->{results}; } @@ -54,32 +50,29 @@ my $tempdir = tempdir CLEANUP => 1; my $job = 0; for my $problem (@problems) { - my $meta = LoadFile "$problem/meta.yml"; - for (1 .. $meta->{testcnt}) { - $meta->{infile}[$_ - 1] = read_file "$problem/$_.in" if $meta->{generator} eq 'File'; - $meta->{okfile}[$_ - 1] = read_file "$problem/$_.ok" if $meta->{runner} eq 'File'; + my $pbmeta = LoadFile "$problem/meta.yml"; + for (1 .. $pbmeta->{testcnt}) { + $pbmeta->{infile}[$_ - 1] = read_file "$problem/$_.in" if $pbmeta->{generator} eq 'File'; + $pbmeta->{okfile}[$_ - 1] = read_file "$problem/$_.ok" if $pbmeta->{runner} eq 'File'; } - if (exists $meta->{files}) { - $_->{content} = read_file "$problem/$_->{name}" for values $meta->{files} + if (exists $pbmeta->{files}) { + $_->{content} = read_file "$problem/$_->{name}" for values $pbmeta->{files} } - set_problem_meta scalar fileparse($problem), $meta; TODO: { - local $TODO = $meta->{todo} if exists $meta->{todo}; - note "Now testing problem $meta->{name} ($meta->{description})"; + local $TODO = $pbmeta->{todo} if exists $pbmeta->{todo}; + note "Now testing problem $pbmeta->{name} ($pbmeta->{description})"; for my $source (<$problem/tests/*>) { my $meta = LoadFile "$source/meta.yml"; $meta->{files}{prog}{content} = read_file "$source/$meta->{files}{prog}{name}"; - $job++; - set_job_inmeta $job, $meta; - set_job_problem $job, scalar fileparse $problem; + $meta = merge $meta, $pbmeta; note "Running $meta->{test_name} ($meta->{test_description})..."; my $savedcwd = cwd; chdir $tempdir; - Gruntmaster::Daemon::process $job; - check_job $job; + Gruntmaster::Daemon::process $meta; chdir $savedcwd; + check_job $meta; } } } diff --git a/t/problems/increment/meta.yml b/t/problems/increment/meta.yml index dc01159..ebf126a 100644 --- a/t/problems/increment/meta.yml +++ b/t/problems/increment/meta.yml @@ -8,6 +8,6 @@ timeout: 1 mlimit: 20971520 description: A trivial interactive problem files: - int: + ver: format: CPP name: int.cpp -- 2.39.2