| 1 | # $Id$ |
|---|
| 2 | # -*-perl-*- |
|---|
| 3 | |
|---|
| 4 | use strict; |
|---|
| 5 | use warnings; |
|---|
| 6 | |
|---|
| 7 | require 't/lib/db-common.pl'; |
|---|
| 8 | |
|---|
| 9 | use TheSchwartz; |
|---|
| 10 | use Test::More tests => 24; |
|---|
| 11 | |
|---|
| 12 | run_tests(8, sub { |
|---|
| 13 | my $client = test_client(dbs => ['ts1']); |
|---|
| 14 | |
|---|
| 15 | # insert a job which will fail, fail, then succeed. |
|---|
| 16 | { |
|---|
| 17 | my $handle = $client->insert("Worker::CompleteEventually"); |
|---|
| 18 | isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job"; |
|---|
| 19 | |
|---|
| 20 | $client->can_do("Worker::CompleteEventually"); |
|---|
| 21 | $client->work_until_done; |
|---|
| 22 | |
|---|
| 23 | is($handle->failures, 1, "job has failed once"); |
|---|
| 24 | |
|---|
| 25 | my $job = Worker::CompleteEventually->grab_job($client); |
|---|
| 26 | ok(!$job, "a job isn't ready yet"); # hasn't been two seconds |
|---|
| 27 | sleep 3; # 2 seconds plus 1 buffer second |
|---|
| 28 | |
|---|
| 29 | $job = Worker::CompleteEventually->grab_job($client); |
|---|
| 30 | ok($job, "got a job, since time has gone by"); |
|---|
| 31 | |
|---|
| 32 | Worker::CompleteEventually->work_safely($job); |
|---|
| 33 | is($handle->failures, 2, "job has failed twice"); |
|---|
| 34 | |
|---|
| 35 | $job = Worker::CompleteEventually->grab_job($client); |
|---|
| 36 | ok($job, "got the job back"); |
|---|
| 37 | |
|---|
| 38 | Worker::CompleteEventually->work_safely($job); |
|---|
| 39 | ok(! $handle->is_pending, "job has exited"); |
|---|
| 40 | is($handle->exit_status, 0, "job succeeded"); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | teardown_dbs('ts1'); |
|---|
| 44 | }); |
|---|
| 45 | |
|---|
| 46 | ############################################################################ |
|---|
| 47 | package Worker::CompleteEventually; |
|---|
| 48 | use base 'TheSchwartz::Worker'; |
|---|
| 49 | |
|---|
| 50 | sub work { |
|---|
| 51 | my ($class, $job) = @_; |
|---|
| 52 | my $failures = $job->failures; |
|---|
| 53 | if ($failures < 2) { |
|---|
| 54 | $job->failed; |
|---|
| 55 | } else { |
|---|
| 56 | $job->completed; |
|---|
| 57 | } |
|---|
| 58 | return; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | sub keep_exit_status_for { 20 } # keep exit status for 20 seconds after on_complete |
|---|
| 62 | |
|---|
| 63 | sub max_retries { 2 } |
|---|
| 64 | |
|---|
| 65 | sub retry_delay { |
|---|
| 66 | my $class = shift; |
|---|
| 67 | my $fails = shift; |
|---|
| 68 | return [undef,2,0]->[$fails]; # fails 2 seconds first time, then immediately |
|---|
| 69 | } |
|---|
| 70 | |
|---|