root/trunk/t/retry-delay.t @ 164

Revision 164, 1.8 kB (checked in by miyagawa, 7 months ago)

PostgreSQL patch from clkao

Line 
1# $Id$
2# -*-perl-*-
3
4use strict;
5use warnings;
6
7require 't/lib/db-common.pl';
8
9use TheSchwartz;
10use Test::More tests => 24;
11
12run_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############################################################################
47package Worker::CompleteEventually;
48use base 'TheSchwartz::Worker';
49
50sub 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
61sub keep_exit_status_for { 20 }  # keep exit status for 20 seconds after on_complete
62
63sub max_retries { 2 }
64
65sub 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
Note: See TracBrowser for help on using the browser.