root/trunk/t/cleanup.t @ 114

Revision 114, 2.4 kB (checked in by bradfitz, 3 years ago)

cleanup, prep for a real release.

Line 
1# -*-perl-*-
2
3use strict;
4use warnings;
5
6require 't/lib/db-common.pl';
7
8use TheSchwartz;
9use Test::More tests => 20;
10
11# for testing:
12$TheSchwartz::T_EXITSTATUS_CLEAN_THRES = 1;  # delete 100% of the time, not 10% of the time
13$TheSchwartz::T_ERRORS_MAX_AGE = 2;          # keep errors for 3 seconds, not 1 week
14
15run_tests(10, sub {
16    my $client = test_client(dbs => ['ts1']);
17    my $dbh = DBI->connect(dsn_for("ts1"), 'root', '');
18    $client->can_do("Worker::Fail");
19    $client->can_do("Worker::Complete");
20
21    # insert a job which will fail, then succeed.
22    {
23        my $handle = $client->insert("Worker::Fail");
24        isa_ok $handle, 'TheSchwartz::JobHandle', "inserted job";
25
26        $client->work_until_done;
27        is($handle->failures, 1, "job has failed once");
28
29        my $min;
30        my $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus");
31        is($rows, 1, "has 1 exitstatus row");
32
33        ok($client->insert("Worker::Complete"), "inserting to-pass job");
34        $client->work_until_done;
35        $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus");
36        is($rows, 2, "has 2 exitstatus rows");
37        ($rows, $min) = $dbh->selectrow_array("SELECT COUNT(*), MIN(jobid) FROM error");
38        is($rows, 1, "has 1 error rows");
39        is($min, 1, "error jobid is the old one");
40
41        # wait for exit status to pass
42        sleep 3;
43
44        # now make another job fail to cleanup some errors
45        $handle = $client->insert("Worker::Fail");
46        $client->work_until_done;
47
48        $rows = $dbh->selectrow_array("SELECT COUNT(*) FROM exitstatus");
49        is($rows, 1, "1 exit status row now");
50
51        ($rows, $min) = $dbh->selectrow_array("SELECT COUNT(*), MIN(jobid) FROM error");
52        is($rows, 1, "has 1 error row still");
53        is($min, 3, "error jobid is only the new one");
54
55    }
56
57    teardown_dbs('ts1');
58});
59
60############################################################################
61package Worker::Fail;
62use base 'TheSchwartz::Worker';
63
64sub work {
65    my ($class, $job) = @_;
66    $job->failed("an error message");
67    return;
68}
69
70sub keep_exit_status_for { 1 }  # keep exit status for 20 seconds after on_complete
71
72sub max_retries { 0 }
73
74sub retry_delay { 1 }
75
76# ---------------
77
78package Worker::Complete;
79use base 'TheSchwartz::Worker';
80sub work {
81    my ($class, $job) = @_;
82    $job->completed;
83    return;
84}
85
86sub keep_exit_status_for { 1 }
87
Note: See TracBrowser for help on using the browser.