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